The purpose of this post is to give you a real-time example and explanation of how ansible variable is defined and not defined (undefined) conditionals are working along with "when" conditional statement and how it controls the flow of the task and the play. The following ansible playbook is created strategically to explain the validation cases of ansible playbook variable is defined or not defined (undefined) along with "when" conditional module
Note*: VLOG of this post is now available at our Youtube channel. you can find the video at end of this post or go to our channel
Playbook to Test When Variable is defined
We have created a simple playbook to demonstrate this by example, This ansible-playbook is designed to find the files in a specific directory based on the inputs being passed ( startup arguments) .
In our case, The Startup arguments are the
--extra-vars (extra variables) being passed while the ansible playbook is being invoked. Just for convenience and better understanding, we are going to use the short form of
--extra-vars which is
Note*: --extra-vars (or) -e is a easiest command line based method and one of the way to pass values (or) set variables in the playbook. just like declaring vars/main.yml file or defining under – vars inside playbook.
As mentioned above, the ansible playbook is having a simple find command, to find files in a certain directory, read from the user at startup. But here is the trick.
When the user defines ( pass it as command line argument with -e) Just the
Search String it brings up all the files in the directory matching the search string.
When the user defines
Search String as well as the
Modified time it brings the matching files which are meeting the age criteria as well.
In the second snippet, you can find there is an extra command-line argument (or) extra variable
-e "mtime=30" which is a modified time flag. ( typical mtime in find command). It supposed to get 30 days old files for us
Note*: you can also use +30 and -1 like you typically do in the Unix find command. with this playbook.
Consider the following playbook. The Single Playbook would react and perform two different operations based on the availability of variables. the tasks would be executed when certain variables are defined or not defined.
This is achieved through defined and undefined. The correct task would be executed based on the variables defined or provided.
How, Ansible Defined and Not Defined works
Here, if you look at the Case1, the condition is to make sure that all the variables are defined and having values.
where we define, Execute the case1 task when the Directory, Search String and Modified time all three variables are defined.
we are validating if three variables are defined before executing this task. If anyone of these variables is not defined this task would not be executed.
On the other hand, Case2 Is more similar, with a tiny difference.
Here we are using
not defined and checking only if two values are defined and one is undefined or not defined, then this task would be executed.
this is how we are achieving the controlled execution of a task based on the variable's availability and declaration.
In the preceding snippets, I have highlighted the text which is different from one another.
Realtime Testing of this playbook
I have two linux machines grouped into the host group named
appservers and am going to create a new directory with a few empty files.
Note*: I preferred to do all these steps with ansible ad-hoc commands. hope you would find it helpful too
Let me introduce you the boxes.
Yes those are my box names
Step1: Create a Directory and Files
Step2: Make sure the files are created
Step3: Now Execute our Playbook
Without modified time
With modified time
The Execution Output - Terminal Recording
The VLOG of this post
Hope the examples on how to use ansible when
Please leave your rating here [ratings]
Let me know your valuable comments and feel free to write it up in case of any feedback or questions
Signup for Exclusive "Subscriber-only" Content