Ansible playbook to illustrate how "when" conditional statement works in Ansible.
Ansible playbook below will run a command: "file -s /dev/sdb"
The output of the command will be saved to the variable: save_the_command_output
Yes, the name of the variable can be anything. Just changed it to something that is sensible.
save_the_command_output is just a string to illustrate its functionality or usage.
After registering or saving the output to a variable, another command will be executed provided the conditional statement "when" is True or the condition is meet or satified.
If the conditional statement is not meet then the command will be bypass or will not be executed.
On this illustration below, if the conditional statement has been meet.
Then a file with a filename "condition_ok" will be written on /tmp directory.
Otherwise, if the condition is not meet then no file will be created.
Sample playbook below:
#===================
---
- hosts: localhost
tasks:
- name: Run a command
command: "file -s /dev/sdb"
name: Save the output of the command to a variable
register: save_the_command_output
- name: Write condition_ok only when the search string can be found or present on the above variable or output
command: touch "/tmp/condition_ok"
name: Below will only be executed when the word data is found on the variable
when: save_the_command_output.stdout | regex_search('data')
#===============
The regex_search keyword will find or search the string "data".
The search string "data" is just an example and can be replaced with string you need to expect that will be part of the variable or output in order to consider the conditional statement as "True" or satisifed.
Enjoy the world of Ansible!!! Use the technology to make your world easier!!!
Stay safe! and Keep things up!
Do ASAP, Always Say A Prayer...
Practice O.T.G. = Obedience To God
Make time for Prayer and Meditation.
Take time to kneel down and Pray!
================================
Free Android Apps:
Click links below to find out more:
Free Android Apps:
Click links below to find out more:
Excel Keyboard guide:
https://play.google.com/store/apps/details?id=chrisjoms.myexcelapplicationguide
Heaven's Dew Fall Prayer app for Android :
https://play.google.com/store/apps/details?id=soulrefresh.beautiful.prayer
Catholic Rosary Guide for Android:
Pray the Rosary every day, countless blessings will be showered upon your life if you recite the Rosary faithfully.
https://play.google.com/store/apps/details?id=com.myrosaryapp
Divine Mercy Chaplet Guide (A Powerful prayer) BFF = Be Filled Faith:
Comments
Post a Comment