Difficulty: Beginner
Estimated Time: 15-20 minutes

In this Lab, we will be exploring conditionals and loops in Ansible.


Often the result of a play may depend on the value of a variable, fact (something learned about the remote system), or previous task result.


Often you’ll want to do many things in one task, such as create a lot of users, install a lot of packages, or repeat a polling step until a certain result is reached.

Ansible Conditionals and Loops

Step 1 of 5

Conditionals Exercise 1

The given playbook attempts to start mysql service on all_servers.

  name: Execute a script on all web server nodes
  hosts: all_servers
      service: name=mysql state=started


Use the when condition to run this task if the host (ansible_host) is the database server. Refer to the inventory file to identify the name of the database server cat inventory.txt

Note: You can get the answer for this exercise by clicking Show Solution.