Ansible Conditional Statements
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
There are no resources for this lesson.
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
In this lesson, you will learn how to create and run a new Ansible playbook that will demonstrate the use of boolean variables and conditional statements.
By the end of this lesson, you'll understand how to control the execution of tasks based on the value of a boolean variable.
Creating the Playbook with Boolean Variable
Start by creating a new playbook file named conditional_test.yml
.
nano conditional_test.yml
In this playbook, we will target all hosts and define a boolean variable create_file
at the top, which will control whether a file should be created or removed.
---
- hosts: all
vars_files:
- secret.yml
vars:
create_file: True
tasks:
Let's add a debug message that will show the value of our variable create_file
:
- name: Should the file exist?
debug:
msg: "File should exist: {{ create_file }}"
Next, add a task to create the file conditional_test_file.txt
in the home directory when create_file
is true:
- name: Create the file
file:
path: ~/conditional_test_file.txt
state: touch
when: create_file == True
This task uses the file
module to create a file, and it executes when create_file
is true (yes
). The state: touch
parameter ensures that the file is created if it doesn't exist.
Now, let's add a task to remove the file if create_file
is false:
- name: Remove the file
file:
path: ~/conditional_test_file.txt
state: absent
when: not create_file == True
This task also uses the file
module but with state: absent
to remove the file if it exists. It executes when create_file
is false (no
).
Running the Playbook
With the playbook ready, you can execute it to see the conditional logic in action. To test both conditions, first run the playbook with create_file
set to yes
, and then change it to no
and run it again.
Use this command to run the playbook:
ansible-playbook conditional_test.yml
Now you should see the debug messages stating True
for the variable value, the task for creating the file should be changed, and the task for removing the file should be skipped:
Verifying the Results
After running the playbook with create_file
set to yes
, check that the file was created on the managed nodes:
ansible all -m command -a "ls ~/conditional_test_file.txt" -e @secret.yml
When you set create_file
to False
and rerun the playbook, use the same command to verify that the file has been removed. Remember that we can supply command line (called extra) variables using the -e
argument. You can use this to set create_file to yes or no.
See the JSON example below {create_file: no}
:
ansible-playbook conditional_test.yml -e '{create_file: False}'
This time, the create file is skipped
Notice that I am supplying the extra variables in JSON format. This is because I am passing a boolean value. If I specify -e "create_file = True"
, the True
will be passed in string format, not as a boolean, since the variable type boolean True is not the same value as the string type "True". This breaks our conditional logic since it expects a boolean value for that variable, not a string value.
If you try this, you'll notice that the file is still deleted if you attempt to set the value like so (this passes a string value):
ansible-playbook conditional_test.yml -e "create_file=True"
But, if you pass your extra args in JSON format, it will parse the variable type of True correctly as a boolean:
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.