In this lab, you will learn how to use the with_items
directive in Ansible to iterate over a list of items and execute tasks for each item. By the end, you'll be familiar with iterating over a list in a playbook and displaying a customized message for each list item.
- Use the
with_items
directive to iterate over lists. - Execute tasks dynamically for each item in a list.
- Basic knowledge of Ansible playbooks.
- Ansible installed on your ansible machine.
- Access to a target host categorized as "webservers."
Estimated Time: 20 minutes
-
Create a new YAML file named
item_iteration.yml
. -
Define the play's name, target hosts, and enable privilege escalation:
--- - name: Iterate over a list of items hosts: webservers become: yes
-
Under the
tasks
section, add a task to iterate over a list of names and print a custom message for each:tasks: - name: Print a message for each item debug: msg: "Hello, {{ item }}" with_items: - John - Jane - Alex - Sarah
Explanation:
with_items
: Iterates over the provided list.{{ item }}
: Refers to the current item in the iteration.
-
Save the
item_iteration.yml
file. -
Run the playbook using the following command:
ansible-playbook item_iteration.yml
-
Observe the output. The playbook will print a custom message for each name in the list.
You can compare your playbook with the item_iteration.yml file in the current directory.
In this lab, you learned how to use the with_items
directive in Ansible to iterate over lists and execute tasks multiple times with different data. This technique is valuable for automating repetitive operations and making playbooks more dynamic and efficient. Practice applying list iteration to various tasks in your Ansible playbooks to enhance your automation capabilities! 👏