

How To Use Loops in Ansible Playbooks
source link: https://www.digitalocean.com/community/tutorials/how-to-use-loops-in-ansible-playbooks
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Tutorial
How To Use Loops in Ansible Playbooks
Configuration ManagementAnsible
-
By Erika Heidi
Published onApril 15, 2021 30 views
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.
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:
nano ~/ansible-practice/playbook-06.yml
Then add the following lines to the new playbook file:
---
- 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:
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.
Recommend
-
84
README.md Ansible Jupyter Kernel
-
33
Ubuntu 16.04 and 18.04 (and likely future versions) often don't have Python 2 installed by default. Sometimes Python 3 is installed, available at /usr/bin/python3 , but for many minimal images I've used,...
-
38
Ansible allows us to automate the configuration management of systems and add any number of clients as we wish. Have you ever wondered how complex this can get? Have you ever wondered how long and confusing the playbooks...
-
56
README.rst ceph-ansible Ansible playbooks for Ceph, the distributed filesystem. Please refer to our hosted documentation here:
-
16
Introduction Sovereign is a set of Ansible playbooks that you can use to build and maintain your own
-
12
Tutorial How To Create and Use Templates in Ansible Playbooks Ubuntu
-
6
Tutorial How To Use Variables in Ansible Playbooks Configuration Management
-
6
Tutorial How To Define and Use Handlers in Ansible Playbooks Ubuntu
-
4
Tutorial How To Use Conditionals in Ansible Playbooks Configuration Management
-
10
Tutorial How To Install and Manage System Packages in Ansible Playbooks Ubuntu
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK