Ansible (Conditionals, Loops and Roles)
In the realm of IT automation, Ansible stands out as a powerful and versatile tool, empowering IT professionals to streamline complex tasks and manage infrastructure with ease. At the heart of Ansible’s capabilities lie three fundamental concepts: conditionals, loops, and roles. These building blocks work in harmony to enable granular control over automation processes, ensuring that tasks are executed only when necessary, repeated efficiently, and organized in a structured manner.
Ansible Conditionals:
Conditionals in Ansible allow you to execute tasks based on certain conditions. Ansible supports a variety of conditionals, including:
when
: Thewhen
keyword is commonly used for conditional execution. It allows a task to be executed only if a specified condition is true.
failed_when
: This allows you to specify conditions under which a task should be considered failed.changed_when
: This sets conditions under which Ansible considers a task to have “changed,” affecting how it reports the success or failure of the task.
For example you have to install a package, but for Debian we use apt, for Red Hat we use yum. You can set conditions based on OS:
—
– name: Install NGINX
hosts: all
tasks:
– name: Install NGINX on Debian
apt:
name: nginx
state: present
when: ansible_os_family == “Debian” and ansible_distribution_version == “16.04”
– name: Install NGINX on Red Hat
yum:
name: nginx
state: present
when: ansible_os_family == ” Red Hat” or ansible_os_family == “SUSE”
Ansible Loops:
Loops in Ansible allow you to repeat a task or a set of tasks multiple times. There are different ways to implement loops in Ansible, including:
with_items
(deprecated in Ansible 2.5+): It is used to iterate over a list of items.loop
(Ansible 2.5 and newer): A more generic way to loop over any list or dictionary.
For Example a conditional within loops:
– name: Check status of a service and send an email
hosts: localhost
tasks:
– command : service httpd status
register : result
– mail :
to : < email >
subject : Service Altert
body : Httpd service is down
when: result.stdout.find(‘down’) != -1
– name: Install Softwares
hosts: all
vars:
packages:
– name: nginx
required: True
– name: mysql
required: True
– name: apache
required: False
tasks:
– name: Install “{{ item.name }}” on Debian
apt:
name: “{{ item.name }}”
state: present
when: item.required == True
loop: “{{ packages }}”
Ansible Roles:
Roles in Ansible are a way of organizing and structuring your playbooks. A role is essentially a collection of tasks, variables, and templates that can be easily reused across multiple playbooks. Roles help in modularizing and organizing your automation code.
Roles directory structure:
tasks
: Contains the main list of tasks to be executed by the role.handlers
: Contains handlers, which are tasks that only run when notified by other tasks.templates
: Contains template files that can be used by tasks.vars
: Contains variables that can be used by tasks.defaults
: Contains default variables for the role.meta
: Contains metadata about the role (e.g., dependencies).
There are many roles already made and available to use at Ansible-Galaxy, to setup your system. To find roles you can go through the Ansible website, but also through terminal:
ansible-galaxy search <name of the role>
To use a specific role:
ansible-galaxy install <name of the role>
By default it goes to /etc/ansible/roles, but can be changed at /etc/ansible/ansible.cfg