Conditionals are one of the fundamental parts of any programming languages so as to control the flow of execution. Ansible also provides a method for this using the ‘when’ clause. The basic ‘when’ operation is very easy. You just have to declare the condition against the when clause like below.
when: ‘some condition’
You can use this with any other modules to control when they are executed.
Let us see how we can use the when clause in multiple situations.
1. Ansible When file exists
We can check the details of a file with the help of Ansible stat module. What the stat module does is it checks for the ‘path’ given in the task using Linux stat command. After the check is done on the remote server, it returns some values to you. Yo can make use of these return values to control the execution flow.
In the following task, I am using the stat command to check for a sample.txt on the remote servers. Stat has a return value called ‘exists’, which is a boolean value. If the file exists it returns true else it returns false. You can store the result of the stat operation in a register and can work with it.
- hosts: loc tasks: - stat: path: /home/mdtutorials2/sample_file.txt register: result - debug: msg: "Ansible When File Exists Example." when: result.stat.exists
2. Ansible When file does not exist
You can also check if the file does not exist in a similar manner. You can use the ‘== false’ against when the parameter to achieve this.
- hosts: loc tasks: - stat: path: /home/mdtutorials2/sample.txt register: result - debug: msg: "Ansible When File Not Present Example." when: result.stat.exists == false
3. Create a file using Ansible when the file does not exist.
If your intention is to create a new file if it is not present on the remote servers, then you should use the file module. You can use the ‘state: touch’ to make sure the file is created. If the file already present, then the modification time of the file will be changed though. It works in the same way the Linux touch command works. You can also mention what should be the permission of the file created.
- name: Create a file in Ansible if it does not exist example - file: path: /home/mdutotials/Ansiblefile_example.txt state: touch mode: "u=rw,g=r,o=r" # mode= 0644 also works
4.Ansible when the directory does not exist.
You can use the stat module to check if a directory exists. You will be setting the directory path against the path parameter. We will store the output in a register. You can then check the return values if it is a directory by using ‘isdir’ parameter. In the
In the following task, I am checking the existence for a sample_directory. I capture the return value in a register. In the next task, I use the ‘result.stat.exists’ to make sure the file exists and then the ‘result.stat.isdir’ to check if it is indeed a directory.
- hosts: loc tasks: - stat: path: sample_directory register: result - debug: msg: "Ansible when directory exists example" when: result.stat.exists == true and result.stat.isdir == true
5. Using Ansible when with command module
We can also use the conditional along with the command/shell module to change the flow of execution.
The below example is trying to shut down the system when the date – taken from an ansible fact – is 09.
tasks: - name: "Ansible command when example" command: shutdown when: ansible_date_time.day == "09"
6. Checking multiple conditions using ‘or’
We can combine multiple conditions by using ‘or’ in conjunction with the statements to be checked. If any of the conditions are true the task will execute.
For example, the following task will be executed if the value of test1 is 5 or 6.
- hosts: all vars: test1: 8 tasks: - debug: msg: "Ansible when or example" when: test1==5 or test1==6
7. Checking multiple conditions using ‘and’
When we are using ‘and’ for checking multiple conditions, only if all the conditions match, will the task execution happen.
For example, the following task will execute only if the value of test2 is divisible by 2 and 3.
- hosts: all vars: test2: 6 tasks: - debug: msg: "Ansible when or example" when: test2%2==0 and test2%3==0
8. Execute a command depending on the Linux version
- hosts: all tasks: - debug: msg: "This is the expected system" when: ansible_distribution == "Ubuntu" and ansible_distribution_version == "16.04"
Following are some of the facts that you can use in such scenarios.
"ansible_distribution": "Ubuntu", "ansible_distribution_file_variety": "Debian", "ansible_distribution_major_version": "16", "ansible_distribution_release": "xenial", "ansible_distribution_version": "16.04",