In this lab, you will learn how to use conditionals in an Ansible playbook. Specifically, you will understand how the when
clause can be used to control the execution of tasks based on the value of a variable.
- Learn how to use the
when
clause to implement conditionals. - Create a task that executes only under specific conditions.
- Basic understanding of Ansible playbooks.
- Ansible installed on your ansible machine.
- Access to target hosts in your inventory.
Estimated Time: 20 minutes
-
Create a new YAML file named
conditional_playbook.yml
. -
Define the play's name, target hosts (
all
in this case), and whether to gather facts:--- - name: A conditional play hosts: all gather_facts: no
Explanation:
hosts: all
targets all hosts in the inventory.gather_facts: no
skips automatic fact gathering to keep the playbook simple.
-
In the
vars
section, define a variable namedepic
and assign it a boolean value:vars: epic: true
Explanation:
- The variable
epic
will be used to control the execution of tasks.
- The variable
-
Under the
tasks
section, add a task that prints a message "Hello A" if the variableepic
istrue
:tasks: - name: Task A debug: msg: "Hello A" when: epic
Explanation:
- The
when
clause ensures the task runs only when the condition (epic: true
) is met.
- The
-
Save the
conditional_playbook.yml
file. -
Run the playbook using the following command:
ansible-playbook conditional_playbook.yml
-
Observe the output. The task "Task A" will execute only if
epic
is set totrue
.
In this lab, you've learned how to use conditionals in an Ansible playbook by leveraging the when
clause. This powerful feature allows you to control task execution based on specific conditions, making your playbooks more dynamic and adaptive. Keep practicing with different conditions to enhance your automation skills! 👏