Configuring web server using Ansible

Configuring web server using Ansible

Ansible is a simple open-source IT engine that automates application deployment, intra- service orchestration, cloud provisioning, and many other IT tools.

Ansible is easy to deploy because it does not use any agents or custom security infrastructure.

Ansible uses playbook to describe automation jobs, and playbook uses very simple language i.e. YAML (It’s a human-readable data serialization language & is commonly used for configuration files, but could be used in many applications where data is being stored)which is very easy for humans to understand, read and write. Hence the advantage is that even the IT infrastructure support guys can read and understand the playbook and debug if needed (YAML – It is in human-readable form).

Task:-

☞ Restarting httpd Service is not idempotence in nature and also consume more resources suggest a way to rectify this challenge in the Ansible playbook

Steps to be followed:-

☞Install httpd software

☞Create a new directory for the new document root

☞Copy the conf file from the managed node

☞Create a webpage

☞Start the services

☞Run the handler if something changed in the configuration file

Solution:-

- hosts: webserver
  vars:
          - port: "8080"
  tasks:
          - name: "installing httpd"
            package: 
                name: "httpd"
                state: present


          - name: "creating directory"
            file:
                path: "/var/www/akash"
                state: directory
          - name: "copying config file"
            template: 
                src: "new.conf"
                dest: "/etc/httpd/conf.d/new.conf"
            notify: "restarting httpd"


          - name: "creating web page"
            copy:
                content: "configuring handlers"
                dest: "/var/www/akash/index.html"
          - name: "starting services"
            service:
                name: "httpd"
                state: started   




  handlers:
   - name: "restarting httpd"
     service:
             name: "httpd"
             state: restarted  

This file will be executed over the webserver group in the inventory.

You can execute the code by running the following command

ansible-playbook webserver.yml

The configuration file to change document root and port number is attached below

Listen {{ port }}

{% for hosts in groups['webserver'] %}
<VirtualHost {{ hosts }}:{{ port }}>
DocumentRoot /var/www/akash/
</VirtualHost>

{% endfor %}

On running the playbook for the first time you can see that the services are restarting as handler is running successfully

No alt text provided for this image

By again running the playbook we can see services are not starting again. Hence there will be no downtime in our server

No alt text provided for this image

By going to the managed node IP and on the port number 8080 (given by us) we can see our website

No alt text provided for this image


That's all!!

For any queries or suggestions feel free to mail me at: akashtiwari370@gmail.com

For viewing and downloading the complete code visit my GitHub





To view or add a comment, sign in

More articles by Akash Tiwari

Others also viewed

Explore content categories