Tests in Ansible
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed functions (filters) in Ansible.
https://cloudaffaire.com/functions-in-ansible/
In this blog post, we will discuss tests in Ansible.
Tests in Ansible:
In the last blog post, we have discussed filters in Ansible. You can extend the functionality of filters by using tests. Tests in Jinja are a way of evaluating template expressions and returning True or False. Jinja2 includes a set of tests and in addition to those Jinja2 tests, Ansible supplies a few more and users can easily create their own.
The main difference between tests and filters are that Jinja tests are used for comparisons, whereas filters are used for data manipulation, and have different applications in jinja. Tests can also be used in list processing filters, like map() and select() to choose items in the list.
Note: Like all templating, tests always execute on the Ansible controller, not on the target of a task, as they test local data.
Next, we are going to explain each test types with a demo.
Tests for variables:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
####################### ## Testing variables ## ####################### ## Create playbook vi myplaybook.yml ---------------------- - hosts: localhost gather_facts: false vars: str1: "cloudaffaire" tasks: - debug: msg: "variable 'str1' is defined with value = {{ str1 }}" when: str1 is defined #can be used with any type of variables - debug: msg: "variable 'str2' is undefined" when: str2 is undefined #can be used with any type of variables ---------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml |
Tests for strings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
##################### ## Testing strings ## ##################### ## Create playbook vi myplaybook.yml ---------------------- - hosts: localhost gather_facts: false vars: str1: "welcome to cloudaffaire" str2: "https://cloudaffaire.com/category/devops/ansible/" str3: "CLOUDAFFAIRE" tasks: - debug: msg: "{{ str1 }} contains 'cloudaffaire'" when: str1 is match(".*cloudaffaire") - debug: msg: "{{ str2 }} contains 'ansible'" when: str2 is search(".*ansible.*") - debug: msg: "{{ str2 }} contains 'devops'" when: str2 is regex("\w+/devops") - debug: msg: "{{ str1 | quote }} is in lowercase" when: str1 is lower - debug: msg: "{{ str3 | quote }} is in Uppercase" when: str3 is upper - debug: msg: "variable str3 is of string type with value = {{ str3 }}" when: str3 is string ---------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml |
Tests for numbers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
##################### ## Testing numbers ## ##################### # Upgrade Jinja2 sudo pip install --upgrade Jinja2 ## Create playbook vi myplaybook.yml ---------------------- - hosts: localhost gather_facts: false vars: num1: 15 num2: 30 tasks: - debug: msg: "num1:{{ num1 }} is divisible by 5" when: num1 is divisibleby 5 - debug: msg: "num1:{{ num1 }} is equal to 15" when: num1 is eq 15 - debug: msg: "num1:{{ num1 }} is not equal to {{ num2 }}" when: num1 is ne num2 - debug: msg: "num1:{{ num1 }} is grater than 10" when: num1 is gt 10 - debug: msg: "num1:{{ num1 }} is grater than equal to 15" when: num1 is ge 15 - debug: msg: "num1:{{ num1 }} is less than 45" when: num1 is lt 45 - debug: msg: "num1:{{ num1 }} is less than equal to 15" when: num1 is le 15 - debug: msg: "num1:{{ num1 }} is a odd number" when: num1 is odd - debug: msg: "num2:{{ num2 }} is a even number" when: num2 is even - debug: msg: "variable num2 is of number type with value = {{ num2 }}" when: num2 is number ---------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml |
Tests for sequences:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
####################### ## Testing sequences ## ####################### ## Create playbook vi myplaybook.yml ---------------------- - hosts: localhost gather_facts: false vars: lst1: [0,1,2,3,4,5,6,7,8,9] lst2: [2,4,6,8] tasks: - debug: msg: "{{ lst1 }} includes {{ lst2 }}" when: lst1 is superset(lst2) - debug: msg: "{{ lst2 }} is included in {{ lst1 }}" when: lst2 is subset(lst1) - debug: msg: "{{ lst1 }} includes 8" when: 8 is in(lst1) - debug: msg: "variable lst2 is of sequence type with value = {{ lst2 }}" when: lst2 is sequence ---------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml |
Tests for paths:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
################### ## Testing paths ## ################### ## Create a dir, file, link cd && mkdir mydir mymountdir mymount touch myfile && ln -s myfile mylink ## Create playbook vi myplaybook.yml ---------------------- - hosts: localhost gather_facts: false vars: pth1: "/home/debjeet/mydir" pth2: "/home/debjeet/myfile" pth3: "/home/debjeet/mylink" pth4: "/home/debjeet/myfile" tasks: - debug: msg: "{{ pth1 }} is a directory" when: pth1 is directory - debug: msg: "{{ pth2 }} is a file" when: pth2 is file - debug: msg: "{{ pth3 }} is a link" when: pth3 is link - debug: msg: "path {{ pth1 }} exists" when: pth1 is exists - debug: msg: "file {{ pth2 }} is same as file {{ pth4 }}" when: pth2 is same_file(pth4) ---------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml |
Tests for tasks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
################### ## Testing tasks ## ################### ## Create playbook vi myplaybook.yml ---------------------- - hosts: localhost gather_facts: false tasks: - shell: echo hello register: result2 - debug: msg: "task execution successfull" when: result2 is succeeded - debug: msg: "task execution successfull" when: result2 is success - debug: msg: "task execution changed" when: result2 is changed - shell: echo /usr/bin/foo.txt register: result1 ignore_errors: True - debug: msg: "task execution failed" when: result1 is failed - debug: msg: "task execution skipped" when: result1 is skipped ---------------------- :wq ## Execute the playbook ansible-playbook myplaybook.yml |
Hope you have enjoyed this article. We have tried to cover as many tests as possible. But still, if few are left, you can reference below documents
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html
https://jinja.palletsprojects.com/en/2.10.x/templates/#builtin-tests
In the next blog post, we will discuss encryption (Ansible Vault) in Ansible.
To get more details on Ansible, please refer below Ansible documentation.