Resolving the wastage of resources of a system ( RAM and CPU ) , using Ansible ....

Resolving the wastage of resources of a system ( RAM and CPU ) , using Ansible ....


Talking about Ansible as an automation tool ... The prime objective that it focussses upon is Configuration Management . Whatever we want to configure whether it be a webserver or mail server or database , Ansible will do that on behalf of us , But , there may be some cases , where we need to a bit modify our playbook or code , so that unnecessary wastage or RAM and CPU doesnot occur , while configuring something on the Managed Host ...


Let's say we have written this playbook ( in the Controller Node ) to configure webserver ( on a different system , i,e, Managed Host ) as shown below :


---
- hosts: all
  vars_files:
          specify.yml
  tasks:
        - name: "Installing apache httpd software"
          package:
                  name: "httpd"
                  state: present
        
        - name: "Creating new document root folder for httpd"
          file:
               dest: "{{ fold }}"
               state: directory
        
        - name: "Copying configuration file to host"
          template:
                  src: "new.conf"
                  dest: "/etc/httpd/conf.d"
          
        - name: "Copying webpages to document root folder"
          copy:
               src: "webpage.html"
               dest: "{{ fold }}"


        - name: "Starting httpd service"
          service:
                  name: "httpd"
                  state: started

        - name: "Enabling firewall permission"
          ansible.posix.firewalld:  
                                 port: "{{ port_no }}/tcp"
                                 state: enabled 
                                 immediate: yes




Here in the above code , we see that a new configuration file has been provided for the apache httpd software in our Managed Host ...

Initially , the webservice was not started .... Therefore , the service module used in this code , will start the webservices for us . The firewall rule enabling will also allow incoming traffic to connect to the webserver program and thus the client will be able to view the webpages ...


Here , we might face a challenge !!!

Let's say we change something in the configuration file of the httpd .. But , for applying those changes we must restart our webserver program ... But , the above code provides no scope of restarting the service on the Managed Host using Ansible .

For this we can update the playbook as follows :

---
- hosts: all
  vars_files:
          specify.yml
  tasks:
        - name: "Installing apache httpd software"
          package:
                  name: "httpd"
                  state: present
        
        - name: "Creating new document root folder for httpd"
          file:
               dest: "{{ fold }}"
               state: directory
        
        - name: "Copying configuration file to host"
          template:
                  src: "new.conf"
                  dest: "/etc/httpd/conf.d"
          
        
        - name: "Copying webpages to document root folder"
          copy:
               src: "webpage.html"
               dest: "{{ fold }}"


        - name: "Starting httpd service"
          service:
                  name: "httpd"
                  state: started

        - name: "Starting httpd service"
          service:
                  name: "httpd"
                  state: restarted

        - name: "Enabling firewall permission"
          ansible.posix.firewalld:
                                  port: "{{ port_no }}/tcp"
                                  state: enabled 

                                  immediate: yes


Here , although the service module has the state: started ---> it will be skipped ( due to the idempotence nature of ANSIBLE ) , because my webservice is still running .

The state: restarted ---> will make the webserver program restart once again , and thus , if any changes was made to the configuration file using the template module here in this code , it will be applied to the webserver program .



YET updating our playbook till here also ... May lead us to one more challenge that we may face !!!


Lets, say in our configuration file we have changed the default document root folder for our webpages that has to be displayed to the clients or customers ... Now , using the above code our changes are applied as well as our httpd service is also restarted , and the applied changes are in action ... Uptill here we are fine ..

But , if we again run our above playbook once more , although now no changes are made to the configuration file .... Still our services will be restarted ....

It may happen sometimes , that we want to check our playbook is running fine or not ... We know the idempotence nature of ANSIBLE , thus we will be sure that our tasks won't be run again from scratch causing the wastage of resources on the Managed Host ...

In the above case , state: restarted ---> will always restart our service , regardless of whether some changes were made to the configuration file or not . Thus , we can think that idempotency is not provided in this case ... Therefore , wastage or unnecessary use of resources will occur on the Managed Host ...


For this we have a solution !!! ANSIBLE can help us by Conditionals and Handlers that it has in itself , for solving such scenarios...


Using Handlers in our playbook as follows :

---
- hosts: all
  vars_files:
          specify.yml
  tasks:
        - name: "Installing apache httpd software"
          package:
                  name: "httpd"
                  state: present
        
        - name: "Creating new document root folder for httpd"
          file:
               dest: "{{ fold }}"
               state: directory
        
        - name: "Copying configuration file to host"
          template:
                  src: "new.conf"
                  dest: "/etc/httpd/conf.d"
          notify:
                - Restart httpd service
                - Enabling firewall permission
        
        - name: "Copying webpages to document root folder"
          copy:
               src: "webpage.html"
               dest: "{{ fold }}"

        - name: "Starting httpd service"
          service:
                  name: "httpd"
                  state: started




  handlers:


           - name: "Restart httpd service"
             service:
                     name: "httpd"
                     state: restarted


           - name: "Enabling firewall permission"
             ansible.posix.firewalld:
                                    port: "{{ port_no }}/tcp"
                                    state: enabled 
                                    
                                    immediate: yes


Here , whenever a change will be made to the configuration file of the httpd software , while uploading it from the contoller node on to the managed host ... A notification will be given to the handler block ... Thus , after all the tasks has been done , the handler block will execute , and here we have specified tghe module and code for restarting the service and also enabling firewall permission ....

Therefore , if no change is made to the configuration file , the handler will not execute and the service will not be restarted . This will save the resources in the Managed Host ... Thus , such optimization of code is very helpful at such scenarios or use cases .

We can also give Conditionals to do the same task as follows :

---
- hosts: all
  vars_files:
          specify.yml
  tasks:
        - name: "Installing apache httpd software"
          package:
                  name: "httpd"
                  state: present
        
        - name: "Creating new document root folder for httpd"
          file:
               dest: "{{ fold }}"
               state: directory
        
        - name: "Copying configuration file to host"
          template:
                  src: "new.conf"
                  dest: "/etc/httpd/conf.d"
          register: output

        - name: "Copying webpages to document root folder"
          copy:
               src: "webpage.html"
               dest: "{{ fold }}"


        - name: "Starting httpd service"
          service:
                  name: "httpd"
                  
                  state: started
         

        - name: "Restart httpd service"
          service:
                  name: "httpd"
                  state: restarted
          when: output.changed == true




So that's it for this Article !!!

Hope you all found it insightful and enlightening ...


No alt text provided for this image


SIGNING OFF ---> Satabrata Paul !!!


要查看或添加评论,请登录

Satabrata Paul的更多文章

社区洞察

其他会员也浏览了