An Overview Of Ansible

What is Ansible?

 
Ansible is software mostly used for software configuration management (SCM), DevOps automation, and orchestration. Ansible simplifies the IT Operations in on-premises and multi-cloud environments.  System admins love to use this tool because of the simple installation and easy to understand concepts and ability to play with YAML files.
 

Why Ansible?

  • Ansible is open source
  • Ansible is very lightweight and there are no constraints regarding the operating system or underlying hardware
  • Highly secure because it's agentless and also ansible server communicates with clients by SSH
  • Easy to understand the concepts by official documentation. No need for any additional documentation except system administration

Important Concepts of Ansible

 
Playbook
 
Playbooks are written in YAML format and have a minimum configuration syntax. These playbooks consist of configurations, administrations, deployments, systems, automation tasks, and other orchestrations functions.
 
Action
 
An action will specify which of the modules to execute. A task must have only one action, but it may also have other parameters.
 
Task
 
Playbooks are used to execute tasks. The task consists of an action with a name and some other keywords. A list of tasks is also called Tasks.
 
Notify
 
You can understand notify as an alert. Whenever we define a handler for multiple tasks it should be done in order, like one by one. If one task is completed a notification will trigger the next task or handler. The purpose of the handler will trigger the next task.
 
Handlers
 
Handlers are also called tasks, but handlers are a particular kind of task that does not execute unless notifying by name. Handlers are mostly used for certain scenarios as when we update an existing configuration and we need to restart the system or service, at that time we will use the handlers to trigger restart by notifying the restart action.
 
Inventory
 
Inventory is a folder or file, which contains a list of Groups, and Hosts. In Inventory, we can define host details like Host's Name, IP, Domain name, etc.
 
Host
 
A host is a remote machine or client managed by Ansible. Each host is assigned by individual variables and organized into groups. These host details are written in the host_vars file in the inventory folder
 
Group
 
A group consists of ‘n’ number hosts. Each group can have variables that can be applied to all hosts in the group. These group details are written in the group_vars file in the inventory folder.
 
Library
 
Consists of module collections which  are available in /usr/bin/ansible or an Ansible playbook.
 
Templates
 
A template is a file, which contains configuration parameters. Each template has dynamic values that are given as variables. This template is used to copy data from the ansible controller to remote clients. This data is generated by the jinja2 engine.
 
Tags
 
Ansible tags should execute only one or some specific tasks from a long playbook instead of executing the whole playbook.
 
Ad-hoc commands
 
Commands, which can execute directly from the terminal to manage the remote hosts.
 
Galaxy
 
Ansible Galaxy is an open-source and online repository for sharing and pulling ansible modules, roles, and other collections.
 

Ansible facts

 
This fact used to fetch information from remote hosts from the ansible controller, which is used to declare as variables in playbooks for identifying remote hosts status.
 
Roles
 
The role is a combination of multiple tasks together into one container or unit to do automated tasks with clean directory structures.
  • We can modify roles easily.
  • By the help of roles, we can reduce the syntax errors
  • Assigning a role to a group of hosts (clients) or a particular host (client) or user, which implies implementing a set of certain variable values, certain tasks, and certain handlers. Because of the directory structure associated with a role, roles become a redistributable container that allows us to share tasks among playbooks.
Modules
 
Modules are the set of functions that Ansible send to clients. Modules are stored in /usr/bin/ansible or /usr/bin/ansible-playbook.
 
The entire ansible directory structure consists of inventories, playbooks, roles.
 
Roles Directory Structure explained in detailed,
  1. ├── ansible.cfg  
  2. ├── inventories  
  3. │   ├── development  
  4. │   │   ├── group_vars  
  5. │   │   │   └── development.yml  
  6. │   │   ├── hosts  
  7. │   │   └── host_vars  
  8. │            └── development_server.yml  
  9. │   └── production  
  10. │       ├── group_vars  
  11. │       │   └── production.yml  
  12. │       ├── hosts  
  13. │       └── host_vars  
  14. │           └── production_server.yml  
  15. ├── playbooks  
  16.      └── deployment.yml  
  17. └── roles  
  18.     └── apache  
  19.         ├── defaults  
  20.         │   └── main.yml  
  21.         ├── files  
  22.         ├── handlers  
  23.         │   └── main.yml  
  24.         ├── meta  
  25.         │   └── main.yml  
  26.         ├── README.md  
  27.         ├── tasks  
  28.         │   └── main.yml  
  29.         ├── templates  
  30.         ├── tests  
  31.         │   ├── inventory  
  32.         │   └── test.yml  
  33.         └── vars  
  34.             └── main.yml  
Example role - to install apache software in hosts,  so create role as apache and configured role components.
 
Tasks - contains main tasks to execute by the role.
  1. [root@gokul apache]# vi tasks/main.yml  
  2. ---  
  3. - name: Install httpd Package  
  4. yum: name=httpd state=latest  
  5. - name: Copy httpd configuration file  
  6. copy: src=/data/httpd.original dest=/etc/httpd/conf/httpd.conf  
  7. - name: Copy index.html file  
  8. copy: src=/data/index.html dest=/var/www/html  
  9. notify:  
  10. - restart apache  
  11. - name: Start and Enable httpd service  
  12. service: name=httpd state=restarted enabled=yes  
Files - contain files required to transfer or deploy to the target machines via this role.
 
Copy the required files (httpd.conf and index.html) to the files directory
  1. [root@gokul apache]# ll files/*  
  2. -rw-r--r-- 1 root root 11753 Feb 4 10:01 files/httpd.conf  
  3. -rw-r--r-- 1 root root 66 Feb 4 10:02 files/index.html  
  4. [root@gokul apache]# cat files/index.html  
Handlers - contains handlers, which may used by this role.
 
Edit handler’s main.yml to restart the server when there is a change
  1. [root@gokul apache]# cat handlers/main.yml  
  2. ---  
  3. # handlers file for /etc/ansible/roles/apache  
  4. - name: restart apache  
  5. service: name=httpd state=restarted  
Defaults - contain default variables for the role.
 
Vars - other variables for the role. Vars have a higher priority than defaults.
 
Templates - contains templates, which can deploy via this role.
 
Meta - defines some data / information about this role.
 
Edit meta main.yml to add the information about the roles like author, descriptions, license,
 
  1. root@gokul apache]# cat meta/main.yml  
  2. galaxy_info:  
  3. author:gokul  
  4. description: Apache Webserver Role  
  5. company: gokul  
  6. # If the issue tracker for your role is not on github, uncomment the  
  7. # next line and provide a value  
  8. # issue_tracker_url: http://gokulakrishna.blog/issue/tracker  
  9. # Some suggested licenses:  
  10. # - BSD (default)  
  11. # - MIT  
  12. # - GPLv2  
  13. # - GPLv3  
  14. # - Apache  
  15. # - CC-BY  
  16. license: license (GPLv2, CC-BY, etc)  
  17. min_ansible_version: 1.2  
  18. # If this a Container Enabled role, provide the minimum Ansible Container version.  
  19. ------skipped  
I hope you understood this Ansible overview.
 
Please like and share.


Similar Articles