By reading this article, you can understand an overview of ansible. What is it? Why we need it? Moreover its concepts.
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 so that System admins are love this tool to use because of simple installation and easy-to-understand concepts and play with YAML files.
Why we need Ansible?
- Ansible is an Open source
- Ansible is very lightweight and no constraints regarding the operating system or underlying hardware are present
- Highly secure because of agentless and also ansible server communicating with clients by SSH
- Ease to understand the concepts by official documentation. No need for any additional skills except system administration
Important Concepts of Ansible
Playbook
Playbooks are written in YAML format and have a minimum of 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 used to execute tasks. The task consists of an action with a name and some other keywords. A list of tasks also called Tasks.
Notify
You can understand notify as an alert. Whenever we define a handler for multiple tasks should be done in order like one by one. If one task completed notify will trigger the next task or handler. The purpose of the handler will trigger the next task.
Handlers
Handlers also called tasks, but handlers are a particular kind of task that does not execute unless notifying by name. Handlers mostly used for certain scenarios as if 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 Hosts details like Hosts Name, IP, Domain name, etc.
Host
A host is a remote machine or client managed by Ansible. Each host 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 collection 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 used to copy data from the ansible controller to remote clients. This data 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 identification 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:
├── 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 created role as apache and configured roles components.
Tasks - contains main tasks to execute by the role.
---
- 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 deployed to the target machines via this role.
Copy the required files (httpd.conf and index.html) to the files directory
-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 - contain handlers, which may used by this role.
Edit handler’s main.yml to restart the server when there is a change
---
# handlers file for /etc/ansible/roles/apache
- name: restart apache
service: name=httpd state=restarted
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,
platforms supported
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