Installation and configuration of Ansible

We can install the Ansible in Controller machine by using one of the below method

  • Using YUM repository
  • Using Python package manager PIP
  • From Source code

Here, we are going to use YUM method,

Ansible Control machine hostname: centos7

Managed hosts: centos6web and centos6db

[root@centos7 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.17.136 centos7
192.168.17.137 centos6db
192.168.0.30 centos6web
[root@centos7 ~]#

Enable the EPEL repo by installing the below RPM,

rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm

# yum install ansible

–> Finished Dependency Resolution

Dependencies Resolved

============================
Package Arch Version Repository Size
============================
Installing:
ansible noarch 2.4.1.0-1.el7 extras 7.6 M
Installing for dependencies:
python-cffi x86_64 1.6.0-5.el7 base 218 k
python-enum34 noarch 1.0.4-1.el7 base 52 k
python-idna noarch 2.4-1.el7 base 94 k
python-ipaddress noarch 1.0.16-2.el7 base 34 k
python-passlib noarch 1.6.5-2.el7 extras 488 k
python-ply noarch 3.4-11.el7 base 123 k
python-pycparser noarch 2.14-1.el7 base 104 k
python2-cryptography x86_64 1.7.2-1.el7_4.1 updates 502 k
python2-jmespath noarch 0.9.0-3.el7 extras 39 k
Updating for dependencies:
openssl x86_64 1:1.0.2k-8.el7 base 492 k
openssl-libs x86_64 1:1.0.2k-8.el7 base 1.2 M

Transaction Summary
==============================
Install 1 Package (+9 Dependent packages)
Upgrade ( 2 Dependent packages)

Total download size: 11 M
Is this ok [y/d/N]: y

Give ‘Y’

Now Ansible has been installed, we can verify the installed version as below,

# ansible –version

[root@centos7 ~]# ansible –version
ansible 2.4.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/root/.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, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623]
[root@centos7 ~]#

Configuring Ansible:

We can configure the Ansible using ansible.cfg file and location of the ansible.cfg on the control machine decides the priority.

Ansible will search for the ansible.cfg file in the below order.

1) ansible.cfg defined by environment variable – $ANSIBLE_CONFIG

2) ansible.cfg presents in current working directory

3) ~/.ansible.cfg presents in user’s home directory (note: it is hidden file)

4) /etc/ansible/ansible.cfg – this file is provided by ansible package.

Highest priority goes to environment variable – $ANSIBLE_CONFIG and least priority is for /etc/ansible/ansible.cfg.

we can identify current config file which is being referred to ansible by running the below command,

#ansible –version

[root@centos7 ~]# ansible –version
ansible 2.4.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/root/.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, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
[root@centos7 ~]#

Let create an empty ansible.cfg in current working directory and see the difference as below,

[root@centos7 ~]# pwd
/root
[root@centos7 ~]# touch ansible.cfg
[root@centos7 ~]# ansible –version
ansible 2.4.1.0
config file = /root/ansible.cfg
configured module search path = [u’/root/.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, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
[root@centos7 ~]#

Now export the environment variable ANSIBLE_CONFIG as below,

[root@centos7 ~]# echo $ANSIBLE_CONFIG

[root@centos7 ~]# touch /tmp/ansible.cfg
[root@centos7 ~]#export ANSIBLE_CONFIG=/tmp/ansible.cfg

[root@centos7 ~]# ansible –version
ansible 2.4.1.0
config file = /tmp/ansible.cfg
configured module search path = [u’/root/.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, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
[root@centos7 ~]#