November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Ansible

Ansible is an open source, powerful automation software for configuring, managing and deploying software applications on the nodes without any downtime just by using SSH. Unlike other alternatives, Ansible is installed on a single host, which can even be your local machine, and uses SSH to communicate with each remote host. This allows it to be incredibly fast at configuring new servers, as there are no prerequisite packages to be installed on each new server.

The controlling machine, where Ansible is installed and Nodes are managed by this controlling machine over SSH. The location of nodes are specified by controlling machine through its inventory. Ansible is agent-less, that means no need of any agent installation on remote nodes, so it means there are no any background daemons or programs are executing for Ansible, when it’s not managing any nodes.

Ansible is a free & open source Configuration and automation tool for UNIX like operating system. It is written in python and similar to Chef or Puppet but there is one difference and advantage of Ansible is that we don’t need to install any agent on the nodes. It uses SSH for making communication to its nodes.

Controller

The controlling machine (Ansible) deploys modules to nodes using SSH protocol and these modules are stored temporarily on remote nodes and communicate with the Ansible machine through a JSON connection over the standard output.
Installation

Installation is pretty easy, verify hostname and IP address before start. The dependancy packages for ansible can be found below.

Set EPEL warehouse
Ansible warehouse yum repository is not in default, so we need to use the following command to enable epel warehouse.

CENTOS 7

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

CENTOS 6

rpm -iUvh  http://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

CONTROL SERVER – CENTOS 7
APP1 CENTOS 7
APP2 CENTOS 6

[root@clusterserver1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.20 clusterserver1.rmohan.com clusterserver1 controlserver
192.168.1.21 clusterserver2.rmohan.com clusterserver2
192.168.1.63 cluster3.rmohan.com cluster3

Step 1: Set EPEL warehouse

yum install ansible

After the installation is complete, check ansible version:

ansible – release

[root@clusterserver1 ~]# ansible –version
ansible 1.9.4
configured module search path = None
[root@clusterserver1 ~]#

yum install ansible
yum install ntp

echo “*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1″ >> /var/spool/cron/root

Use ssh-copy-id command to copy the public key to Ansible node.

ssh-keygen  -t rsa -f ~/.ssh/id_rsa  -P ”

awk ‘{if ($0!~/'”$(hostname)”‘|localhost/)print $NF}’ /etc/hosts |xargs -i ssh-copy-id -i ~/.ssh/id_rsa.pub root@{}

To define the node list Ansible  Edit the hosts
Save and exit the file.
Hosts file examples are as follows:

[root@clusterserver1 ~]# cat /etc/ansible/hosts
[appserver]
192.168.1.21
192.168.1.63

try to run the server command Ansible
Use ping connectivity check ‘test-servers’ or ansible node.

ansible  -m ping ‘appserver’

[root@clusterserver1 ~]# ansible  -m ping ‘appserver’
192.168.1.21 | success >> {
“changed”: false,
“ping”: “pong”
}

192.168.1.63 | success >> {
“changed”: false,
“ping”: “pong”
}

Execute shell commands

Check Ansible node running time (uptime): Example 1

ansible -m command -a “uptime” ‘appserver’

[root@clusterserver1 ~]# ansible -m command -a “uptime” ‘appserver’
192.168.1.63 | success | rc=0 >>
23:01:50 up 12:09,  3 users,  load average: 0.00, 0.00, 0.00

192.168.1.21 | success | rc=0 >>
23:01:50 up 13:34,  2 users,  load average: 0.00, 0.01, 0.05

[root@clusterserver1 ~]#

Kernel version check node: Example 2

[root@clusterserver1 ~]# ansible -m command -a “uname -r” ‘appserver’
192.168.1.63 | success | rc=0 >>
2.6.32-573.7.1.el6.x86_64

192.168.1.21 | success | rc=0 >>
3.10.0-123.20.1.el7.x86_64

[root@clusterserver1 ~]# ansible -m command -a “`cat /etc/redhat-release`” ‘appserver’
192.168.1.63 | success | rc=0 >>
2.6.32-573.7.1.el6.x86_64

192.168.1.21 | success | rc=0 >>
3.10.0-123.20.1.el7.x86_64

[root@clusterserver1 ~]# ansible -m command -a “cat /etc/redhat-release” ‘appserver’
192.168.1.63 | success | rc=0 >>
CentOS release 6.7 (Final)

192.168.1.21 | success | rc=0 >>
CentOS Linux release 7.1.1503 (Core)

[root@clusterserver1 ~]# ansible -m command -a “python -c ‘import socket; print(socket.gethostbyname(socket.gethostname()))'” ‘appserver’
192.168.1.63 | success | rc=0 >>
192.168.1.63

192.168.1.21 | success | rc=0 >>
192.168.1.21

[root@clusterserver1 ~]# ansible -m command -a ‘hostname’  ‘appserver’
192.168.1.63 | success | rc=0 >>
cluster3.rmohan.com

192.168.1.21 | success | rc=0 >>
clusterserver2.rmohan.com

[root@clusterserver1 ~]#  ansible -m command -a “useradd mohan” ‘appserver’
192.168.1.21 | FAILED | rc=9 >>
useradd: user ‘mohan’ already exists

192.168.1.63 | success | rc=0 >>

[root@clusterserver1 ~]#ansible -m command -a “grep mohan /etc/passwd” ‘appserver’

[root@clusterserver1 ~]# ansible -m command -a “grep mohan /etc/passwd” ‘appserver’
192.168.1.63 | success | rc=0 >>
mohan:x:500:500::/home/mohan:/bin/bash

192.168.1.21 | success | rc=0 >>
mohan:x:1000:1000:mohan:/home/mohan:/bin/bash

[root@clusterserver1 ~]#ansible -m command -a “df -Th” ‘appserver’
[root@clusterserver1 ~]# ansible -m command -a “df -Th” ‘appserver’
192.168.1.21 | success | rc=0 >>
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        18G  1.4G   17G   8% /
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G  8.6M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               xfs       497M  167M  330M  34% /boot

192.168.1.63 | success | rc=0 >>
Filesystem           Type   Size  Used Avail Use% Mounted on
/dev/mapper/vg_cluster3-lv_root
ext4    50G  1.4G   46G   3% /
tmpfs                tmpfs  491M     0  491M   0% /dev/shm
/dev/sda1            ext4   477M   55M  398M  12% /boot
/dev/mapper/vg_cluster3-lv_home
ext4    47G   52M   45G   1% /home

[root@clusterserver1 ~]#

Lets install apache on  2 Nodes

[root@clusterserver1 ~]# cat test.yaml
– hosts: appserver
remote_user: root
tasks:
– yum: name=httpd state=latest

[root@clusterserver1 ~]# ansible-playbook test.yaml -f 10

PLAY [appserver] **************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.1.21]
ok: [192.168.1.63]

TASK: [yum name=httpd state=latest] *******************************************
changed: [192.168.1.63]
changed: [192.168.1.21]

PLAY RECAP ********************************************************************
192.168.1.21               : ok=2    changed=1    unreachable=0    failed=0
192.168.1.63               : ok=2    changed=1    unreachable=0    failed=0

[root@clusterserver1 ~]# cat test.yaml
– hosts: appserver
remote_user: root
tasks:
– yum: name=httpd state=latest
– name: httpd is running and enabled
service: name=httpd state=started enabled=yes

# target hostname or group name
– hosts: appserver
# define tasks
tasks:
# task name (any name you like)
– name: Test Task
# use file module to set the file state
file: path=/home/mohan/test.conf state=touch owner=mohan group=mohan mode=0600

[root@clusterserver1 ~]# ansible-playbook test.yaml -f 10

PLAY [appserver] **************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.1.63]
ok: [192.168.1.21]

TASK: [Test Task] *************************************************************
changed: [192.168.1.63]
changed: [192.168.1.21]

PLAY RECAP ********************************************************************
192.168.1.21               : ok=2    changed=1    unreachable=0    failed=0
192.168.1.63               : ok=2    changed=1    unreachable=0    failed=0

[root@clusterserver1 ~]# ansible appserver -m shell  -a “rpm -qa | egrep ‘vim-enhanced|wget|unzip'”
192.168.1.63 | success | rc=0 >>
wget-1.12-5.el6_6.1.x86_64

192.168.1.21 | FAILED | rc=1 >>

[root@clusterserver1 ~]#

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>