Learning Ansible : PART 3

Services: apache2_module, handlers, notify:


apache2_module: Enables or disables a specified module of the Apache2 webserver.


Handler are tasks that are defined in a playbook and can be called by the notify property in case of state change during task execution.



[root@licvdo1000 ansible]# cat webcomponents.yml
---
 - hosts: testserver
   become: true
   tasks:
     - name: install web components
       yum: name={{item}} state=latest update_cache=yes
       with_items:
       - httpd
       - mod_wsgi
       - python2-pip
       - python-virtualenv
     - name: ensure httpd is running
       service: name=httpd state=started enabled=yes
     - name: ensure mod_wsgi enabled
       apache2_module: state=present name=wsgi
       notify: restart httpd

   handlers:
       - name: restart httpd
         service: name=httpd state=restarted



[root@licvdo1000 ansible]# ansible-playbook webcomponents.yml

PLAY [testserver] **********************************************************************************************

TASK [Gathering Facts] *****************************************************************************************
ok: [licvdo1001]

TASK [install web components] **********************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of
 using a loop to supply multiple items and specifying `name: "{{item}}"`, please use `name: ['httpd',
'mod_wsgi', 'python2-pip', 'python-virtualenv']` and remove the loop. This feature will be removed in version
2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [licvdo1001] => (item=[u'httpd', u'mod_wsgi', u'python2-pip', u'python-virtualenv'])

TASK [ensure httpd is running] *********************************************************************************
ok: [licvdo1001]

TASK [ensure mod_wsgi enabled] *********************************************************************************
ok: [licvdo1001]

PLAY RECAP *****************************************************************************************************
licvdo1001                 : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


Copy Module:


It is used to copy a file from the control machine to the target machine. 


[root@licvdo1000 ansible]# cat webserver.yml
---
 - hosts: testserver
   become: true
   tasks:
     - name: install webserver
       yum: name=httpd state=latest update_cache=yes
     - name: ensure httpd is running
       service: name=httpd state=started enabled=yes
     - name: copy demo html file
       copy: src=index.html dest=/var/www/html/index.html mode=0755
       notify: restart httpd

   handlers:
       - name: restart httpd
         service: name=httpd state=restarted


Before running the playbook, the URL is displaying the below webpage:



Now, We run this playbook and when We try to open the same URL, it should show the new page which is copied to the destination.

[root@licvdo1000 ansible]# ansible-playbook webserver.yml

PLAY [testserver] **********************************************************************************************

TASK [Gathering Facts] *****************************************************************************************
ok: [licvdo1001]

TASK [install webserver] ***************************************************************************************
ok: [licvdo1001]

TASK [ensure httpd is running] *********************************************************************************
ok: [licvdo1001]

TASK [copy demo html file] *************************************************************************************
changed: [licvdo1001]

RUNNING HANDLER [restart httpd] ********************************************************************************
changed: [licvdo1001]

PLAY RECAP *****************************************************************************************************
licvdo1001                 : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0



Boom!!! We can see the new webpage which we have created.








No comments:

Post a Comment

Installation of Jenkins on Linux and Deployment NGINX through Jenkins

Installation of Jenkins: [root@worker1 ~]# useradd -c "jenkins user" jenkins [root@worker1 ~]# passwd jenkins Changing passw...