How To Install And Configure Ansible On Centos 7
In this blog , I have explained in detail on how to install and configure ansible on Centos 7.
In this guide , We will also learn to execute commands on the target servers.
Ansible is most widely used configuration management tool , But there are other tools such as Chef , Puppet , Saltstack etc.
What Is Ansible?
- Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.
- It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.
- In simple words , It is a configuration management system which is used to control large number of servers for administration in an automated from the one server.
- Ansible is a agent-less software ,which means you don’t need to install any software on the target servers.The communication between the ansible server and the target servers is through SSH keys based authentication.
How Ansible Works?
- Ansible works by connecting with the target servers and send a small programs called as ansible modules.
- Ansible execute those modules on target servers over SSH connection.
- Once the installation or configuration is completed (whatever is sent to target servers as modules) , The modules will be removed from the target servers.
What Is Ansible Playbook?
- Ansible playbooks are written in YAML.
- Playbook contains the series of commands which the users wants to execute them in target servers.These commands are defined as tasks and they are basically called as modules.
- You can define different tasks for each hosts.
For this tutorial , We will need two servers.
Server 1 : Ansible Control Node – We will install ansible here and from this machine we will connect to targets servers , also called as ansible hosts over SSH.
Server 2 : Ansible Host – This is the server that will be managed / controlled by the Ansible Control Node.
Make sure you have sudo or root privileges to the servers.
Installing Ansible On Centos
Lets go ahead and install ansible on the Ansible control Node (Ansible Server).
Before installing ansible make sure the server is up-to-date.
sudo yum update
To install ansible , We need to install Centos EPEL repository on the system.
yum install epel-release -y
Using the below command , We can install the ansible from the epel repository.
yum install ansible -y
Once the ansible is installed on the system , We can check the version of ansible installed on the system using the below command.
Now the Ansible Control Node has all the required softwares installed to manage the ansible hosts.
While installing Ansible , Ansible-playbook package also will be installed on the system.To check the version of ansible-playbook ,
Adding SSH Keys To Ansible Hosts
Ansible (Control Node) uses SSH to communicate with the target servers (ansible hosts)
We need to generate SSH keys in the ansible server and then add those keys in the client hosts.
To generate SSH key pair , Use the below command.
ssh-keygen -t rsa
If you want you can enter a secure passphrase to add a layer of security to prevent any unauthorized users from logging in with the help of keys.
Press Enter until the command return to bash shell.
Go to .ssh folder and copy id_rsa.pub file.
We need to copy this public key to the client hosts.
Login to the client server and then we need to add the public key under a user’s .ssh folder on authorized_keys file.
In my case , I will be adding the SSH public key to ubuntu user.Go to ubuntu user’s .ssh folder
and then open authorized_keys file and paste the SSH public key here.
Save and close the file.
Now from the Ansible Control node , You should be able to SSH into the client servers as ubuntu user.
Setup Inventory File
Next step is to configure inventory file.The inventory file contains information about the target servers that will be managed by the ansible control node.
We can add ‘n’ number of servers in the inventory file and each hosts can be organized into groups and sub-groups.
Lets edit the default ansible inventory file and add the client hosts.
If you don’t find hosts file under /etc/ansible directory , You can manually create it.
In the default inventory file , You can find some examples and we can use this as a reference to setup our own inventory.
First we will define a group and then we will add a host in it as shown below.
ansible-host is the group
host1 is the alias for the server 172.31.22.178
Likewise You can add many hosts under a group or you can create different groups and add hosts based on their use cases.
Once done , Save and close the file.
Run the below command to check the hosts added to ansible inventory.
Testing Connection Between Ansible Control Node And Client Hosts
We have successfully installed and configured ansible , Now its time to test the connection between ansible control node and the client hosts.
From the ansible control node , Run the below command to ping the client hosts.
ansible ansiblehosts -m ping --user ubuntu
In the above command , ansiblehosts is the host group , ping (a module) is a ansible-adhoc command which tries to connect with the client hosts and returns pong response.
I am explicitly referring –user as ubuntu as I am authenticating target server as ubuntu user and it is because , the ansible control node’s SSH public key is added to ubuntu user.
You should get a response as shown below.
But If you want to specify the ansible user with in the ansible inventory , Instead of referring it explicitly , You can configure as shown below.
[ansiblehosts]host1 ansible_ssh_host=172.31.22.178 ansible_user=ubuntu
We are still authenticating as ubuntu user , But we dont need to pass –user parameter while running ansible ping command.
If you have added SSH public key to the root user , You don’t have specify –user explicitly.
If you have configured multiple hosts in the ansible inventory file , You can ping all of them at once using the below command.
ansible -m ping all
The above command will ping all the client system though you can configured multiple host groups in your inventory file.
Running Ad-Hoc Commands
As like you run some shell commands on the servers directly , You can use ansible control node to execute commands on the client systems.
To check the Disk usage on the client hosts from ansible control node.
ansible ansiblehosts -a "df -Th"
The above command it to check disk usage of hosts under specific host group (ansiblehosts)
ansible all -a "df -Th"
The above command is to check disk usage of all the hosts added to the ansible inventory.
To check the Free Memory on the client hosts
ansible all -a "free -m"
Likewise you can replace “df -Th” / “free -m” and you can run any command as you like.
We have successfully installed and configured ansible on Centos 7 and also we have learnt to setup inventory and run some ad-hoc commands from the ansible node server to the client systems.
Thanks for reading , Hope you find it helpful.
Please do check out my other blogs.