Report this

What is the reason for this report?

How To Use Loops in Ansible Playbooks

Published on April 15, 2021
Erika Heidi

By Erika Heidi

Developer Advocate

How To Use Loops in Ansible Playbooks

When automating server setup, sometimes you’ll need to repeat the execution of the same task using different values. For instance, you may need to change permissions of multiple files, or create multiple users. To avoid repeating the task several times in your playbook file, it’s better to use loops instead.

In programming, a loop allows you to repeat instructions, typically until a certain condition is met. Ansible offers different looping methods, with the loop keyword being the most recommended option for longer term compatibility.

The following example creates three different files on the /tmp location. It uses the file module within a task that implements a loop using three different values.

Create a new file called playbook-06.yml in your ansible-practice directory:

  1. nano ~/ansible-practice/playbook-06.yml

Then add the following lines to the new playbook file:

~/ansible-practice/playbook-06.yml
---
- hosts: all
  tasks:
    - name: creates users files
      file:
        path: /tmp/ansible-{{ item }}
        state: touch
      loop:
        - sammy
        - erika
        - brian

Save and close the file when you’re done.

Then, run ansible-playbook with the same connection arguments from the previous examples. Again, we’re using an inventory file named inventory and a user named sammy, but you should change these values accordingly:

  1. ansible-playbook -i inventory playbook-06.yml -u sammy

You’ll get output like this, showing each individual item value that was used within the loop:

Output
... TASK [creates users files] ****************************************************************************** changed: [203.0.113.10] => (item=sammy) changed: [203.0.113.10] => (item=erika) changed: [203.0.113.10] => (item=brian) ...

For more detailed information on how to use loops when writing Ansible playbooks, please refer to the official documentation.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

Tutorial Series: How To Write Ansible Playbooks

Ansible is a modern configuration management tool that doesn’t require the use of an agent software on remote nodes, using only SSH and Python to communicate and execute commands on managed servers. This series will walk you through the main Ansible features that you can use to write playbooks for server automation. At the end, we’ll see a practical example of how to create a playbook to automate setting up a remote Nginx web server and deploy a static HTML website to it.

About the author

Erika Heidi
Erika Heidi
Author
Developer Advocate
See author profile

Dev/Ops passionate about open source, PHP, and Linux. Former Senior Technical Writer at DigitalOcean. Areas of expertise include LAMP Stack, Ubuntu, Debian 11, Linux, Ansible, and more.

Still looking for an answer?

Was this helpful?


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.