Edited 5 days ago by ExtremeHow Editorial Team
AutomationAnsibleConfiguration ManagementDevOpsScriptingCommand LineServer SetupOrchestrationPlaybooksManagement
This content is available in 7 different language
Managing a large number of servers and automating processes is a common task for system administrators and DevOps professionals. Ansible is a powerful and simple tool that can help in this aspect. It enables infrastructure as code, configuration management, and automation of tasks. This guide will walk you through the detailed steps on how to install and use Ansible on Linux. We will cover the basics needed to get started with Ansible.
Ansible is an open-source automation tool or platform used for IT tasks such as application deployment, orchestration, configuration management, in which it uses playbooks to configure systems, deploy software, and orchestrate advanced workflows. It is easy to set up, requires no agents, and uses YAML (Yet Another Markup Language) for its playbooks, making it fairly simple for beginners to understand and use effectively.
Before you get started, make sure you have the following:
The installation process for Ansible varies slightly depending on the distribution of Linux you are using. Let's take a look at some of the most common methods:
Follow these steps to install Ansible on Ubuntu:
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt update
sudo apt install ansible
ansible --version
If you are using CentOS or Red Hat Enterprise Linux (RHEL), follow these steps:
sudo yum install epel-release
sudo yum install ansible
ansible --version
For Fedora users, the process is pretty similar:
sudo dnf install ansible
ansible --version
After installing Ansible, the next step is to configure it. Ansible uses SSH to communicate with remote servers. You need to set up SSH access to the host machines you will manage through Ansible.
ssh-keygen
ssh-copy-id <user>@<host>
ssh <user>@<host>
The Ansible inventory file lists the hosts and groups of hosts that Ansible has been instructed to manage. By default, it is located at /etc/ansible/hosts
.
sudo nano /etc/ansible/hosts
[webservers]<hostname> ansible_host=<IP>[dbservers]<hostname> ansible_host=<IP>
Ansible playbooks are files that contain a series of tasks that Ansible will follow. They are written in YAML format, which is easy to read and understand. Here is a basic structure of an Ansible playbook:
---
- name: Ensure Apache is installed
hosts: webservers
tasks:
- name: Install Apache
apt:
name: apache2
state: present
In the above example:
name
: This describes what the playbook is about.hosts
: Specifies the hosts or groups of hosts to which the playbook applies.tasks
: List of tasks to be executed, each task has its own name
and action
.Ansible modules are the building blocks for creating Ansible playbooks. Each module is a standalone script that Ansible runs one at a time. Modules can be used to manage hardware, operating system configuration, and more.
Example of using apt
module in a playbook:
- name: Install Apache
hosts: webservers
tasks:
- name: Install Apache package
apt:
name: apache2
state: latest
Once you have written the playbook, you need to execute it to automate the tasks you want on your server. To run the playbook:
.yml
file extension.playbook.yml
with the filename of your playbook):ansible-playbook playbook.yml
Suppose you have created a playbook named setup_web.yml
with the following contents:
- name: Set up web server
hosts: webservers
become: true
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache
service:
name: apache2
state: started
Run it like this:
ansible-playbook setup_web.yml
After running, Ansible installs Apache and starts its service on all hosts listed under the webserver group in your inventory file.
To manage more complex systems, Ansible roles let you organize playbooks into reusable components. Roles allow you to split a playbook into multiple, smaller files that can be more easily maintained. Roles typically contain tasks, handlers, templates, variables, and files, organized into a specific structure.
ansible-galaxy init <rolename>
/roles
containing directories for tasks, handlers, etc.- hosts: webservers
roles:
- <rolename>
This will cause the functions defined in the role to be applied to the specified host.
Some common issues you may encounter when working with Ansible include:
Ansible is a versatile tool for automating server and application provisioning, configuration, and management. With its vast collection of modules and easy-to-understand playbooks written in YAML, Ansible becomes an indispensable tool in an IT administrator's toolkit. By following the steps mentioned, professionals can easily install Ansible using a Linux package manager, configure it easily, and automate tasks across their infrastructure with playbooks and roles.
If you find anything wrong with the article content, you can