Bootstrap FreeKB - Ansible - Getting Started with Lists
Ansible - Getting Started with Lists

Updated:   |  Ansible articles

Here is how you would create an empty list that contains no values using the vars plugin. The bracket characters [ ] are used to create a list. The debug module can be used to print the list.

---
- hosts: localhost
  vars:
    fruit: []
  tasks:
  - debug: 
      var: fruit
...

 

Which should return the following. "null" is returned since the list is empty.

ok: [localhost] => {
    "fruit": null
}

 


List of string values

Here is how you would create a list that contains values.

---
- hosts: localhost
  vars:
    fruit: 
      - apple
      - banana
      - orange
      - grapes
  tasks:
  - debug: 
      var: fruit
...

 

Or like this.

---
- hosts: localhost
  vars:
    fruit: [ apple, banana, orange, grapes ]
  tasks:
  - debug: 
      var: fruit
...

 

Which should return the following. Notice the values are wrapped in double quotes, meaning the values are strings.

TASK [output the contents of the 'fruit' list]
ok: [server1.example.com] => {
    "fruit": [
        "apple",
        "banana",
        "orange",
        "grapes"
    ]
}

 


Booleans

Here is how you would create a list of booleans.

  • boolean = true, false, yes, no, 1, 0
---
- hosts: localhost
  vars:
    valid_values: [ true, false ]
  tasks:
  - debug:
      var: valid_values
...

 

Which should return the following. Notice the values are not wrapped in double quotes, meaning the values are booleans or integers.

TASK [output the contents of the 'valid_values' list]
ok: [server1.example.com] => {
    "msg": [
        true,
        false
    ]
}

 


List that contains dictionary

Here is an example of how you could append a dictionary inside of a list. Check out my article on appending to a dictionary for more details on this.

---
- hosts: localhost
  vars:
    food: {}
  tasks:
  - set_fact:
      food: "{{ food | combine({'fruit': 'apple'}) }}"

  - set_fact:
      food: "{{ food | combine({'fruit': 'banana'}) }}"

  - debug: 
      var: food
...

 

Which should return the following, showing that the list contains a dictionary.

ok: [localhost] => {
    "food": {
        "fruit": "apple", 
        "veggie": "tomato"
    }
}

 


register out

Let's say you have captured output using the register parameter.

- name: ps command
  shell: "ps -ef | grep foo | awk '{print $2}'"
  register: out

 

The debug module can be used to display the values registered to stdout_lines.

- name: print stdout_lines
  debug:
    var: out.stdout_lines

 

Which could return something like this.

TASK[print stdout_lines]
ok: [localhost] => "{
    "out.stdout_lines" => [
        "847",
        "1044",
        "3399"
    ]
}

 

Here is how you could create an list that contains the values in out.stdout_lines.

- name: create the 'pids' list
  set_fact:
    pids: "{{ items }}"
  vars:
    items: "{{ out.stdout_lines }}"

 

Or sometimes, the values you want to append to the list are already in some other list. In this example, the "path" variable is inside of the out.files list.

ok: [server1.example.com] => {
    "out": {
        "files": [
            {
              "path": "/tmp/foo.txt",
           }
        ]
    }
}

 

In this scenario, here is how you would append the "path" values to the "files" list.

- name: create the 'files' list
  set_fact:
    files: "{{ items }}"
  vars:
    items: "{{ out.files | map(attribute='stdout_lines') | list }}"

 

Or to append multiple lists to the pids list.

- name: create the 'files' list
  set_fact:
    files: "{{ items }}"
  vars:
    items: "{{ foo.files | map(attribute='stdout_lines') | list }} + {{ bar.files | map(attribute='stdout_lines') | list }}"

 


index elements

Each element in the list is assigned an index number, like this.

  • 0 = apple
  • 1 = banana
  • 2 = orange
  • 3 = grapes

Here is how to output a specific element in the list.

- name: output item 2 in the 'fruit' list
  debug: 
    var: fruit[2]

 

Which should return the following.

TASK [output item 2 in the 'fruit' list]
ok: [server1.example.com] => {
    "msg": [
        "orange"
    ]
}

 


Looping through a list

The following parameters can be used to loop through each item in the list.

In this example, the loop parameter is used to loop over an list of fruit.

- name: loop through the 'fruit' list
  debug: 
    msg: "{{ item }}"
  loop:
    - apple
    - banana
    - orange
    - grapes

 

Which should return the following.

TASK [loop through the 'fruit' list] 
ok: [server1.example.com] => (item=apple) => {
    "msg": "apple"
}
ok: [server1.example.com] => (item=banana) => {
    "msg": "banana"
}
ok: [server1.example.com] => (item=orange) => {
    "msg": "orange"
}
ok: [server1.example.com] => (item=grapes) => {
    "msg": "grapes"
}

 

Or, the index_of module can be used to get the index number of an element in a list, to avoid looping through each element in the list, which can be useful in reducing the amount of output returned when looping through a large list.

---
- hosts: localhost
  gather_facts: false
  vars:
    fruit:
     - apple
     - banana
     - orange
     - grapes
  tasks:
  - debug:
      msg: "banana index number = {{ lookup('ansible.utils.index_of', fruit, 'regex', 'banana') }}"
...

 

Running this playbook should return the following.

ok: [localhost] => {
    "msg": "banana index number = 1"
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter db1eb1 in the box below so that we can be sure you are a human.