Bootstrap FreeKB - Ansible - Loop through list using vars
Ansible - Loop through list using vars

Updated:   |  Ansible articles

The following parameters can be used to loop through each item in an array / list.

In this example, the debug module and vars parameter are used to loop over an array of fruit (a fruit loop!).

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

 

Or like this.

---
- hosts: localhost
  vars:
    fruit:
      - apple
      - banana
      - orange
      - grapes
  tasks:
  - debug: 
      var: items
    vars:
      items: "{{ fruit }}"
...

 

Which should return the following.

ok: [localhost] => {
    "items": [
        "apple",
        "banana",
        "orange",
        "grapes"
    ]
}

 

As a bit of a more practical example, let's say you are using the find module to find the files in the /tmp directory. 

---
- hosts: localhost
  tasks:
  - find:
      paths: /tmp
    register: out

  - debug:
      var: out
...

 

Which should produce JSON like this.

ok: [server1.example.com] => {
    "items": {
        "changed": false,
        "examined": 1,
        "failed": false,
        "files": [
            {
              "atime": 1583384414.8106842,
              "mtime": 1583388631.5600421,
              "dev": 64768,
              "gid": 0,
              "inode": 131881,
              "isblk": false,
              "ischr": false,
              "isdir": false,
              "isfifo": false,
              "isgid": false,
              "islnk": false,
              "isreg": true,
              "issock": false,
              "isuid": false,
              "mode": "0664",
              "mtime": 158338361.5600421,
              "nlink": 1,
              "path": "/tmp/foo.txt",
              "rgrp": true,
              "roth": true,
              "rusr": true,
              "size": 8,
              "uid": 0,
              "wgrp": false,
              "woth": false,
              "wusr": true,
              "xgrp": false,
              "xoth": false,
              "xusr": false
           },
            {
              "atime": 1583384414.8106842,
              "mtime": 1583388631.5600421,
              "dev": 64768,
              "gid": 0,
              "inode": 131881,
              "isblk": false,
              "ischr": false,
              "isdir": false,
              "isfifo": false,
              "isgid": false,
              "islnk": false,
              "isreg": true,
              "issock": false,
              "isuid": false,
              "mode": "0664",
              "mtime": 158338361.5600421,
              "nlink": 1,
              "path": "/tmp/bar.txt",
              "rgrp": true,
              "roth": true,
              "rusr": true,
              "size": 8,
              "uid": 0,
              "wgrp": false,
              "woth": false,
              "wusr": true,
              "xgrp": false,
              "xoth": false,
              "xusr": false
           }
        ],
        "matched": 2,
        "msg": ""
    }
}

 

Here is how you could loop over the "files" array.

---
- hosts: localhost
  tasks:
  - find:
      paths: /tmp
    register: out

  - debug:
      var: items
    vars:
      items: "{{ out.files }}"
...

 

Here is how you could create an array that has the path to each file in the /tmp directory.

---
- hosts: localhost
  tasks:
  - find:
      paths: /tmp
    register: out

  - debug:
      var: files_array
    vars:
      files_array: "{{ out.files | map(attribute='path') | list }}"
...

 

Which should return something like this.

ok: [localhost] => {
    "files_array": [
        "/tmp/foo.txt",
        "/tmp/bar.txt"
    ]
}

 




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 3fa678 in the box below so that we can be sure you are a human.