Hamsa K
Editor
7 min read | 4 years ago

How to Install Ansible on Centos 8

Install Ansible on Centos 8/Rhel 8

Ansible is the leading Open Source configuration management system.It depends on the SSH protocol to communicate with the remote nodes.Ansible allows sysadmins to manage hundreds of servers from one centralize node.

Step 1: Install Ansible

Ansible is not avaualble in centos 8 default repository.so first we need to install epel repository with following command.

[root@localhost ~]# dnf install epel-release

once epel repository is enabled, then install ansible.

[root@localhost ~]# dnf install ansible
[root@localhost ~]# dnf install ansible
Last metadata expiration check: 0:07:03 ago on Wed 24 Jun 2020 01:04:54 PM IST.
Dependencies resolved.
========================================================================================================================================================================
 Package                                       Arch                              Version                                     Repository                            Size
========================================================================================================================================================================
Installing:
 ansible                                       noarch                            2.9.10-1.el8                                epel                                  17 M
Installing dependencies:
 python3-babel                                 noarch                            2.5.1-5.el8                                 AppStream                            4.8 M
 python3-jinja2                                noarch                            2.10.1-2.el8_0                              AppStream                            538 k
 python3-jmespath                              noarch                            0.9.0-11.el8                                AppStream                             45 k
 python3-markupsafe                            x86_64                            0.23-19.el8                                 AppStream                             39 k
 python3-pyasn1                                noarch                            0.3.7-6.el8                                 AppStream                            126 k
 libsodium                                     x86_64                            1.0.18-2.el8                                epel                                 162 k
 python3-bcrypt                                x86_64                            3.1.6-2.el8.1                               epel                                  44 k
 python3-pynacl                                x86_64                            1.3.0-5.el8                                 epel                                 100 k
 sshpass                                       x86_64                            1.06-9.el8                                  epel                                  27 k
Installing weak dependencies:
 python3-paramiko                              noarch                            2.4.3-1.el8                                 epel                                 289 k
Transaction Summary
========================================================================================================================================================================
Install  11 Packages
Total download size: 23 M
Installed size: 123 M
Is this ok [y/N]: y

once Ansible is installed, you can verify version with following command.

[root@localhost ~]# ansible --version
ansible 2.9.10
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, May 21 2019, 23:51:36) [GCC 8.2.1 20180905 (Red Hat 8.2.1-3)]

Step 2: Install Ansible using pip

generally python3 and pip will come default with centos8 and Rhel 8 installation

only. if they are not installed, you can install them with below commands.

install python3 and verify version also

dnf install python3
python3 -V

Install pip (Python’s package manager)

dnf install python3-pip
pip3 -V

Now install Ansible using pip

pip3 install ansible --user

Step 3: Test Ansible 

First we need to make sure ssh is up and running in server.

systemctl status sshd

Ansible uses openssh for remote communication. Ansible supports both password and passwordless authentication to execute commands on managed nodes.Here i am using passwordless authentication.

If you installed Ansible using yum or dnf, then its configuration file, inventory file and roles directory created automatically under /etc/ansible folder.The hosts file is inventory where you will have all your remote nodes.now opwn hosts file using vi or nano editor and add remote nodes like below

[root@localhost ~]# vi /etc/ansible/hosts

Add remote nodes at end of the file( in my case one node is added.you can add multiple nodes). 

[web]
192.168.0.100

Now generate your local user’s public and private key using ssh-keygen

[root@localhost ~]# ssh-keygen -t rsa

sample output:

[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:8Y5Uy1B2FK4J8hg5DoiP/GJCYpYx3ncssE7Abz22TJg root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|          o.+.   |
|.. .   . o o     |
|.=... = + . .    |
|ooB *o.* B +     |
|oB.E BooS *      |
|= = = =. o       |
|.o o o  . .      |
|o .              |
|                 |
+----[SHA256]-----+

Now exchange public key between the ansible server and its remote nodes.

[root@localhost ~]# ssh-copy-id 192.168.0.100

sample output:

[root@localhost ~]# ssh-copy-id 192.168.0.100
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.0.100 (192.168.0.100)' can't be established.
ECDSA key fingerprint is SHA256:3VKw4jTt1URYOMlzSWl/co4bZMgCv7buhANVj31mwQs.
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.0.100's password:xxxxx------ Enter remote node password
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh '192.168.0.100'"
and check to make sure that only the key(s) you wanted were added.

Lets verify the connectivity from Ansible server to its remote nodes using ping module as shown below.

[root@localhost ~]# ansible -m ping all

sample output:

[root@localhost ~]# ansible -m ping all
192.168.0.100 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

 



Warning! This site uses cookies
By continuing to browse the site, you are agreeing to our use of cookies. Read our terms and privacy policy