Installing Ansible on RHEL/CentOS 7

Using Vagrant, pip or a Docker containers, Ansible can be installed in different ways on all major Linux distributions.

The server where Ansible engine is installed is called the controller. The servers which Ansible will manage are called managed hosts. Installing Ansible on Windows machine to use as a controller is not supported. However, Windows machines can be managed by Ansible.

In this post we will explore how to install Ansible on a RHEL/CentOS 7.5 box using the yum native package manager. We will also review the commons best practice to follow after the installation to make the Ansible administration as smooth as possible.

The environment used will be one controller (server1) and two managed nodes (server1 and server2) as shown below. Server1 will play the of a controller and a managed hosts at the same time.

  • Updating the System


Before going ahead with the installation, let’s update the the system on all hosts.

[user@server1 ~]$ sudo yum -y update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.fibergrid.in
* extras: mirrors.fibergrid.in
* updates: mirrors.fibergrid.in
Resolving Dependencies
--> Running transaction check
---> Package NetworkManager.x86_64 1:1.10.2-13.el7 will be updated
---> Package NetworkManager.x86_64 1:1.10.2-14.el7_5 will be an update
---> Package NetworkManager-libnm.x86_64 1:1.10.2-13.el7 will be updated
---> Package NetworkManager-libnm.x86_64 1:1.10.2-14.el7_5 will be an update
---> Package NetworkManager-team.x86_64 1:1.10.2-13.el7 will be updated
...

 

  • Checking the Prerequisites


  • On the controller node

On RHEL, the Ansible Engine repository needs to be initially added

$ sudo subscription-manager repos --enable rhel-7-server-ansible-2.6-rpms

 

On CentOS, first ensure that the CentOS 7 EPEL repository is installed

$ sudo yum install -y epel-release

 

  • On the managed nodes

To be able to use all available types of Ansible modules, Python needs to be installed on both the controller and any managed nodes

$ sudo yum install -y python2

 

  • Installing Ansible


Once the prerequisites are installed, we can go ahead with the Ansible installation

[user@server1 ~]$ sudo yum install -y ansible
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.fibergrid.in
* extras: mirrors.fibergrid.in
* updates: mirrors.fibergrid.in
Resolving Dependencies
--> Running transaction check
---> Package ansible.noarch 0:2.4.2.0-2.el7 will be installed
--> Processing Dependency: sshpass for package: ansible-2.4.2.0-2.el7.noarch
--> Processing Dependency: python2-jmespath for package: ansible-2.4.2.0-2.el7.noarch
--> Processing Dependency: python-six for package: ansible-2.4.2.0-2.el7.noarch
--> Processing Dependency: python-setuptools for package: ansible-2.4.2.0-2.el7.noarch
--> Processing Dependency: python-passlib for package: ansible-2.4.2.0-2.el7.noarch
--> Processing Dependency: python-paramiko for package: ansible-2.4.2.0-2.el7.noarch
--> Processing Dependency: python-jinja2 for package: ansible-2.4.2.0-2.el7.noarch
--> Processing Dependency: python-httplib2 for package: ansible-2.4.2.0-2.el7.noarch
--> Processing Dependency: python-cryptography for package: ansible-2.4.2.0-2.el7.noarch
...
Installed:
ansible.noarch 0:2.4.2.0-2.el7
Dependency Installed:
PyYAML.x86_64 0:3.10-11.el7                        libyaml.x86_64 0:0.1.4-11.el7_0                                     python-babel.noarch 0:0.9.6-8.el7
python-backports.x86_64 0:1.0-8.el7                python-backports-ssl_match_hostname.noarch 0:3.5.0.1-1.el7          python-cffi.x86_64 0:1.6.0-5.el7
python-enum34.noarch 0:1.0.4-1.el7                 python-httplib2.noarch 0:0.9.2-1.el7                                python-idna.noarch 0:2.4-1.el7
python-ipaddress.noarch 0:1.0.16-2.el7             python-jinja2.noarch 0:2.7.2-2.el7                                  python-markupsafe.x86_64 0:0.11-10.el7
python-paramiko.noarch 0:2.1.1-4.el7               python-passlib.noarch 0:1.6.5-2.el7                                 python-ply.noarch 0:3.4-11.el7
python-pycparser.noarch 0:2.14-1.el7               python-setuptools.noarch 0:0.9.8-7.el7                              python-six.noarch 0:1.9.0-2.el7
python2-cryptography.x86_64 0:1.7.2-2.el7          python2-jmespath.noarch 0:0.9.0-3.el7                               python2-pyasn1.noarch 0:0.1.9-7.el7
sshpass.x86_64 0:1.06-2.el7
Complete!

 

  • Checking Ansible Successful Installation


To check if Ansible has been installed successfully, you can run the ansible command

[user@server1 ~]$ ansible

 

Let’s check the Ansible version installed

[user@server1 ~]$ ansible --version
ansible 2.5.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
[user@server1 install]$

 

We may also run a single ad-hoc command against the Ansible controller by pinging the localhost

[user@server1 ~]$ ansible localhost -m ping
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

localhost | SUCCESS => {
	"changed": false,
	"ping": "pong"

[user@server1 ~]

 

If you encounter any issue ansible running this command, add the -vvvv flag to see more details:

[user@server1 ~]$ ansible localhost -m ping -vvvv
ansible 2.5.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
Parsed /etc/ansible/hosts inventory source with ini plugin
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

Loading callback plugin minimal of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/minimal.pyc
META: ran handlers
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/ping.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: user
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/user/.ansible/tmp/ansible-tmp-1531309517.72-85916721275337 `" && echo ansible-tmp-1531309517.72-85916721275337="` echo /home/user/.ansible/tmp/ansible-tmp-1531309517.72-85916721275337 `" ) && sleep 0'
<127.0.0.1> PUT /home/user/.ansible/tmp/ansible-local-725272R4Yo/tmpnl58GM TO /home/user/.ansible/tmp/ansible-tmp-1531309517.72-85916721275337/ping.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/user/.ansible/tmp/ansible-tmp-1531309517.72-85916721275337/ /home/user/.ansible/tmp/ansible-tmp-1531309517.72-85916721275337/ping.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /home/user/.ansible/tmp/ansible-tmp-1531309517.72-85916721275337/ping.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/user/.ansible/tmp/ansible-tmp-1531309517.72-85916721275337/ > /dev/null 2>&1 && sleep 0'
localhost | SUCCESS => {
	"changed": false,
	"invocation": {
		"module_args": {
			"data": "pong"
		}
	},
	"ping": "pong"
}
META: ran handlers
META: ran handlers
[user@server1 ~]$

 

That’s it for this post. In the next one, we will prepare the remote hosts to make them manageable from the controller host.

3 Comments

  1. Pingback: Ansible for the Impatient Beginners – vAdmin-Land

  2. Samirlag

    Good documentation steps for beginners

    Reply
    1. Mourad NAKIB (Post author)

      Thanks Samir. I’m happy it helped.

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *