How to Install Ansible on CentOS 8

Install Ansible on CentOS 8

In this article, we will have explained the necessary steps to install and configure Ansible on CentOS 8. 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 8 and will try to manage its two nodes:

  • Ansible Server – ansible.linuxtips.us ( 192.168.77.20 )
  • Ansible Client – 192.168.77.21

Install Ansible on CentOS

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

sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo dnf update

Step 2. Install Anisble on CentOS 8.

Once the epel repository is enabled, using the following dnf command to install Ansible:

sudo dnf install ansible

Verify 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:

[[email protected] ~]# ssh-keygen -t rsa -b 4096 -C "[email protected]"
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 [email protected]
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]-----+
[[email protected] ~]#

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

[[email protected] ~]# ssh-copy-id [email protected]
/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
[email protected]'s password:

Number of key(s) added: 1

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

[[email protected] ~]#
[[email protected] ~]# ssh-copy-id [email protected]
/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
[email protected]'s password:

Number of key(s) added: 1

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

[[email protected] ~]#
===============================
[[email protected] ~]# ssh [email protected]
Last login: Thu Sep 19 16:00:57 2019 from bezafari-c1
[[email protected] ~]# exit
logout
Connection to 192.168.77.21 closed.
[[email protected] ~]#

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

[[email protected] ~]# ssh [email protected]
Last login: Thu Jan 17 17:00:57 2019 from godetz-c1
[[email protected] ~]# exit
logout
Connection to 192.168.77.21 closed.
[[email protected] ~]#

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:

[[email protected] ~]# nano /etc/ansible/hosts
[test-servers]
192.168.77.20
192.168.77.21

Step 5. Manage ansible client from ansible server.

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

[[email protected] ~]# ansible -m ping 'test-servers'
192.168.77.20 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.77.21 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[[email protected] ~]#

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

[[email protected] ~]# ansible -m command -a "uptime" 'test-servers'
192.168.77.21 | CHANGED | rc=0 >>
16:17:35 up 20 min, 3 users, load average: 0.11, 0.16, 0.29

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

[[email protected] ~]#

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

[[email protected] ~]# ansible -m command -a "df -Th" 'test-servers' > /tmp/command-output.txt
[[email protected] ~]# cat /tmp/command-output.txt
192.168.77.20 | CHANGED | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 13G 8.0G 8.0G 14% /
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.77.21 | 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
[[email protected] ~]#

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