FreeKB - Ansible Getting Started with Arrays Dictionary Lists
Ansible - Getting Started with Arrays Dictionary Lists

There are 20+ ways to create variables in Ansible, such as on the command line, in a playbook, or from a file, just to name a few. Refer to Ansible - Getting Started with Variables.

The following terms are synomymous, and can be used interchangeably. Each term refers to a collection of values.

  • array
  • dictionary
  • list

Here is how you would create an empty array that contains no values using the vars plugin. The bracket characters [ ] are used to create an array.

vars:
  fruit: []

 

The debug module can be used to output the entire array.

- name: output the contents of the 'fruit' array
  debug: 
    var: fruit

 

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

TASK [output the contents of the 'fruit' array]
ok: [server1.example.com] => {
    "fruit": null
}

 

Here is how you would create an array of fruit. In this scenario, the values are strings.

vars:
  fruit: 
    - apple
    - banana
    - orange
    - grapes

 

Or like this.

vars:
  fruit: [ apple, banana, orange, grapes ]

 

The debug module can be used to output the entire array, like this.

- name: output the contents of the 'fruit' array
  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' array]
ok: [server1.example.com] => {
    "fruit": [
        "apple",
        "banana",
        "orange",
        "grapes"
    ]
}

 


Booleans

Here is how you would create an array of booleans.

  • boolean = true, false, yes, no, 1, 0
vars:
  valid_values: [ true, false ]

 

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' array]
ok: [server1.example.com] => {
    "msg": [
        true,
        false
    ]
}

 


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 array that contains the values in out.stdout_lines.

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

 

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

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" array.

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

 

Or to append multiple lists to the pids array.

- name: create the 'files' array
  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 array 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 array.

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

 

Which should return the following.

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

 


Looping through a list

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

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

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

 

Which should return the following.

TASK [loop through the 'fruit' array] 
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"
}

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |