{"id":7626,"date":"2018-06-23T09:47:26","date_gmt":"2018-06-23T01:47:26","guid":{"rendered":"http:\/\/rmohan.com\/?p=7626"},"modified":"2018-06-23T09:47:26","modified_gmt":"2018-06-23T01:47:26","slug":"ansible-trail","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7626","title":{"rendered":"ansible trail"},"content":{"rendered":"<p>##### Steps for deployment of Ansible on CentOS 7<\/p>\n<p>##### Dependency Tasks<\/p>\n<p>### Install EPEL<br \/>\nsudo yum install epel-release<\/p>\n<p>### Install pending updates<br \/>\nsudo yum -y update<\/p>\n<p>##### Install Ansible<\/p>\n<p>### Install Ansible<br \/>\nsudo yum -y install ansible<\/p>\n<p>### Verify the Version<br \/>\nansible &#8211;version<\/p>\n<p>[db]<br \/>\nnode1.rmohan.com<br \/>\n[app]<br \/>\nnode2.rmohan.com<br \/>\n[db]<br \/>\nnode3.rmohan.com<\/p>\n<p>ansible all &#8211;list-hosts<br \/>\nansible db &#8211;list-hosts<br \/>\nansible db -m ping<\/p>\n<p>connect with password authentication, it&#8217;s possible to do with &#8220;k&#8221;<br \/>\nansible db -k -m command -a &#8220;uptime&#8221;<\/p>\n<p>ansible db -k -m command -a &#8220;cat \/etc\/shadow&#8221; -b &#8211;ask-become-pass<\/p>\n<p>other user&#8217;s priviledge except root, specify the option &#8220;&#8211;become-user=xxx&#8221;.<br \/>\nIf you&#8217;d like to use another way to use priviledge except sudo (su | pbrun | pfexec | runas), specify the option &#8220;&#8211;become-method=xxx&#8221;.<\/p>\n<p>ansible db -m ping<br \/>\nansible db -m command -a uptime<br \/>\nansible db -a &#8220;tail \/var\/log\/dmesg&#8221;<\/p>\n<p>ansible -m ping db<br \/>\nansible -m ping -all<br \/>\nansible -m command -a &#8220;df -h&#8221; db<br \/>\nansible -m command -a &#8220;free -mt&#8221; db<br \/>\nansible -m command -a &#8220;uptime&#8221; all<br \/>\nansible -m command -a &#8220;arch&#8221; all<br \/>\nansible -m shell -a &#8220;hostname&#8221; all<br \/>\nansible -m command -a &#8220;df -h&#8221; db &gt; \/tmp\/df_outpur.txt<\/p>\n<p>ansible all -a &#8220;echo hello world&#8221;<br \/>\nansible all -m ping<br \/>\nansible db -m ping<br \/>\nansible db -m setup -l node-1<br \/>\nansible db -m command -a &#8220;hostname&#8221;<br \/>\nansible db -m command -a &#8220;hostname&#8221; -o<br \/>\nansible db -m command -a &#8220;uptime&#8221;<br \/>\nansible db -m shell -a &#8216;echo $TERM&#8217;<br \/>\nansible db -b -m yum -a &#8220;name=httpd state=present&#8221;<\/p>\n<p>ansible web -b -m service -a &#8220;name=httpd state=started&#8221;<br \/>\nansible web -b -m service -a &#8220;name=httpd state=stopped&#8221;<\/p>\n<p>ansible web -a &#8220;\/sbin\/reboot&#8221; -f 10<\/p>\n<p>Adhoc Commands<\/p>\n<p>ansible web -a &#8220;yum update -y&#8221;<br \/>\nansible app -a &#8220;yum -y install tomcat&#8221;<br \/>\nansible app -a &#8220;service tomcat status&#8221;<br \/>\nansible app -a &#8220;service tomcat start&#8221;<br \/>\nansible app -a &#8220;yum -y install curl wget&#8221;<br \/>\nansible app -a &#8220;curl web&#8221;<br \/>\nansible app -a &#8220;bash -c &#8216;curl -k https:\/\/github.com\/opstree-ansible\/ansible-training\/blob\/master\/attendees\/exercise\/application\/sample.war &gt; \/var\/lib\/tomcat\/webapps\/sample.war'&#8221;<br \/>\nansible app -a &#8220;service tomcat restart&#8221;<br \/>\nansible app -a &#8220;curl node2.rmohan.com:8080\/sample\/&#8221;<\/p>\n<p>ansible centos -m copy -a &#8220;src=test.txt<br \/>\nansible centos -m copy -a &#8220;src=test.txt dest=\/tmp\/test.txt&#8221;<br \/>\nansible centos -m yum -a &#8220;install libselinux-python&#8221;<br \/>\nansible centos -m copy -a &#8220;src=test.txt dest=\/tmp\/test.txt&#8221;<\/p>\n<p>vi playbook_sample.yml<br \/>\n# target hostname or group name<br \/>\n&#8211; hosts: web<br \/>\n# define tasks<br \/>\ntasks:<br \/>\n# task name (any name you like)<br \/>\n&#8211; name: Test file<br \/>\n# use file module to set the file state<br \/>\nfile: path=\/tmp\/test.conf state=touch owner=root group=root mode=0600<\/p>\n<p>run Playbook<br \/>\nansible-playbook playbook_sample.yml<\/p>\n<p>ansible web1 -m command -a &#8220;ls -l \/tmp\/&#8221;<\/p>\n<p>[root@controller test]# ansible web -m command -a &#8220;ls -l \/tmp\/test.conf&#8221;<br \/>\nnode1.rmohan.com | SUCCESS | rc=0 &gt;&gt;<br \/>\n-rw&#8212;&#8212;- 1 root root 0 Mar 31 20:29 \/tmp\/test.conf<\/p>\n<p>create a Playbook which Apache httpd is installed and running.<br \/>\nvi playbook_sample2.yml<br \/>\n&#8211; hosts: web<br \/>\n# use priviledge (default : root)<br \/>\nbecome: yes<br \/>\n# the way to use priviledge<br \/>\nbecome_method: sudo<br \/>\n# define tasks<br \/>\ntasks:<br \/>\n&#8211; name: httpd is installed<br \/>\nyum: name=httpd state=installed<br \/>\n&#8211; name: httpd is running and enabled<br \/>\nservice: name=httpd state=started enabled=yes<\/p>\n<p>ansible-playbook -v playbook_sample2.yml &#8211;ask-become-pass<\/p>\n<p>ansible web -m shell -a &#8220;\/bin\/systemctl status httpd | head -3&#8221; -b &#8211;ask-become-pass<\/p>\n<p>[root@controller test]# ansible web -m shell -a &#8220;\/bin\/systemctl status httpd | head -3&#8221; -b &#8211;ask-become-pass<br \/>\nSUDO password:<br \/>\nnode1.rmohan.com | SUCCESS | rc=0 &gt;&gt;<br \/>\n? httpd.service &#8211; The Apache HTTP Server<br \/>\nLoaded: loaded (\/usr\/lib\/systemd\/system\/httpd.service; enabled; vendor preset: disabled)<br \/>\nActive: active (running) since Sat 2018-03-31 20:36:52 +08; 46s ago<\/p>\n<p>\/root\/ansible\/playbook\/test<\/p>\n<p>playbook_sample.yml<\/p>\n<p>&#8211; hosts: db<br \/>\nbecome: yes<br \/>\nbecome_method: sudo<br \/>\ntasks:<br \/>\n&#8211; name: General packages are installed<br \/>\nyum: name={{ item }} state=installed<br \/>\nwith_items:<br \/>\n&#8211; vim-enhanced<br \/>\n&#8211; wget<br \/>\n&#8211; unzip<br \/>\ntags: General_Packages<\/p>\n<p>[root@controller test]# ansible-playbook playbook_sample.yml &#8211;ask-become-pass<br \/>\nSUDO password:<\/p>\n<p>PLAY [db] *****************************************************************************************************************************************************************************************************************************************************************<\/p>\n<p>TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************<br \/>\nok: [node3.rmohan.com]<\/p>\n<p>TASK [General packages are installed] *************************************************************************************************************************************************************************************************************************************<br \/>\nok: [node3.rmohan.com] =&gt; (item=[u&#8217;vim-enhanced&#8217;, u&#8217;wget&#8217;, u&#8217;unzip&#8217;])<\/p>\n<p>PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************<br \/>\nnode3.rmohan.com : ok=2 changed=0 unreachable=0 failed=0<\/p>\n<p>[root@controller test]#<\/p>\n<p>ansible db -m shell -a &#8220;rpm -qa | grep -E &#8216;vim-enhanced|wget|unzip'&#8221; &#8211;ask-become-pass<\/p>\n<p>variables from &#8220;GATHERING FACTS&#8221;<br \/>\nvi playbook_sample3.yml<\/p>\n<p># refer to &#8220;ansible_distribution&#8221;, &#8220;ansible_distribution_version&#8221;<br \/>\n&#8211; hosts: target_servers<br \/>\ntasks:<br \/>\n&#8211; name: Refer to Gathering Facts<br \/>\ncommand: echo &#8220;{{ ansible_distribution }} {{ ansible_distribution_version }}&#8221;<br \/>\nregister: dist<br \/>\n&#8211; debug: msg=&#8221;{{ dist.stdout }}&#8221;<\/p>\n<p>[root@controller test]# ansible-playbook playbook_sample3.yml<\/p>\n<p>PLAY [web] ****************************************************************************************************************************************************************************************************************************************************************<\/p>\n<p>TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************************************<br \/>\nok: [node1.rmohan.com]<\/p>\n<p>TASK [Refer to Gathering Facts] *******************************************************************************************************************************************************************************************************************************************<br \/>\nchanged: [node1.rmohan.com]<\/p>\n<p>TASK [debug] **************************************************************************************************************************************************************************************************************************************************************<br \/>\nok: [node1.rmohan.com] =&gt; {<br \/>\n&#8220;msg&#8221;: &#8220;CentOS 7.4.1708&#8243;<br \/>\n}<\/p>\n<p>PLAY RECAP ****************************************************************************************************************************************************************************************************************************************************************<br \/>\nnode1.rmohan.com : ok=3 changed=1 unreachable=0 failed=0<\/p>\n<p>vi playbook_sample4.yml<br \/>\n&#8211; hosts: target_servers<br \/>\nbecome: yes<br \/>\nbecome_method: sudo<br \/>\nhandlers:<br \/>\n&#8211; name: restart sshd<br \/>\nservice: name=sshd state=restarted<br \/>\ntasks:<br \/>\n&#8211; name: edit sshd_config<br \/>\nlineinfile: &gt;<br \/>\ndest=\/etc\/ssh\/sshd_config<br \/>\nregexp=&#8221;{{ item.regexp }}&#8221;<br \/>\nline=&#8221;{{ item.line }}&#8221;<br \/>\nwith_items:<br \/>\n&#8211; { regexp: &#8216;^#PermitRootLogin&#8217;, line: &#8216;PermitRootLogin no&#8217; }<br \/>\nnotify: restart sshd<br \/>\ntags: Edit_sshd_config<\/p>\n<p>ansible-playbook playbook_sample4.yml &#8211;ask-become-pass<\/p>\n<p>export JAVA_HOME=\/opt\/java\/java\/<br \/>\nexport JRE_HOME=\/opt\/java\/java\/jre<br \/>\nexport PATH=$PATH:\/opt\/java\/java\/bin:\/opt\/java\/java\/jre\/bin<\/p>\n","protected":false},"excerpt":{"rendered":"<p>##### Steps for deployment of Ansible on CentOS 7<\/p>\n<p>##### Dependency Tasks<\/p>\n<p>### Install EPEL sudo yum install epel-release<\/p>\n<p>### Install pending updates sudo yum -y update<\/p>\n<p>##### Install Ansible<\/p>\n<p>### Install Ansible sudo yum -y install ansible<\/p>\n<p>### Verify the Version ansible &#8211;version<\/p>\n<p>[db] node1.rmohan.com [app] node2.rmohan.com [db] node3.rmohan.com<\/p>\n<p>ansible all &#8211;list-hosts ansible db [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[91],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7626"}],"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=7626"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7626\/revisions"}],"predecessor-version":[{"id":7627,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7626\/revisions\/7627"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}