Definition:
Ansible is a configuration management and provisioning tool that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.
It works by establishing a connection between the nodes, through SSH, and pushing small programs called modules. In Ansible-speak, a script is called a playbook. A playbook usually contains a list of hosts (what Ansible calls remote servers) that need to be configured and an ordered list of tasks to perform on those hosts.
Why Ansible:
- Resource utilization: Ansible is an agent-less software and does not require any extra services or daemons to be installed on its nodes.
- Security: Ansible uses SSH connection protocol between Ansible server and its nodes. Using an SSH connection is more secure because the connection is encrypted.
- Idempotency: Ansible modules are idempotent which means that modules can be safely re-run any number of times.
- Simple Code: Ansible programs or modules are written in simple YAML script files and can be created either by placing everything in a single file or by following a structured model.
Below is the comparison chart for some of the configuration management tools available:
Parameter/Tool | Ansible | Chef | Puppet |
Architecture | Agent Less | Master/Agent | Master/Agent |
Mechanism | Push | Pull | Pull |
Connection | SSH, Minimal in nature | Chef server, chef client | Server & client |
Language | Python , Simple YAML structure | Ruby, ERB & JSON | Ruby |
Learning Curve | Low Learning | High Learning | High Learning |
Use Ansible to configure 4Linux (ubuntu-based web servers to install nginx)
How Ansible works
For example, consider hosts as webserver 1, 2, 3, and 4. Ansible uses inventory file (a list of nodes) to establish communication.
# /etc/ansible/hosts
[webservers]
webserver1.hostname.com
webserver2.hostmame.com
webserver3.hosname.com
webserver4.hostname.com
Below are the tasks we are going to perform:
- Install nginx
- Generate nginx configuration file
- Start the nginx service
Let us name the playbook, webserver.yml, and execute the below command.
Webserver.xml
---
- hosts: webservers
tasks:
- name: Installs nginx web server
apt: pkg=nginx state=installed update_cache=true
notify:
- start nginx
# ansible-playbook webservers.yml
Once the above command is executed, Ansible makes ssh connection parallely with all the nodes and performs nginx web configuration tasks.
Ansible Execution Flow