Tuesday, May 16, 2017

Where to use Hyphen and colon

Hyphen is used to specify list items, and colon : is used to specify dictionary items or key-value pair. A comparable example with another language (e.g. Python) will make this clear. 

Let's say we have a list my_list like this in Python

my_list = ['foo', 'bar']

In Ansible you will specify this list items with hyphen (Space should be there between hyphen and list items:

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'
}

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

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

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.

Example 

---
- hosts: ss
  tasks:
    - name: test connection
      ping:
      remote_user: bhr_moham607
    - name : test connectin2
      ping:

That's what tasks is. Each hyphen starts a new list item (a task), and each list item may have multiple key-value pairs (properties of the task).

In the above example, name is the word which defines the list of items, so we are adding hyphen to all  list of items as per above example.

No comments:

Post a Comment