Difficulty: Beginner
Estimated Time: 15-20 minutes

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

Conditionals

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.

Loops

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.

Don’t stop now! The next scenario will only take about 10 minutes to complete.

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
  tasks:
    -
      service: name=mysql state=started

Task

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.

playbook.yml