YAML:什么时候等号 (=) 可以用于字典? [英] YAML: When an equals sign (=) can be used for dictionaries?
问题描述
我一直在应该用 YAML 编写的 ansible playbook 中阅读 key1=value1 key2=value2
风格的字典.另一方面,我没有找到这种格式的任何文档,而且似乎在某些情况下它对我不起作用.确切的规格是什么?在哪里可以找到?
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?
推荐答案
在 Ansible 中 key=value
通常不用于 dicts.
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
这里将name
和state
参数传递给service
模块.
Here you pass name
and state
parameters to service
module.
从 YAML 的角度来看 name=apache state=restarted
是一个字符串.Ansible 在幕后做了一些魔法来拆分它.但它变得不可靠和复杂的参数很麻烦,所以我总是使用原生的 YAML 语法:
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
而且这个 key=value
只适用于模块/动作参数,你不能像这样定义字典:
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
这篇关于YAML:什么时候等号 (=) 可以用于字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!