How to Install Ansible on CentOS 7

Install Ansible on CentOS 7

In this article, we will have explained the necessary steps to install and configure Ansible on CentOS 7. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Ansible is a free and open source Configuration and automation tool for UNIX like operating system. It is written in python and similar to Chef or Puppet but there is one difference and advantage of Ansible is that we don’t need to install any agent on the nodes. It uses SSH for making communication to its nodes.

In this article we will demonstrate how to install and configure Ansible in CentOS 7 and will try to manage its two nodes:

  • Ansible Server – ansible.idroot.us ( 192.168.43.46 )
  • Ansible Client – 192.168.43.120

Install Ansible on CentOS

Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.

sudo yum install epel-release
sudo yum update

Step 2. Install Anisble on CentOS 7.

Install Anisble using yum command:

yum install ansible

Once the installation is completed, check the ansible version:

ansible --version

Step 3. Setup Password less SSH configuration between ansible server and ansible client.

We need to generate ssh keys on ansible server and copy this key to public key on ansible client:

[root@idroot ~]# ssh-keygen -t rsa -b 4096 -C "root@192.168.43.46"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:rqAwbwQ8/meilanamaria4D2V0fQ49amw2WKbYIA root@192.168.43.46
The key's randomart image is:
+---[RSA 4096]----+
| .oo.+o |
| . =.=.. . |
|. E o + X . o |
|.o. + + = X . |
| .o. + oSX + |
| .. o.+ = |
|o. o oo.o |
| +.. +oo |
| .o ... |
+----[SHA256]-----+
[root@idroot ~]#

We can use ssh-copy-id command to copy public key from ansible server to ansible clients:

[root@idroot ~]# ssh-copy-id root@192.168.43.46
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.43.15's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@192.168.43.46'"
and check to make sure that only the key(s) you wanted were added.

[root@idroot ~]#
[root@idroot ~]# ssh-copy-id root@192.168.43.120
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.43.131 (192.168.43.131)' can't be established.
ECDSA key fingerprint is SHA256:OuHrnRxppengenkimpoi5Z+1tOWOF8eYZ5Le5MNwUQ.
ECDSA key fingerprint is MD5:78:1c:a5:72:bb:25:fa:c7:67:39:fc:91:b9:fb:b6:20.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.43.131's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@192.168.43.120'"
and check to make sure that only the key(s) you wanted were added.

[root@idroot ~]#
===============================
[root@urclouds ~]# ssh root@192.168.43.120
Last login: Thu Sep 19 16:00:57 2019 from bezafari-c1
[root@client ~]# exit
logout
Connection to 192.168.43.120 closed.
[root@idroot ~]#

We need to be check our ansible server and ansible clients is working password less or not? We can check as like below:

[root@idroot ~]# ssh root@192.168.43.120
Last login: Thu Jan 17 17:00:57 2019 from bezafari-c1
[root@client ~]# exit
logout
Connection to 192.168.43.120 closed.
[root@idroot ~]#

Step 4. Ansible server configuration to manage ansible client.

You can make inventory of our ansible server and ansible client using ansible server host file. Host file can be find in this path on ansible server /etc/ansible/host:

[root@idroot ~]# nano /etc/ansible/hosts
[test-servers]
192.168.43.46
192.168.43.120

Step 5. Manage ansible client from ansible server.

Check the connectivity of ‘test-servers’ or ansible clients using ping:

[root@idroot ~]# ansible -m ping 'test-servers'
192.168.43.46 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.43.120 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@idroot ~]#

We can check the uptime of our ansible client from ansible server:

[root@idroot ~]# ansible -m command -a "uptime" 'test-servers'
192.168.43.120 | CHANGED | rc=0 >>
16:17:35 up 20 min, 3 users, load average: 0.09, 0.16, 0.29

192.168.43.46 | CHANGED | rc=0 >>
16:17:35 up 22 min, 4 users, load average: 0.18, 0.29, 0.55

[root@idroot ~]#

Redirecting the output of command to a file. Like below:

[root@idroot ~]# ansible -m command -a "df -Th" 'test-servers' > /tmp/command-output.txt
[root@idroot ~]# cat /tmp/command-output.txt
192.168.43.46 | CHANGED | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 13G 5.0G 8.0G 39% /
devtmpfs devtmpfs 482M 0 482M 0% /dev
tmpfs tmpfs 497M 212K 497M 1% /dev/shm
tmpfs tmpfs 497M 7.1M 490M 2% /run
tmpfs tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 158M 340M 32% /boot
tmpfs tmpfs 100M 16K 100M 1% /run/user/0
192.168.43.120 | CHANGED | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 13G 3.9G 9.1G 30% /
devtmpfs devtmpfs 906M 0 906M 0% /dev
tmpfs tmpfs 921M 84K 920M 1% /dev/shm
tmpfs tmpfs 921M 8.7M 912M 1% /run
tmpfs tmpfs 921M 0 921M 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 157M 341M 32% /boot
tmpfs tmpfs 185M 20K 184M 1% /run/user/42
tmpfs tmpfs 185M 0 185M 0% /run/user/0
[root@idroot ~]#

Congratulation, you have learned how to install and configure Ansible on CentOS 7. If you have any question, please leave a comment below.