In this lab, you will deepen your understanding of using advanced conditionals in Ansible playbooks. You will learn to combine multiple conditions with logical operators (and
, or
, not
) and use the | bool
filter to evaluate human-readable variables like "yes" or "no."
- Learn how to use advanced conditionals in tasks.
- Combine multiple conditions with logical operators.
- Use the
| bool
filter for evaluating variables.
- 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
advanced_conditional_playbook.yml
. -
Define the play's name, target hosts (
all
in this case), and whether to gather facts:--- - name: A conditional play with advanced logic hosts: all gather_facts: no
Explanation:
hosts: all
targets all hosts in the inventory.gather_facts: no
skips automatic fact gathering for simplicity.
-
In the
vars
section, define the following variables:vars: epic: true boomer: no distro: "Ubuntu"
Explanation:
epic
is a boolean variable.boomer
is a human-readable string evaluated as a boolean using the| bool
filter.distro
specifies the operating system.
-
Under the
tasks
section, add the following tasks:-
Task A: Executes if both
epic
is true andboomer
evaluates to true.- name: Task A debug: msg: "Hello A" when: epic and boomer | bool
-
Task B: Executes if
epic
is false andboomer
evaluates to false.- name: Task B debug: msg: "Hello B" when: - not epic - not boomer | bool
-
Task C: Executes if either
epic
is true or thedistro
is "Ubuntu."- name: Task C debug: msg: "Hello C" when: epic or distro == "Ubuntu"
-
Task D: Executes if
epic
is true and eitherboomer
evaluates to true or thedistro
is "Ubuntu."- name: Task D debug: msg: "Hello D" when: - epic - boomer | bool or distro == "Ubuntu"
-
-
Save the
advanced_conditional_playbook.yml
file. -
Run the playbook using the following command:
ansible-playbook advanced_conditional_playbook.yml
-
Observe the output. Each task will execute based on the specified conditions.
In this lab, you've learned how to use advanced conditionals in Ansible playbooks by combining logical operators and boolean filters. These techniques allow you to create dynamic, flexible playbooks that respond intelligently to variable values and system states. Experiment with additional conditions to further refine your skills! 👏