Saturday, July 13, 2019

Using VARS in ansible


# Ansible playbook to Install & manage web, firewalld   using VARS.
================================================


[root@ansimaster:~/ansiroot/playbook]# vi  mange_web_using_vars.yml
---
 - name: Install Apache and start the service
   hosts: webservers
   vars:
     web_pkg: httpd
     firewall_pkg: firewalld
     python_pkg: python-httplib2
     web_service: httpd     
     firewall_service: firewalld
     rule: http
  
   tasks:
     - name: installing httpd, firewalld, python-httpdlib2 packages...
       yum:
         name:
           - "{{ firewall_pkg }}"
           - "{{ web_pkg }}"
           - "{{ python_pkg }}"
         state: present

     - name: Starting and Enabling the {{ firewall_pkg }} services
       service:
         name: "{{ firewall_service }}"
         enabled: true
         state: started

     - name: Starting and Enabling the {{ web_service }} services
       service:
         name: "{{ web_service }}"
         enabled: true
         state: started

  
     - name: Creating web conntent to be served
       copy:
         content: " Welcome Mylinuxfriend sponserd by mylinuxfriends.blogspot.com"
         dest: /usr/share/httpd/noindex/index.html
   
     - name: Open the port for {{ rule }}
       firewalld:
         service: "{{ rule }}"
         permanent: true
         state: enabled

 - name: Verify the Apache service
   hosts: webservers
   tasks:
     - name: Ensure the webserver is reachable
       uri:
         url: http://192.168.122.50
         status_code: 200
[root@ansimaster:~/ansiroot/playbook]#






====================================
Now lets check our playbook is good to execute or not.
====================================



[root@ansimaster:~/ansiroot/playbook]# \ playbook 11:52 AM]ansible-playbook mange_web_using_vars.yml -C

PLAY [Install Apache and start the service] **********************************************************************************************

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

TASK [installing httpd, firewalld, python-httpdlib2 packages...] *************************************************************************
changed: [192.168.122.50]

TASK [Starting and Enabling the firewalld services] **************************************************************************************
changed: [192.168.122.50]

TASK [Starting and Enabling the httpd services] ******************************************************************************************
changed: [192.168.122.50]

TASK [Creating web conntent to be served] ************************************************************************************************
changed: [192.168.122.50]

TASK [Open the port for http] ************************************************************************************************************
ok: [192.168.122.50]

PLAY [Verify the Apache service] *********************************************************************************************************

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

TASK [Ensure the webserver is reachable] *************************************************************************************************
skipping: [192.168.122.50]

PLAY RECAP *******************************************************************************************************************************
192.168.122.50             : ok=7    changed=4    unreachable=0    failed=0  

[root@ansimaster:~/ansiroot/playbook]# \ playbook 11:52 AM]

=============================================
Yeah its good and good to execute, just remove -C and hit the magic button ENTER
===============================================


No comments:

Post a Comment

Complete list of  recommended VS Code extensions  for writing and managing  AWS Terraform scripts , including syntax checking, variable supp...