问题描述
我正在学习 Ansible,但我对何时在剧本中使用连字符以及何时不使用连字符感到困惑.据我所知,连字符用于 Ansible 中的列表.
I am learning Ansible but I am getting confused when to use hyphen and when not to use hyphen in playbook. As I know, hyphen is used for list in Ansible.
例如
--- # my first playbook
- hosts: webservers ( why did we use hyphen here it is not a list)
tasks:
- name: installing httpd
yum: name=httpd state=installed ( why we shouldn't use hyphen here).
Ansible 文档中说连字符是用于列表的,例如:
From Ansible documentation, it is said that hyphen is for list, for example:
fruits:
- apple
- grapes
- orange
所以,我很困惑何时使用连字符,何时不使用.
So, I am confused when to use hyphens and when not to use.
推荐答案
Hyphen -
用于指定列表项,冒号 :
用于指定字典项或键值对.我认为与另一种语言(例如 Python)的可比示例将说明这一点.假设您有一个这样的列表 my_list
:
Hyphen -
is used to specify list items, and colon :
is used to specify dictionary items or key-value pair. I think a comparable example with another language (e.g. Python) will make this clear. Let's say you have a list my_list
like this:
my_list = ['foo', 'bar']
在 Ansible 中,您将使用连字符指定此列表项:
In Ansible you will specify this list items with hyphen:
my_list:
- foo
- bar
现在假设您有一个这样的键值对或字典:
Now let's say you have a key-value pair or dictionary like this:
my_dict = {
'key_foo': 'value_foo',
'key_bar': 'value_bar'
}
在 Ansible 中,您将在键值对或字典中使用冒号而不是连字符:
In Ansible, you will use colon instead of hyphen for key-value pair or dictionary:
my_dict:
key_foo: value_foo
key_bar: value_bar
在剧本中,您有一个剧本列表,而在每个剧本中,您都有一个任务列表.由于 tasks
是一个列表,每个任务项都以一个连字符开头,如下所示:
Inside a playbook you have a list of plays and inside each play you have a list of tasks. Since tasks
is a list, each task item is started with a hyphen like this:
tasks:
- task_1
- task_2
现在每个任务本身就是一个字典或键值对.您的示例任务包含两个键,name
和 yum
.yum
本身是另一个字典,带有 name
、state
等键.
Now each task itself is a dictionary or key value pair. Your example task contains two keys, name
and yum
. yum
itself is another dictionary with keys name
, state
etc.
因此要指定任务列表,您可以使用连字符,但由于每个任务都是字典,因此它们包含冒号.
So to specify task list you use hyphen, but since every task is dictionary they contain colon.
这篇关于在ansible中使用连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!