From the course: Red Hat Certified Engineer (EX294) Cert Prep: 2 Using Ansible Playbooks

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Looping and conditionals

Looping and conditionals

From the course: Red Hat Certified Engineer (EX294) Cert Prep: 2 Using Ansible Playbooks

Start my 1-month free trial

Looping and conditionals

- [Instructor] As with most languages, you can combine loops and conditionals. In this simple example, we're looping through a static list of even numbers and for each iteration, the number is automatically placed in the item variable. The condition is set using the when clause. When the value of the item variable is greater than five, the debug module will print the contents of the item variable and a message. I've created a runnable version of the script in this chapter's exercise files directory named loop-list-conditional.yml. You can execute it by changing into that directory and using Ansible playbook. We can combine looping through a dictionary with a condition as well. First in a variable section, we define and add values to a dictionary named mydict. For keys, we have the words one, two, and three, and for values, we have the numbers 1, 2, and 3. When we loop through the contents of the dictionary, then we loop…

Contents