Install and Configure Ansible on Ubuntu 16.04

in this article we will learn how to install and configure ansible in ubuntu 16.02, Ansible is a agent less configuration management tool which help us to manage large number of client machines easily, ansible running and communicates over ssh protocol, So it's doesn't need to install any software on client side.


Ansible Configuration management tool

Scenario : 

master server : 10.80.253.11 / master
client server 1 : 10.80.253.12 / slave1
client server 2 :  10.80.253.13 / slave2
you can enter machine ip and name in /etc/host file. 

Installing the Ansible on Ubuntu 16.04 (we will install ansible on Master server only).
To install ansible you must have to configure PPA (Personal Package Archive) on your system for latest version of ansible.

$sudo apt-add-repository ppa:ansible/ansible
Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.
http://ansible.com/
 More info: https://launchpad.net/~ansible/+archive/ubuntu/ansible
Press [ENTER] to continue or ctrl-c to cancel adding it
gpg: keyring `/tmp/tmpb9i7viln/secring.gpg' created
gpg: keyring `/tmp/tmpb9i7viln/pubring.gpg' created
gpg: requesting key 7BB9C367 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpb9i7viln/trustdb.gpg: trustdb created
gpg: key 7BB9C367: public key "Launchpad PPA for Ansible, Inc." imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK

After add PPA to server you will have to update server using below command.
$sudo apt-get update
Get:1 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial InRelease [18.0 kB]
Hit:2 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Get:3 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Get:4 http://security.ubuntu.com/ubuntu xenial-security InRelease [107 kB]
Get:5 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial/main amd64 Packages [536 B]
Get:6 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [767 kB]
Get:8 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial/main i386 Packages [536 B]
Get:9 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial/main Translation-en [344 B]


Finaly install ansible using below command.
$sudo apt-get install ansible

Finding Ansible version
# ansible --version
ansible 2.5.1
  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/dist-packages/ansible
  executable location = /usr/bin/ansible
python version = 2.7.12 (default, Nov 20 2017, 18:23:56) [GCC 5.4.0 20160609]

Ansible Hosts Configuration : 
ansible requires information of remote machine name and ip addresses, this all information are stored in host file. default path of file is  /etc/ansible/hosts. we can edit it or create new one also. append ip or name of server in host file. 
 $vim /etc/ansible/hosts
[servers]
slave1 
slave2

and save the file.


we need to setup ssh keys and copy in all client machine. so they can communicates each other without asking password.

Click here To configure password less login between server and client using ssh.


Executing the Simple Ansible Commands : 
First we will check ping command, and run below command  which ping client machine which we have defined in /etc/ansible/host file.

root@master:~# ansible -m ping servers
slave2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
slave1 | SUCCESS => {
    "changed": false,
    "ping": "pong"

}


Success means all client machines can communicates with server.


We can use " shell module to run a terminal command from the Anisble remote hosts and retrive the results.
Let's find out memory usage of all client machine. run below command to check memory usage of all ansible clinet machine.

$ ansible -m shell -a "free -m" servers

slave1 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:            990         155         170          14         663         648
Swap:          1905           0        1905

slave2 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:            990         159         170          14         660         646
Swap:          1905           0        1905

You can see the output of memory utilization.


To check disk usage of all out ansible client with below command.
$ ansible -m shell -a "df -h" servers

slave1 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
udev            474M     0  474M   0% /dev
tmpfs           100M   11M   89M  11% /run
/dev/sda1       5.4G  3.9G  1.2G  77% /
tmpfs           496M  188K  495M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           496M     0  496M   0% /sys/fs/cgroup
/dev/sda5       268M   62M  189M  25% /boot
tmpfs           100M   32K   99M   1% /run/user/108
tmpfs           100M     0  100M   0% /run/user/0

slave2 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
udev            474M     0  474M   0% /dev
tmpfs           100M   11M   89M  11% /run
/dev/sda1       5.4G  3.9G  1.3G  77% /
tmpfs           496M  188K  495M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           496M     0  496M   0% /sys/fs/cgroup
/dev/sda5       268M   62M  189M  25% /boot
tmpfs           100M   32K   99M   1% /run/user/108
tmpfs           100M     0  100M   0% /run/user/0


You can check the any service status or restart any service using below command on all ansible client.

$ansible -m shell -a "service sshd restart " servers
slave2 | SUCCESS | rc=0 >>

slave1 | SUCCESS | rc=0 >>


To check uptime of all ansible clients : 

$ ansible -m shell -a "uptime" servers

slave1 | SUCCESS | rc=0 >>
 13:19:49 up 10 days, 23:28,  1 user,  load average: 0.00, 0.00, 0.00

slave2 | SUCCESS | rc=0 >>
 13:19:46 up 10 days, 23:28,  1 user,  load average: 0.00, 0.00, 0.00


To check process of any ansible client :

$ ansible -m shell -a "ps -aux | grep ssh " slave1

slave1 | SUCCESS | rc=0 >>
root     10307  0.0  0.5  65512  5388 ?        Ss   13:11   0:00 /usr/sbin/sshd -D
root     10414  0.2  0.7  95196  7280 ?        Ss   13:21   0:00 sshd: root@pts/8
root     10519  0.0  0.0   4508   716 pts/8    S+   13:21   0:00 /bin/sh -c ps -aux | grep ssh
root     10521  0.0  0.0  21292   920 pts/8    S+   13:21   0:00 grep ssh

here we have checked process of ssh of slave1 server.


To check any files from remote ansible client : 

$ansible -m shell -a "cat /etc/timezone" slave1

slave1 | SUCCESS | rc=0 >>
Asia/Kolkata


To check CPU info of client machine.

$ansible -m shell -a "cat /proc/cpuinfo" slave1

slave1 | SUCCESS | rc=0 >>
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 63
model name      : Intel(R) Xeon(R) CPU E5-2609 v3 @ 1.90GHz
stepping        : 2
cpu MHz         : 1899.999
cache size      : 15360 KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 15
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq monitor ssse3 cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm fsgsbase avx2 invpcid
bugs            :
bogomips        : 3799.99
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical, 48 bits virtual
power management:


So in this article we have learn how to configured ansible server and clients. So in next article i will explain you to how to create and run ansible playbooks. 



Thanks,
Vishal


Vishal Vyas [Linux Guru]

Welcome to Linux Guru! Hello, friends. My name is Vishal Vyas, and I am a DevOps engineer with expertise in Linux and Cloud Computing. I am also a Certified Kubernetes Administrator with over 12 years of experience in the IT field, working with various technologies. Through this blog, I aim to share my technical knowledge on Linux, AWS, DevOps, and web technologies. I will be posting about what I have learned from the latest web technologies and similar topics.

Post a Comment

If you have any doubts, Please let me know

Previous Post Next Post