Ansible : Automation Basics
Infrastructure deployment and management can be challenging tasks in the current fast-paced IT environment. Manual configurations, repetitious work, and human errors add time to the process and raise the possibility of configuration errors. Here comes Ansible, a powerful and equally loved open-source automation tool that makes managing IT infrastructure simple.
What is Ansible ?
Ansible is an automation engine that allows you to automate tasks across your infrastructure. It is lightweight and simple to set up because it is agentless, which means that the target PCs don’t need to have any software installed. Ansible is efficient and secure because it connects to distant computers using SSH.
One of the key features that make Ansible lightweight and simple to set up is its agentless nature. Being agentless means that you don’t need to install any additional software or agents on the target machines (remote hosts) to manage them using Ansible.
With agent-based systems, you typically need to deploy and maintain agents on each target machine, which can add complexity to the setup and management process. However, with Ansible, communication with remote hosts happens over standard protocols like SSH (Secure Shell) or WinRM (Windows Remote Management), making it easy to get started without any additional installation requirements on the target hosts.
The agentless approach simplifies the deployment and reduces the overhead of managing the agents themselves. It also means that you can quickly start using Ansible to automate tasks on your infrastructure without worrying about installing and maintaining additional software on your target machines. This simplicity and ease of setup have contributed to Ansible’s widespread adoption and popularity in the IT automation and configuration management domain.
Getting Started :
Installing Ansible on your control machine — the computer from which you will execute Ansible — is the first step in using it. This could be a central server or a machine on your property. Using Python’s package manager, pip, you may install Ansible as demonstrated below:
pip install ansible
1. Inventory File
To define the target hosts and groups, Ansible uses an inventory file. The inventory file, which is in INI or YAML format, includes details about the hosts, their IP addresses, and the groups to which they belong.
Ansible employs an inventory file that categorises and lists all the hosts (controlled nodes) in accordance with their functions. The inventory file, which can be in INI or YAML format, contains crucial details about each host, including IP addresses, connection information, and variables.
[web_servers]
web1 ansible_host=192.168.1.101
web2 ansible_host=192.168.1.22
[databases]
db1 ansible_host=192.168.1.201
db2 ansible_host=192.168.1.32
2. Playbooks:
Ansible automation is defined using Playbooks, which are written in YAML. The activities that should be executed on the managed nodes are specified in a playbook, which is a collection of organised tasks. Each playbook consists of one or more “plays”, and each play is directed at a certain inventory of hosts.
Ansible uses playbooks written in YAML format to define automation tasks. Here Ansible playbook for setting up the Nginx web server on distant hosts is provided :
---
- name: Install Nginx
hosts: web_servers
tasks:
- name: Update apt cache (Ubuntu/Debian)
become: true
apt:
update_cache: yes
- name: Install Nginx
become: true
apt:
name: nginx
state: present
in this example, there is only one play in the playbook. The play has two jobs and targets hosts in the “web_servers” category. The first task is to refresh the package cache, and the second is to install Nginx on Debian-based systems using the package manager.
The “become” directive is used to tell Ansible to execute this task with elevated privileges. It usually means running the task as the “root” user or using “sudo” to perform privileged operations. Installing packages typically requires elevated permissions, so this directive ensures that the task will be executed with the necessary privileges.
apt:
name: nginx
state: present
This is the main part of the task, where the apt module is used to manage packages on the target machine. The apt module is specific to Debian-based systems (like Ubuntu) and allows Ansible to interact with the package manager (apt-get) to install, remove, or update packages.
name: nginx:
This specifies the name of the package that Ansible will try to install. In this case, it’s “nginx,” which is a popular web server and reverse proxy.
state: present:
This tells Ansible the desired state for the package. Here, it’s set to “present,” which means Ansible will ensure that the “nginx” package is installed on the target machine.
When this playbook is executed against a target machine, Ansible will check if Nginx is already installed. If it’s not present, Ansible will use the apt module to install the Nginx package with elevated privileges due to the become: true directive.
3. Modules:
Ansible employs modules to carry out particular tasks on controlled nodes. Ansible ships with a huge selection of pre-built modules, which are brief scripts written in Python or any other language. Playbooks can use them to manage users, services, files, packages, and more.
In the example playbook above, the apt and copy modules are used to install Nginx and copy the web application files to the managed nodes, respectively.
4. Execution:
Once the inventory and playbooks are prepared, you can run Ansible on the control machine, specifying the playbook to execute. Ansible connects to the managed nodes via SSH (or another configured connection method) and starts executing the defined tasks.
You can start the Ansible playbook using the command line after creating the inventory file and the playbook:
ansible-playbook -i inventory.ini install_nginx.yaml
Ansible will establish connections to the hosts in the “web_servers” group and carry out the playbook’s actions on those hosts.
Steps to follow
Let’s go over how to configure your infrastructure with Ansible. Make sure Ansible is set up on your command computer, using pip.
1. Create the Inventory:
Create the inventory file with the required host information and organise the hosts logically by roles.
2. Create Playbooks:
Playbooks are definitions of the actions you want to take on the managed nodes.
3. Understand Modules:
Familiarize yourself with the available modules and their parameters. Each module’s documentation in Ansible has a wealth of information.
4. Run Playbooks:
Use the ansible-playbook command to carry out the playbooks. Keep an eye on the results to make sure everything went well.
Additional:
Dynamic Inventory:
Ansible allows you to use dynamic inventory scripts to fetch the inventory information from external sources like cloud providers or databases. Your inventory is automatically kept current in this way.
Templates:
Ansible can use Jinja2 templates to generate configuration files dynamically. When deploying apps with unique configurations, this is quite helpful.
Roles:
Using roles allows for the hierarchical organisation and reuse of tasks, handlers, and variables. They give you a more flexible way to manage your infrastructure.
Deploy a basic application:
Using Docker with Nginx to deploy a web application is demonstrated in the following straightforward Ansible playbook:
- name: Deploy Web App
hosts: web_servers
tasks:
- name: Install Docker
become: true
apt:
name: docker.io
state: present
- name: Start Docker service
become: true
service:
name: docker
state: started
- name: Pull Docker image
become: true
docker_image:
name: dummy_web_app_image
source: pull
- name: Start the web container
become: true
docker_container:
name: dummy_web_app_container
image: dummy_web_app_image
ports:
- "80:80"
- name: Install Nginx
become: true
apt:
name: nginx
state: present
- name: Configure Nginx
become: true
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify:
- Restart Nginx
handlers:
- name: Restart Nginx
become: true
service:
name: nginx
state: restarted
Here the playbook sets up Docker, pulls a pre-built web application image, runs it as a container, and configures Nginx to act as a reverse proxy.
Docker allows you to expose network ports of a container to the host machine, allowing external traffic to reach services running inside the container. The format for defining port mappings in Docker is:
host_port : container_port
the line ports: - "80:80"
in the Ansible playbook ensures that the web application inside the Docker container is accessible from port 80 on the host machine, enabling users to interact with the web application through their web browsers.
Conclusion:
Ansible’s straightforward push-based architecture, inventory management, playbook execution, and extensive module library make it a powerful automation tool. You can effectively prepare your infrastructure and automate repetitive processes, saving time and lowering the risk of errors in your IT operations, by comprehending how Ansible functions and adhering to the setup flow. It’s time to embrace Ansible and experience the advantages of automation firsthand.
Good luck automating !!!