YAML: When an equals sign (=) can be used for dictionaries?

ぐ巨炮叔叔 提交于 2019-12-30 07:24:09

问题


I read key1=value1 key2=value2 style dictionaries all the time in ansible playbooks that are supposed to be written in YAML. On the other hand I didn't find any documentation for this format and there seem to be cases where it doesn't work for me. What is the exact specification and where can I find it?


回答1:


In Ansible key=value is not used for dicts in general.

It is an alternative syntax to pass parameters to actions/modules, like:

- name: restart apache
  service: name=apache state=restarted

Here you pass name and state parameters to service module.

From YAML perspective name=apache state=restarted is a string. There's some magic done under the hood by Ansible to split it. But it become unreliable and cumbersome with complex arguments, so I always use native YAML syntax:

- name: restart apache
  service:
    name: apache
    state: restarted

And this key=value works only for modules/actions parameters, you can't define dictionaries like this:

vars:
  # this will give you a string, not dict
  mydict: key1=value1 key2=value 


来源:https://stackoverflow.com/questions/42907723/yaml-when-an-equals-sign-can-be-used-for-dictionaries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!