Install Ansible on Linux Easily
Install Ansible on Linux using your system’s package manager like apt or yum. Update packages, install Ansible, and verify with ansible –version to start automating your servers.
🤖AI Overview:
Installing Ansible on Linux involves setting up the automation tool on popular distributions such as Ubuntu and CentOS. It requires Python and configuring the proper repositories. Ansible enables efficient server management over SSH without additional agents, ideal for automating configuration management tasks on Linux systems.
Ansible has gained popularity among configuration management tools due to its excellent features, which include the following:
- Ansible is Agentless and does not rely on Agent and special software to install on a node or system.
- It is written based on Python programming language.
- Ansible coding in setting up provisioning details based on YAML files and language.
- It uses SSH protocol to make configuration changes in connecting to the remote server and communicating and managing systems; So it is a very safe method.
Prerequisites for installing Ansible on Linux
Ansible requires a control node to install, which is created by connecting each machine to the remote machine through port 22 (SSH). As a result, you can consider your Linux VPS or a local machine to host and run Ansible. In this tutorial, we have assigned Linux VPS with Ubuntu or CentOS operating system as the Ansible control node. To install Ansible, other factors that must be considered are as follows:
- User account with sudo privileges
- ufw firewall configuration
- External access to the management panel of the non-root user
- Having the SSH key pair in connection with the desired user
- Installing Python, which is necessary to manage Linux by Ansible.
How to install Ansible on Linux (Ubuntu)
If you are interested in using the Ansible configuration management tool, you must install it on the machine you plan to designate as an Ansible control node.
First, you must connect to the Ansible controller that you intend to create (Ubuntu 20.04 server) through SSH and access its command line.
1. Since Ansible requires Python to manage and perform server infrastructure control tasks, your Linux system must support Python programming language. Python is usually installed as the default package in Linux distributions, but if you find that the Ubuntu operating system does not support Python, first install the Python package on your Linux operating system by running the following command:
sudo apt-get install python3
2. Then add the project’s official PPA (Ansible package repository) to the Ubuntu server resources that you have considered as the control node by entering the following command:
sudo apt-add-repository ppa:ansible/ansible
After executing the previous command, accept the PPA installation confirmation request by pressing Enter.
3. Update the Ubuntu System Package Repository to apply the changes you made in the previous step to the Ubuntu System Package Repository:
sudo apt update
4. Now the Ubuntu system is ready to download and install the Ansible tool, for this purpose you must run the following command:
sudo apt install ansible
5. You can make sure that the Ansible installation process is correct by running the following command:
ansible -v
In the output, if Ansible is installed correctly, the version of Ansible that you installed should be displayed.
Therefore you equipped your Ubuntu server with Ansible to manage different hosting servers.
How to install Ansible on CentOS
Follow the steps below to install Ansible on the CentOS7 server:
1. First, connect to the CentOS server from your client through the SSH protocol.
2. If the Python language package is not installed by default on the CentOS server, install it by running the following command:
sudo yum install python3 -y
3. Then, if the CentOS system does not support the EPEL repository, you must install the Extra Packages for Enterprise Linux (EPEL) repository, which includes the Ansible package:
sudo yum install epel-release
4. Update the package list of the CentOS system using the following command so that the default CentOS repository becomes aware of the addition of the EPEL repository:
sudo yum update -y
5. After making sure to install the EPEL repository and update the default CentOS repository, now you can install Ansible on CentOS7:
sudo yum install ansible -y
6. Run the following command to check the success of Ansible installation on CentOS7:
ansible --version
Or
ansible -v
The display of the Ansible version in the output of the previous commands indicates the success of the Ansible installation process on CentOS.
In the next steps, we will explain how to configure Ansible to add your hosts to the control node’s inventory file.
How to configure Ansible controller hosts
For Ansible to perform its management tasks efficiently and communicate with other nodes, it needs to set up an Inventory File that stores information such as IP addresses and names of hosts that you want to manage with Ansible. The contents of the Inventory File can store multiple servers and their information in the form of sets or subsets to make it easier to organize information related to multiple servers.
Another advantage of Inventory File is that you can set variables that are allowed to be used in playbooks and templates of certain hosts.
Of course, creating and editing an Inventory File is not the only way to connect to managed nodes, but it is the most efficient method because you will need the managed server’s IP or hostname to execute commands.
Therefore, in the configuration of Ansible controller hosts, you must first access the default Ansible inventory content of your system and edit it. For this purpose, access the /etc/ansible/hosts file on the Ansible control node through the nano editor (any editor of your choice):
sudo nano /etc/ansible/hosts
By default, Ansible creates the inventory file in the, etc/ansible/hosts directory, but you can change the default location of the inventory file according to your needs. For this purpose, when running Ansible commands, add the -i parameter to the path of your custom inventory file.
You can use our example as a guide for your system inventory settings, in this tutorial we have assigned the Ubuntu server as an Ansible controller host. So pay attention to the following example:
/etc/ansible/hosts
[servers]
server1 ansible_host=192.0.168.111
server2 ansible_host=192.0.168.112
server3 ansible_host=192.0.168.113
[all:vars]
ansible_python_interpreter=/usr/bin/python3
In the example you see, 3 servers with the names server1, server2, and server3 have formed a group. Of course, to use the above example, you have to put your Ansible IP host. We have provided the previous example for a better understanding.
By running the previous example, for existing hosts, the ansible_python_interpreter host parameter will be valid and the remote Ubuntu server will be set to use /usr/bin/python3 Python 3.
After the desired settings, save the /etc/ansible/hosts file by pressing CTRL+X, then Y, and Enter. Then exit the file.
Run the following command to check your inventory:
ansible-inventory --list -y
Output:
all:
children:
servers:
hosts:
server1:
ansible_host: 192.0.168.111
ansible_python_interpreter: /usr/bin/python3
server2:
ansible_host: 192.0.168.112
ansible_python_interpreter: /usr/bin/python3
server3:
ansible_host: 192.0.168.113
ansible_python_interpreter: /usr/bin/python3
ungrouped: {}
The output you will get will be similar to our example output but your server’s infrastructure may be different than ours.
Testing Ansible connection to managed nodes
This part of the article is presented to check the Ansible connection to the servers that you have set in the inventory file. To run Ansible connection check commands, it is better to have root user permission because this user account has the necessary permission by default in newly created servers. Of course, it should be noted that if the Ansible hosts have created a normal user with sudo privileges, you should use that user account.
You can use the-u
argument to find out the remote server user.
Therefore, to check the connection from the Client side or the Ansible control node, run the following command:
ansible all -m ping -u root
Output:
server1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
server2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
server3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
If you are connecting to your default servers through SSH for the first time, respond to the confirmation request about the correctness of the hosts you are accessing through Ansible by entering Yes and then pressing the Enter button.
receiving the Pong message from the server indicates successful Ansible connections and now you can run Ansible commands.
By executing this command, Ansible’s internal ping module is called to test connections on all available nodes. The ping module checks the availability of hosts, the validity of SSH credentials, and the execution of Ansible modules based on Python.
How to use Ansible through Ad-Hoc commands
After starting Ansible on the remote server and making sure that the Ansible control node is connected to the hosts you specified in the inventory file, you can make the process of controlling and managing the hosts you specified more efficient and easier by executing Ad-Hoc commands. Therefore, learning how to use Ad-Hoc one-line commands is very useful in improving the management of your hosts.
One of the advantages of Ansible is that by executing the command on the remote server, it is possible to execute the same command on the servers specified in the inventory file which makes it easier to control your other servers
If you want to check the disk space occupied by your servers, you can run the following command in the Ansible control node:
ansible all -a "df -h" -u root
In the previous command, you can replace the “df -h” parameter with other parameters for different purposes. You can also call Ansible modules using ad-hoc commands. For example, by running the following command, you can install the latest version of vim on your specified servers in the inventory file:
ansible all -m apt -a "name=vim state=latest" -u root
You can also check each of the hosts separately for better management of your hosts. For example, by running Ansible commands, control the desired host or subgroup and monitor the hosts’ uptime:
ansible servers -a "uptime" -u root
You may want to check 2 servers among several servers. For this purpose, you can separate them in the running of the commands as in the example we provide:
ansible server1:server2 -m ping -u root
Conclusion
Installing Ansible on Linux is a foundational skill for automating server management and simplifying system administration tasks.
Whether you are using Ubuntu, CentOS, or other Linux distributions, installing Ansible involves ensuring the environment meets prerequisites, using the appropriate package manager or repository to install Ansible, configuring your managed hosts, and verifying connectivity.
By following this guide on how to install Ansible on Linux, you now have the tools to automate application deployments, system updates, configuration changes, and more across your infrastructure from a centralized control node.
As you grow comfortable with Ansible, you can explore writing playbooks and creating complex automation workflows, making your server administration faster, more reliable, and consistent.
FAQ
2. What are prerequisites before installing Ansible on Linux?
You need sudo privileges, SSH access (typically port 22), Python installed, and a Linux machine as the control node.
3. How do I install Ansible on Ubuntu?
Install Python3 if needed, add the Ansible PPA if your Ubuntu version requires it, update packages, then run "sudo apt install ansible".
4. How is Ansible installed on CentOS or alternatives?
Install Python3 if missing, enable EPEL repository, update packages, then install Ansible using "sudo dnf install ansible" or "yum".
5. How do I configure hosts for Ansible?
Edit "/etc/ansible/hosts" to list the IPs or hostnames of managed nodes and set necessary variables.
6. How can I test the Ansible connection to managed hosts?
Use "ansible all -m ping -u user" to send a ping command; a successful pong confirms connection.
7. Can Ansible run commands on multiple servers at once?
Yes, it supports "ad-hoc" commands that run on all or selected hosts from the inventory file simultaneously.
8. Why is Python required for Ansible?
Ansible modules and scripts rely on Python. It must be installed for Ansible to function on both the control node and managed hosts.
9. Is Ansible secure?
Yes, it uses SSH for communication with hosts and requires no agents, minimizing security risks.
10. Where to find more help on Ansible installation?
Official Ansible documentation and community tutorials provide detailed guidance and troubleshooting tips.