本文介绍了YAML:什么时候等号(=)可以用于字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读应该用 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

在这里,您将 namestate 参数传递给 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:什么时候等号(=)可以用于字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 20:26