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,
- ├── ansible.cfg
- ├── inventories
- │ ├── development
- │ │ ├── group_vars
- │ │ │ └── development.yml
- │ │ ├── hosts
- │ │ └── host_vars
- │ └── development_server.yml
- │ └── production
- │ ├── group_vars
- │ │ └── production.yml
- │ ├── hosts
- │ └── host_vars
- │ └── production_server.yml
- ├── playbooks
- └── deployment.yml
- └── roles
- └── apache
- ├── defaults
- │ └── main.yml
- ├── files
- ├── handlers
- │ └── main.yml
- ├── meta
- │ └── main.yml
- ├── README.md
- ├── tasks
- │ └── main.yml
- ├── templates
- ├── tests
- │ ├── inventory
- │ └── test.yml
- └── vars
- └── 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.
- [root@gokul apache]# vi tasks/main.yml
- ---
- - name: Install httpd Package
- yum: name=httpd state=latest
- - name: Copy httpd configuration file
- copy: src=/data/httpd.original dest=/etc/httpd/conf/httpd.conf
- - name: Copy index.html file
- copy: src=/data/index.html dest=/var/www/html
- notify:
- - restart apache
- - name: Start and Enable httpd service
- 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
- [root@gokul apache]# ll files/*
- -rw-r--r-- 1 root root 11753 Feb 4 10:01 files/httpd.conf
- -rw-r--r-- 1 root root 66 Feb 4 10:02 files/index.html
- [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
- [root@gokul apache]# cat handlers/main.yml
- ---
- # handlers file for /etc/ansible/roles/apache
- - name: restart apache
- 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,
- root@gokul apache]# cat meta/main.yml
- galaxy_info:
- author:gokul
- description: Apache Webserver Role
- company: gokul
- # If the issue tracker for your role is not on github, uncomment the
- # next line and provide a value
- # issue_tracker_url: http://gokulakrishna.blog/issue/tracker
- # Some suggested licenses:
- # - BSD (default)
- # - MIT
- # - GPLv2
- # - GPLv3
- # - Apache
- # - CC-BY
- license: license (GPLv2, CC-BY, etc)
- min_ansible_version: 1.2
- # If this a Container Enabled role, provide the minimum Ansible Container version.
- ------skipped
I hope you understood this Ansible overview.
Please like and share.