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.

parameter are passed then execute some task. extra vars parameter having specific value.

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

Ansible Playbook validations http://techieroop.com/ansible-playbook-validations/ http://techieroop.com/wp-content/uploads/2014/07/Ansible-Playbook-Validation.png http://techieroop.com/wp-content/uploads/2014/07/Ansible-Playbook-Validation-150x120.png Ansible DevOPS Ansible,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 validateIf specific extra vars parameter are passed then execute some task. extra vars parameter having... <h2>Ansible Playbook validations :</h2> While writing ansible playbook sometime we need to validate <strong>extra vars</strong> parameter or tasks. So that we can run ansible playbook without any fatal error or warning.<strong>These are few condition you might validate</strong> <ul> <li>If specific<strong> extra vars</strong> parameter are passed then execute some task.</li> <li><strong>extra vars</strong> parameter having specific value.</li> <li>It may possible on some ansible task we want to check previous task result. Bases on previous task output value execute next task.</li> </ul> and many more cases...In ansible playbook we can debug playbook error by using<strong> -v</strong> (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 . <h3>Ansible Playbook validations</h3> <h4>1) Ansible playbook extra vars parameter validation</h4> <pre><code> # 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 == "" )</code></pre> <h4>2) Ansible playbook extra-vars for conditional task</h4> <pre><code> # 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</code></pre> <h4>3) Ansible playbook based on output value.</h4> <pre><code> # 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' </code></pre> <h3>Playbook code look like this:-</h3> <pre><code><strong>playbook.yml</strong> - 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 </code></pre> <h3>Ansible playbook commands:-</h3> <strong>1) ansible-playbook playbook.yml</strong>or<strong>ansible-playbook <strong>playbook</strong>.yml -e "param1="</strong>It will abort extenuation and displayed error message <pre><code> PLAY [local] ******************************************************************TASK: [1.validate extra vars parameter] ***************************************** failed: [127.0.0.1] => {"failed": true} msg: Param1 is not passed or blank DEBUG-ENDSFATAL: all hosts have already failed -- abortingPLAY RECAP ******************************************************************** to retry, use: --limit @/home/prod/roopendra.retry </code></pre> <strong>2) ansible-playbook <strong>playbook</strong>.yml -e "param1=install"</strong> <pre><code> 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 </code></pre> <strong>3) ansible-playbook <strong>playbook</strong>.yml -e "param1=remove"</strong> <pre><code> 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 </code></pre> Roopendra https://plus.google.com/+Techieroopendra Roopendra Vishwakarma roopendramca@gmail.com Administrator I'm Roopendra Vishwakarma, a DevOps Engineer and Blogger from India. I have experience in DevOps, Web Development and various Open Source Technology. I mostly write about latest technology, getting started tutorial and tricks and tips. TechieRoop 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.

parameter are passed then execute some task. extra vars parameter having specific value.

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.ymloransible-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-ENDSFATAL: all hosts have already failed -- abortingPLAY 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 7,907 times, 106 visits today)