Monday, May 23, 2016

How to work with Ansible (Automation tool) on CentOS 7


Ansible is free tool to automation tool for Linux hosts. Its useful for the environment where you have lots of linux hosts/servers to manage and maintain. Lets get started with it,

I have tested the below mentioned steps on CentOS 7:

First of all, we are going to need epel on CentOS to install ansible:



[root@vm2 ~]# uname -r
3.10.0-123.el7.x86_64


[root@vm2 ~]# cat /etc/redhat-release

CentOS Linux release 7.0.1406 (Core)


[root@vm2 ~]# yum install epel-release

Now after installing epel, lets install ansible:

[root@vm2 ~]# yum install ansible

After installation is done, test it out:

[root@vm2 ~]# ansible --version

ansible 2.0.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

Now, ansible works on ssh keys. So just generate one if you don't have it.

[root@vm2 ~]# ssh-keygen
<You can have a secure password, or just keep it blank also should work>

Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.

 Now Copy the ssh key to remote hosts:

[root@vm2 ~]# ssh-copy-id 192.168.0.81

[root@vm2 ~]# ssh-copy-id 192.168.1.222

Now Add your hosts/servers in the below mentioned ansible file:

[root@vm2 ~]# vim /etc/ansible/hosts

[servers]
192.168.0.81
192.168.1.222

Now, Save & Exit.

Its time to test ansible with ping module:

[root@vm2 ~]#  ansible -m ping 'servers'
192.168.0.81 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
192.168.1.222 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Looks good.

Lets try some other command:

[root@vm2 ~]# ansible -m command -a 'rpm -qa kernel' 'servers'
192.168.0.81 | SUCCESS | rc=0 >>
kernel-3.10.0-327.18.2.el7.x86_64
kernel-3.10.0-327.10.1.el7.x86_64
kernel-3.10.0-327.el7.x86_64
kernel-3.10.0-327.13.1.el7.x86_64

192.168.1.222 | SUCCESS | rc=0 >>
kernel-2.6.32-504.el6.x86_64
kernel-2.6.32-573.7.1.el6.x86_64

Yeah, So Now you are able to fetch these information in single command.


[root@vm2 ~]# ansible -m command -a 'grep CPU /proc/cpuinfo' 'test-servers'
192.168.0.81 | SUCCESS | rc=0 >>
model name    : Intel(R) Core(TM) i3-4130 CPU @ 3.40GHz
model name    : Intel(R) Core(TM) i3-4130 CPU @ 3.40GHz
model name    : Intel(R) Core(TM) i3-4130 CPU @ 3.40GHz
model name    : Intel(R) Core(TM) i3-4130 CPU @ 3.40GHz

192.168.1.222 | SUCCESS | rc=0 >>
model name    : Intel(R) Core(TM) i3-3240 CPU @ 3.40GHz
model name    : Intel(R) Core(TM) i3-3240 CPU @ 3.40GHz
model name    : Intel(R) Core(TM) i3-3240 CPU @ 3.40GHz
model name    : Intel(R) Core(TM) i3-3240 CPU @ 3.40GHz



[root@vm2 ~]# ansible -m command -a 'free -m' 'test-servers'
192.168.1.222 | SUCCESS | rc=0 >>
             total       used       free     shared    buffers     cached
Mem:          7693       5845       1847        187          7        449
-/+ buffers/cache:       5388       2304
Swap:         7999        684       7315

192.168.0.81 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:          11726        6426         390         393        4909        4568
Swap:         16383         130       16253



[root@vm2 ~]# ansible -m command -a 'fdisk -l /dev/sda' 'test-servers'
192.168.0.81 | SUCCESS | rc=0 >>

Disk /dev/sda: 500.1 GB, 500107862016 bytes, 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0000f988

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1230847      614400   83  Linux
/dev/sda2         1230848   525518847   262144000   83  Linux
/dev/sda3       525518848   559073279    16777216   82  Linux swap / Solaris
/dev/sda4       559073280   976773167   208849944    5  Extended
/dev/sda5       559075328   976773167   208848920   83  Linux

192.168.1.222 | SUCCESS | rc=0 >>

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa34fa34f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          32      256000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              32        7681    61440000   83  Linux
/dev/sda3            7681        8701     8192000   82  Linux swap / Solaris
/dev/sda4            8701       60802   418497560    5  Extended
/dev/sda5            8701       60802   418496512   83  Linux


Awesome. You can Also Create/Delete user accounts:

[root@vm2 ~]# ansible -m command -a "useradd spiderman" 'test-servers'
192.168.1.222 | SUCCESS | rc=0 >>


192.168.0.81 | SUCCESS | rc=0 >>




[root@vm2 ~]# ansible -m command -a "grep spiderman /etc/passwd " 'test-servers'
192.168.0.81 | SUCCESS | rc=0 >>
spiderman:x:1002:1002::/home/spiderman:/bin/bash

192.168.1.222 | SUCCESS | rc=0 >>
spiderman:x:505:506::/home/spiderman:/bin/bash



[root@vm2 ~]# ansible -m command -a "userdel -r spiderman" 'test-servers'
192.168.0.81 | SUCCESS | rc=0 >>


192.168.1.222 | SUCCESS | rc=0 >>




Hope this helped you.

Thanks ^_^








No comments:

Post a Comment