在Ansible中使用连字符 [英] Using hyphen in ansible

查看:119
本文介绍了在Ansible中使用连字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习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.

推荐答案

连字符-用于指定列表项,而冒号:用于指定字典项或键值对.我认为使用其他语言(例如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

现在,每个任务本身都是字典或键值对.您的示例任务包含两个键,nameyum. yum本身是另一本具有键namestate等的字典.

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中使用连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆