Ansible Playbook validations
Ansible Playbook validations :
While writing ansible playbook sometime we need to validate extra vars parameter or tasks. So that we can run ansible playbook without any fatal error or warning.
These are few condition you might validate
- If specific extra vars parameter are passed then execute some task.
- extra vars parameter having specific value.
- It may possible on some ansible task we want to check previous task result. Bases on previous task output value execute next task.
and many more cases…
In ansible playbook we can debug playbook error by using -v (verbose) in playbook command. If you want to display your own error message instead of ansible fatal error or warning. So here is the ansible playbook validation condition .
Ansible Playbook validations
1) Ansible playbook extra vars parameter validation
# 1 Validate if extra params are not passed or blank
- name: validate extra vars parameter
fail: msg="Param1 is not passed or blank"
when: param1 is not defined or ( param1 is defined and param1 == "" )
2) Ansible playbook extra-vars for conditional task
# 2 register a variable when param1== install
- name: validate task if param1 value is install
command: echo 'TechieRoop'
register: installTrue
when: param1 is defined and param1 == 'install'
# 3 register a variable when param1 == remove
- name: validate task if param1 value is remove
command: echo 'param1 value is {{ param1 }}'
register: removeTrue
when: param1 is defined and param1 == 'remove'
# 4. If install is passed in extra vars param then execute this task.
# You can execute this task in #2 itself. this for demo purpose how you can register
# a variable and execute some based on varaible.
- name: Execute task if param1 == 'install'
command: echo 'Install task !!'
when: installTrue | changed
# 5. If remove is passed in extra vars param then execute this task.
# You can execute this task in #3 itself. this for demo purpose how you can register
# a variable and execute some based on varaible.
- name: Execute task if param1 == 'remove'
command: echo 'Remove task !!'
when: removeTrue | changed
3) Ansible playbook based on output value.
# 6. Execute task based on output of some task
# This task execute when task #2 has output value 'TechieRoop'
- name: validate task if param1 value is install
command: echo "This task run if task 2 has value TechieRoop in output"
when: installTrue.stdout != '' and installTrue.stdout == 'TechieRoop'
Playbook code look like this:-
playbook.yml
- hosts: local
user: roop
gather_facts: no
connection: local
tasks:
# 1 Validate if extra params are not passed or blank
- name: validate extra vars parameter
fail: msg="Param1 is not passed or blank"
when: param1 is not defined or ( param1 is defined and param1 == "" )
# 2 register a variable when param1== install
- name: validate task if param1 value is install
command: echo 'TechieRoop'
register: installTrue
when: param1 is defined and param1 == 'install'
# 3 register a variable when param1 == remove
- name: validate task if param1 value is remove
command: echo 'param1 value is {{ param1 }}'
register: removeTrue
when: param1 is defined and param1 == 'remove'
# 4. If install is passed in extra vars param then execute this task.
# You can execute this task in #2 itself. this for demo purpose how you can register
# a variable and execute some based on varaible.
- name: Execute task if param1 == 'install'
command: echo 'Install task !!'
when: installTrue | changed
# 5. If remove is passed in extra vars param then execute this task.
# You can execute this task in #3 itself. this for demo purpose how you can register
# a variable and execute some based on varaible.
- name: Execute task if param1 == 'remove'
command: echo 'Remove task !!'
when: removeTrue | changed
# 6. Execute task based on output of some task
# This task execute when task #2 has output value 'TechieRoop'
- name: validate task if param1 value is install
command: echo "This task run if task 2 has value TechieRoop in output"
when: installTrue.stdout != '' and installTrue.stdout == 'TechieRoo
Ansible playbook commands:-
1) ansible-playbook playbook.yml
or
ansible-playbook playbook.yml -e “param1=”
It will abort extenuation and displayed error message
PLAY [local] ******************************************************************
TASK: [1.validate extra vars parameter] *****************************************
failed: [127.0.0.1] => {"failed": true}
msg: Param1 is not passed or blank
DEBUG-ENDS
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/home/prod/roopendra.retry
2) ansible-playbook playbook.yml -e “param1=install”
PLAY [local] ******************************************************************
TASK: [1.validate extra vars parameter] ***************************************
skipping: [127.0.0.1]
TASK: [2.validate task if param1 value is install] ****************************
changed: [127.0.0.1]
TASK: [3.validate task if param1 value is remove] *****************************
skipping: [127.0.0.1]
TASK: [4.Execute task if param1 == 'install'] *********************************
changed: [127.0.0.1]
TASK: [5.Execute task if param1 == 'remove'] **********************************
skipping: [127.0.0.1]
TASK: [6.validate task if param1 value is install] ****************************
changed: [127.0.0.1]
PLAY RECAP ********************************************************************
127.0.0.1 : ok=3 changed=3 unreachable=0 failed=0
3) ansible-playbook playbook.yml -e “param1=remove”
PLAY [local] ******************************************************************
TASK: [1.validate extra vars parameter] ***************************************
skipping: [127.0.0.1]
TASK: [2.validate task if param1 value is install] ****************************
skipping: [127.0.0.1]
TASK: [3.validate task if param1 value is remove] *****************************
changed: [127.0.0.1]
TASK: [4.Execute task if param1 == 'install'] *********************************
skipping: [127.0.0.1]
TASK: [5.Execute task if param1 == 'remove'] **********************************
changed: [127.0.0.1]
TASK: [6.validate task if param1 value is install] ****************************
skipping: [127.0.0.1]
PLAY RECAP ********************************************************************
127.0.0.1 : ok=2 changed=2 unreachable=0 failed=0
(Visited 1,974 times, 51 visits today)
Nice article. My two cents here:
1. {{ “param1 is not defined or ( param1 is defined and param1 == “” ) }} can be changed to {{ param1 is not defined or param1 == “” }}
2. As long as your first task will fail the play if param1 is undefined or empty, you don’t need to check param1 is defined in any subsequent conditions