Ansible

How to use Ansible with_item

The Ansible items plugin is a lookup plugin used to return the list of items provided to it with recursing. A lookup plugin allows you to access the data from outside resources. Data provided in the by lookup plugins are accessed via Jinja2 templates allowing you to load variables, templates, and more. This guide takes a look at the with_items plugin.

What is Ansible with_items?

The Ansible with_items is a handy plugin to perform loop operations in a playbook. The plugin accepts items and then passes them to the calling module. For example, you can pass a list of packages to install and then give each item in the list to the install task.

NOTE: If an item has a nested list, Ansible will flatten it but not recursion.

To use the with_items plugins, use the with_items keyword in a playbook and pass a list of items under it. You can then call each item within the specified list and perform the required operations.

Basic Usage

The following example illustrates the syntax for the with_items plugin:

---
- hosts: all
  name: with items syntax
  debug:
    msg: "This is item {{item}}"
  with_items:
  - "a"
  - "b"
  - "c"

The above example shows a simple list using the with_items plugin.

If you want to have a nested list, you can do:

---
- hosts: all
  name: nested list
  debug:
    msg: "Databases {{item}}"
  with_items:
  - ["MySQL", "PostgreSQL"] - "SQL"
  - ["MongoDB", "DocumentDB", "Firestore"] - "NoSQL"

You can also have a list with variables in it, as shown in the example playbook below:

---
- hosts: all
  name: nested list
  var1: "{{item.var1}}"
  var2: "{{item.var2}}"
  with_items:
  - {var1: example1, var2: example2}
  - {var1: example3, var2: example4}

Ansible with_items Examples

The following examples illustrate how you can use the Ansible with_items module.

---
- hosts: all
  become: true
  gather_facts: no
  tasks:
    - name: create users
      user:
        name: "{{item}}"
        groups: test
        state: present
      with_items:
        - user1
        - user2

The playbook above uses with_items to create multiple users.

---
- hosts: all
  become: true
  gather_facts: no
  tasks:
    - name: install packages
      apt:
        name: "{{item}}"
        state: present
      with_items:
        - apache2
        - ufw
        - mysql

In the example above, we use the with_items to loop over items and pass them to the apt package installer.

---
- hosts: all
  become: true
  gather_facts: no
  tasks:
    - name: create files
      file:
        path: "~/{{item.name}}"
        state: touch
        mode: "{{item.perm}}"
      with_items:
      - {name: file1, perm: "0777"}
      - {name: file2, perm: "0644"}

In the example playbook above, we create a list of files and their respective permissions using the with_items plugin.

Conclusion

This guide discussed how the Ansible with_items plugin works and how to loop over a list of items.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list