Ansible Conditional

Ansible Conditional
Techiio-author
Written by Nilima PaulJanuary 24, 2022
10 min read
Ansible
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible Conditional.

Introduction to Ansible Conditional

In Ansible, we generally get and utilize it from remote objective hubs and afterward utilize this information to settle on the choice to execute play sort inquires. Likewise results additionally rely upon the worth of factors passed to plays and assignments, realities got or made, or the outcomes from previous outcomes. Commonly this likewise works in a manner where one variable's worth is subject to another variable. So this multitude of sorts of circumstances, are taken care of by Ansible conditionals. In Ansible, conditionals incorporate when, circle, custom realities, register factors, and usually utilized realities. In this article, we will attempt to cover these.

What is Ansible Conditional?

In Ansible, we have numerous helpful elements boundaries that are not modules but rather can be utilized in errands to determine a condition or pre-imperative to execute this assignment. These are essentially the choices to control the execution of an errand or play. This is a significant perspective as when dealing with numerous remote has, the conditions might change like their Operating frameworks are unique, in such cases, it turns out to be vital that main the errands composed for that specific condition should run on that else it will be disregarded. This makes us not toeak a framework and effectively run our playbooks.

Below is the list of main conditionals which you will end you using in day-to-day operations.

  • when
  • loop and conditionals
  • custom facts
  • register variables
  • conditional imports
  • selecting files and templates based on variables
  • Commonly used facts

We will learn these conditionals in the next section of this article.

How Do Ansible Conditionals Works?

To work with Ansible Conditionals, one should acclimate herself of the watchwords, which Ansible takes as adequate qualities in playbooks. Likewise, the punctuation part of utilizing these conditionals is a significant angle to learn. Here in this segment, we will see more insights regarding the adequate catchphrases and their utilization as conditionals.

1. When

This is used in situations where we need to run a task on a host or multiple hosts which satisfy the given condition or conditions. Also, where the specified condition does not match, the task will be skipped. The conditions given under when can be as simple as checking the operating system version or can be a bit complex where values of a Jinja2 template contained in a variable are checked.

Syntax:

- name: <some task>
command: <command to be executed> when: <condition to check>

Also, when we have more than one condition, then we can specify those like below:

tasks:
name: <some task>
command: <command to be executed> when:
<first condition to check>
<second condition to check>

Note below points as well, while using variables or when you want to use some math operations.

  • We do not need to use {{ }} when specifying variables inside conditions, as these are already implicit.
  • When a mentioned variable is not set, you can pass or fail using Jinja2 defined test.
  • When fetching variables from remote target hosts. Then be cautious on the fact that the data generated on target hosts may be dependent on some package’s availability.

2. Loop and Conditionals

These are required at whatever point we need to run an assignment against a rundown of things. The assignment will run in each host for all recorded things independently. The circle can be utilized with The condition will be checked against everything in the rundown. Test code will resemble underneath:

Code:

tasks:
- command: <some command> loop: [ 1, 2, 3, 4 ] when: item > 3

we can also make use of jinja2 templates while using a dictionary or similar cases.

3. Custom Facts

This is used in cases when you have been fetching some data using a custom module. Based on that fetched data, we set the facts and use them in conditionals.

4. Register Variables

More often than not in playbooks, we store the result esteems into a variable and afterward in playbook access this variable or a piece of it to use in another assignment. Essentially, these enlisted factors can be utilized in layouts, activity lines, and conditionals to take a choice.

The following is test punctuation for this which can be changed over to genuine.

Syntax:

- shell: <some command>register:<var_output>
shell: <some command based on registered variable> when: <condition checking register variable part>

5. Conditional Imports

Though this is not mostly used there are some cases when we need such features. A good example would be when you have different operating systems on remote target nodes. Sample code will be like below:-

Code:

---
hosts: webhost vars_files:
"vars/file_common.yaml"
[ "vars/{{ ansible_facts['os_family'] }}.yaml", "vars/defaults.yaml"] tasks:
- name: making httpd service in started state service: name={{ httpd }} state=started

Here in this example variable httpd is characterized in both of the records imported.

Presently we will attempt to accomplish some down-to-earthwork in this segment, by chipping away at a similar model. Here, in our test lab, we have a regulator hub named ansible-regulator and two remote objective machines named have one and host-two.

Examples of Ansible Conditional

In this example, we have a playbook like below, where we will use some conditions based on registered variables and another condition.

Code:

---
hosts: all tasks:
name: Here we check whether OS is Redhat and print as shell: echo "This is n RHEL based machine"
when: ansible_distribution == "RedHat"
name: Here we check whether OS is Ubuntu and print as shell: echo "This is a Ubuntu based machine"
when: ansible_distribution == "Ubuntu"

Running this playbook will give output like below where we can see that when a condition is true, then accordingly output message is printed. Also, we are using -v to make it give output in verbose mode.

blogpost

Conclusion

As you can see in this article, Ansible conditionals are very useful in situation-based playbooks. So are our production environments mostly. So having sound knowledge of Ansible conditionals make you competent in writing complex playbooks where we have data dependency on many other conditions. So learn it first and then use it.

Ansible Conditional
Ansible
linux
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software91
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration28
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance