{"id":5406,"date":"2015-12-14T15:47:40","date_gmt":"2015-12-14T07:47:40","guid":{"rendered":"http:\/\/rmohan.com\/?p=5406"},"modified":"2015-12-14T15:47:40","modified_gmt":"2015-12-14T07:47:40","slug":"ansible","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5406","title":{"rendered":"Ansible"},"content":{"rendered":"<p>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.<\/p>\n<p>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\u2019s not managing any nodes.<\/p>\n<p>Ansible is a free &amp; 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\u2019t need to install any agent on the nodes. It uses SSH for making communication to its nodes.<\/p>\n<p>Controller<\/p>\n<p>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.<br \/>\nInstallation<\/p>\n<p>Installation is pretty easy, verify hostname and IP address before start. The dependancy packages for ansible can be found below.<\/p>\n<p>Set EPEL warehouse<br \/>\nAnsible warehouse yum repository is not in default, so we need to use the following command to enable epel warehouse.<\/p>\n<p>CENTOS 7<\/p>\n<p>rpm -iUvh http:\/\/dl.fedoraproject.org\/pub\/epel\/7\/x86_64\/e\/epel-release-7-5.noarch.rpm<\/p>\n<p>CENTOS 6<\/p>\n<p>rpm -iUvh\u00a0 http:\/\/dl.fedoraproject.org\/pub\/epel\/epel-release-latest-6.noarch.rpm<\/p>\n<p>CONTROL SERVER &#8211; CENTOS 7<br \/>\nAPP1 CENTOS 7<br \/>\nAPP2 CENTOS 6<\/p>\n<p>[root@clusterserver1 ~]# cat \/etc\/hosts<br \/>\n127.0.0.1\u00a0\u00a0 localhost localhost.localdomain localhost4 localhost4.localdomain4<br \/>\n::1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 localhost localhost.localdomain localhost6 localhost6.localdomain6<br \/>\n192.168.1.20 clusterserver1.rmohan.com clusterserver1 controlserver<br \/>\n192.168.1.21 clusterserver2.rmohan.com clusterserver2<br \/>\n192.168.1.63 cluster3.rmohan.com cluster3<\/p>\n<p>Step 1: Set EPEL warehouse<\/p>\n<p>yum install ansible<\/p>\n<p>After the installation is complete, check ansible version:<\/p>\n<p>ansible &#8211; release<\/p>\n<p>[root@clusterserver1 ~]# ansible &#8211;version<br \/>\nansible 1.9.4<br \/>\nconfigured module search path = None<br \/>\n[root@clusterserver1 ~]#<\/p>\n<p>yum install ansible<br \/>\nyum install ntp<\/p>\n<p>echo &#8220;*\/5 * * * * \/usr\/sbin\/ntpdate pool.ntp.org &gt;\/dev\/null 2&gt;&amp;1&#8243; &gt;&gt; \/var\/spool\/cron\/root<\/p>\n<p>Use ssh-copy-id command to copy the public key to Ansible node.<\/p>\n<p>ssh-keygen\u00a0 -t rsa -f ~\/.ssh\/id_rsa\u00a0 -P &#8221;<\/p>\n<p>awk &#8216;{if ($0!~\/'&#8221;$(hostname)&#8221;&#8216;|localhost\/)print $NF}&#8217; \/etc\/hosts |xargs -i ssh-copy-id -i ~\/.ssh\/id_rsa.pub root@{}<\/p>\n<p>To define the node list Ansible\u00a0 Edit the hosts<br \/>\nSave and exit the file.<br \/>\nHosts file examples are as follows:<\/p>\n<p>[root@clusterserver1 ~]# cat \/etc\/ansible\/hosts<br \/>\n[appserver]<br \/>\n192.168.1.21<br \/>\n192.168.1.63<\/p>\n<p>try to run the server command Ansible<br \/>\nUse ping connectivity check &#8216;test-servers&#8217; or ansible node.<\/p>\n<p>ansible\u00a0 -m ping &#8216;appserver&#8217;<\/p>\n<p>[root@clusterserver1 ~]# ansible\u00a0 -m ping &#8216;appserver&#8217;<br \/>\n192.168.1.21 | success &gt;&gt; {<br \/>\n&#8220;changed&#8221;: false,<br \/>\n&#8220;ping&#8221;: &#8220;pong&#8221;<br \/>\n}<\/p>\n<p>192.168.1.63 | success &gt;&gt; {<br \/>\n&#8220;changed&#8221;: false,<br \/>\n&#8220;ping&#8221;: &#8220;pong&#8221;<br \/>\n}<\/p>\n<p>Execute shell commands<\/p>\n<p>Check Ansible node running time (uptime): Example 1<\/p>\n<p>ansible -m command -a &#8220;uptime&#8221; &#8216;appserver&#8217;<\/p>\n<p>[root@clusterserver1 ~]# ansible -m command -a &#8220;uptime&#8221; &#8216;appserver&#8217;<br \/>\n192.168.1.63 | success | rc=0 &gt;&gt;<br \/>\n23:01:50 up 12:09,\u00a0 3 users,\u00a0 load average: 0.00, 0.00, 0.00<\/p>\n<p>192.168.1.21 | success | rc=0 &gt;&gt;<br \/>\n23:01:50 up 13:34,\u00a0 2 users,\u00a0 load average: 0.00, 0.01, 0.05<\/p>\n<p>[root@clusterserver1 ~]#<\/p>\n<p>Kernel version check node: Example 2<\/p>\n<p>[root@clusterserver1 ~]# ansible -m command -a &#8220;uname -r&#8221; &#8216;appserver&#8217;<br \/>\n192.168.1.63 | success | rc=0 &gt;&gt;<br \/>\n2.6.32-573.7.1.el6.x86_64<\/p>\n<p>192.168.1.21 | success | rc=0 &gt;&gt;<br \/>\n3.10.0-123.20.1.el7.x86_64<\/p>\n<p>[root@clusterserver1 ~]# ansible -m command -a &#8220;`cat \/etc\/redhat-release`&#8221; &#8216;appserver&#8217;<br \/>\n192.168.1.63 | success | rc=0 &gt;&gt;<br \/>\n2.6.32-573.7.1.el6.x86_64<\/p>\n<p>192.168.1.21 | success | rc=0 &gt;&gt;<br \/>\n3.10.0-123.20.1.el7.x86_64<\/p>\n<p>[root@clusterserver1 ~]# ansible -m command -a &#8220;cat \/etc\/redhat-release&#8221; &#8216;appserver&#8217;<br \/>\n192.168.1.63 | success | rc=0 &gt;&gt;<br \/>\nCentOS release 6.7 (Final)<\/p>\n<p>192.168.1.21 | success | rc=0 &gt;&gt;<br \/>\nCentOS Linux release 7.1.1503 (Core)<\/p>\n<p>[root@clusterserver1 ~]# ansible -m command -a &#8220;python -c &#8216;import socket; print(socket.gethostbyname(socket.gethostname()))'&#8221; &#8216;appserver&#8217;<br \/>\n192.168.1.63 | success | rc=0 &gt;&gt;<br \/>\n192.168.1.63<\/p>\n<p>192.168.1.21 | success | rc=0 &gt;&gt;<br \/>\n192.168.1.21<\/p>\n<p>[root@clusterserver1 ~]# ansible -m command -a &#8216;hostname&#8217;\u00a0 &#8216;appserver&#8217;<br \/>\n192.168.1.63 | success | rc=0 &gt;&gt;<br \/>\ncluster3.rmohan.com<\/p>\n<p>192.168.1.21 | success | rc=0 &gt;&gt;<br \/>\nclusterserver2.rmohan.com<\/p>\n<p>[root@clusterserver1 ~]#\u00a0 ansible -m command -a &#8220;useradd mohan&#8221; &#8216;appserver&#8217;<br \/>\n192.168.1.21 | FAILED | rc=9 &gt;&gt;<br \/>\nuseradd: user &#8216;mohan&#8217; already exists<\/p>\n<p>192.168.1.63 | success | rc=0 &gt;&gt;<\/p>\n<p>[root@clusterserver1 ~]#ansible -m command -a &#8220;grep mohan \/etc\/passwd&#8221; &#8216;appserver&#8217;<\/p>\n<p>[root@clusterserver1 ~]# ansible -m command -a &#8220;grep mohan \/etc\/passwd&#8221; &#8216;appserver&#8217;<br \/>\n192.168.1.63 | success | rc=0 &gt;&gt;<br \/>\nmohan:x:500:500::\/home\/mohan:\/bin\/bash<\/p>\n<p>192.168.1.21 | success | rc=0 &gt;&gt;<br \/>\nmohan:x:1000:1000:mohan:\/home\/mohan:\/bin\/bash<\/p>\n<p>[root@clusterserver1 ~]#ansible -m command -a &#8220;df -Th&#8221; &#8216;appserver&#8217;<br \/>\n[root@clusterserver1 ~]# ansible -m command -a &#8220;df -Th&#8221; &#8216;appserver&#8217;<br \/>\n192.168.1.21 | success | rc=0 &gt;&gt;<br \/>\nFilesystem\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Type\u00a0\u00a0\u00a0\u00a0\u00a0 Size\u00a0 Used Avail Use% Mounted on<br \/>\n\/dev\/mapper\/centos-root xfs\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 18G\u00a0 1.4G\u00a0\u00a0 17G\u00a0\u00a0 8% \/<br \/>\ndevtmpfs\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 devtmpfs\u00a0 1.9G\u00a0\u00a0\u00a0\u00a0 0\u00a0 1.9G\u00a0\u00a0 0% \/dev<br \/>\ntmpfs\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tmpfs\u00a0\u00a0\u00a0\u00a0 1.9G\u00a0\u00a0\u00a0\u00a0 0\u00a0 1.9G\u00a0\u00a0 0% \/dev\/shm<br \/>\ntmpfs\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tmpfs\u00a0\u00a0\u00a0\u00a0 1.9G\u00a0 8.6M\u00a0 1.9G\u00a0\u00a0 1% \/run<br \/>\ntmpfs\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tmpfs\u00a0\u00a0\u00a0\u00a0 1.9G\u00a0\u00a0\u00a0\u00a0 0\u00a0 1.9G\u00a0\u00a0 0% \/sys\/fs\/cgroup<br \/>\n\/dev\/sda1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 xfs\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 497M\u00a0 167M\u00a0 330M\u00a0 34% \/boot<\/p>\n<p>192.168.1.63 | success | rc=0 &gt;&gt;<br \/>\nFilesystem\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Type\u00a0\u00a0 Size\u00a0 Used Avail Use% Mounted on<br \/>\n\/dev\/mapper\/vg_cluster3-lv_root<br \/>\next4\u00a0\u00a0\u00a0 50G\u00a0 1.4G\u00a0\u00a0 46G\u00a0\u00a0 3% \/<br \/>\ntmpfs\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tmpfs\u00a0 491M\u00a0\u00a0\u00a0\u00a0 0\u00a0 491M\u00a0\u00a0 0% \/dev\/shm<br \/>\n\/dev\/sda1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ext4\u00a0\u00a0 477M\u00a0\u00a0 55M\u00a0 398M\u00a0 12% \/boot<br \/>\n\/dev\/mapper\/vg_cluster3-lv_home<br \/>\next4\u00a0\u00a0\u00a0 47G\u00a0\u00a0 52M\u00a0\u00a0 45G\u00a0\u00a0 1% \/home<\/p>\n<p>[root@clusterserver1 ~]#<\/p>\n<p>Lets install apache on\u00a0 2 Nodes<\/p>\n<p>[root@clusterserver1 ~]# cat test.yaml<br \/>\n&#8211; hosts: appserver<br \/>\nremote_user: root<br \/>\ntasks:<br \/>\n&#8211; yum: name=httpd state=latest<\/p>\n<p>[root@clusterserver1 ~]# ansible-playbook test.yaml -f 10<\/p>\n<p>PLAY [appserver] **************************************************************<\/p>\n<p>GATHERING FACTS ***************************************************************<br \/>\nok: [192.168.1.21]<br \/>\nok: [192.168.1.63]<\/p>\n<p>TASK: [yum name=httpd state=latest] *******************************************<br \/>\nchanged: [192.168.1.63]<br \/>\nchanged: [192.168.1.21]<\/p>\n<p>PLAY RECAP ********************************************************************<br \/>\n192.168.1.21\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : ok=2\u00a0\u00a0\u00a0 changed=1\u00a0\u00a0\u00a0 unreachable=0\u00a0\u00a0\u00a0 failed=0<br \/>\n192.168.1.63\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : ok=2\u00a0\u00a0\u00a0 changed=1\u00a0\u00a0\u00a0 unreachable=0\u00a0\u00a0\u00a0 failed=0<\/p>\n<p>[root@clusterserver1 ~]# cat test.yaml<br \/>\n&#8211; hosts: appserver<br \/>\nremote_user: root<br \/>\ntasks:<br \/>\n&#8211; yum: name=httpd state=latest<br \/>\n&#8211; name: httpd is running and enabled<br \/>\nservice: name=httpd state=started enabled=yes<\/p>\n<p># target hostname or group name<br \/>\n&#8211; hosts: appserver<br \/>\n# define tasks<br \/>\ntasks:<br \/>\n# task name (any name you like)<br \/>\n&#8211; name: Test Task<br \/>\n# use file module to set the file state<br \/>\nfile: path=\/home\/mohan\/test.conf state=touch owner=mohan group=mohan mode=0600<\/p>\n<p>[root@clusterserver1 ~]# ansible-playbook test.yaml -f 10<\/p>\n<p>PLAY [appserver] **************************************************************<\/p>\n<p>GATHERING FACTS ***************************************************************<br \/>\nok: [192.168.1.63]<br \/>\nok: [192.168.1.21]<\/p>\n<p>TASK: [Test Task] *************************************************************<br \/>\nchanged: [192.168.1.63]<br \/>\nchanged: [192.168.1.21]<\/p>\n<p>PLAY RECAP ********************************************************************<br \/>\n192.168.1.21\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : ok=2\u00a0\u00a0\u00a0 changed=1\u00a0\u00a0\u00a0 unreachable=0\u00a0\u00a0\u00a0 failed=0<br \/>\n192.168.1.63\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : ok=2\u00a0\u00a0\u00a0 changed=1\u00a0\u00a0\u00a0 unreachable=0\u00a0\u00a0\u00a0 failed=0<\/p>\n<p>[root@clusterserver1 ~]# ansible appserver -m shell\u00a0 -a &#8220;rpm -qa | egrep &#8216;vim-enhanced|wget|unzip'&#8221;<br \/>\n192.168.1.63 | success | rc=0 &gt;&gt;<br \/>\nwget-1.12-5.el6_6.1.x86_64<\/p>\n<p>192.168.1.21 | FAILED | rc=1 &gt;&gt;<\/p>\n<p>[root@clusterserver1 ~]#<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,29],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5406"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5406"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5406\/revisions"}],"predecessor-version":[{"id":5407,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5406\/revisions\/5407"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}