Configure Apache Webserver Using Ansible
By: Rohit Raut
Tags: Ansible, DevOps, Apache Web Server

About Ansible

What is Webserver?

A web server is server software or hardware dedicated to running this software, that can satisfy client requests on the World Wide Web. A web server can, in general, contain one or more websites. A web server processes incoming network requests over HTTP.

What is Playbook?

The real power of Ansible, however, is in learning how to use playbooks to run multiple, complex tasks against a set of targeted hosts in an easily repeatable manner.

A play is an ordered set of tasks run against hosts selected from your inventory. A playbook is a text file containing a list of one or more plays to run in a specific order. Plays allow you to change a lengthy, complex set of manual administrative tasks into an easily repeatable routine with predictable and successful outcomes.

In a playbook, you can save the sequence of tasks in a play into a human-readable and immediately runnable form. The tasks themselves, because of the way in which they are written, documents the steps needed to deploy your infrastructure.

A playbook consist of multiple plays is in a text file written in YAML format and is normally saved with the extension yml. Each play must have at list two keys

hosts: “hosts” is an attribute, which specifies the hosts against which the plays will be run by ansible. Tasks: it consists of a list of tasks/configurations applied on hosts.

Prerequisites:

♦ Yum Should be Configured.

Folder Tree:

Variables File:

doc_root: /var/www/new_doc_root

port_number: 8080

Webserver Conf File:

Listen {{ port_number }}

<virtualhost {{ ansible_facts['default_ipv4']['address'] }}:{{ port_number }}>

documentroot {{ doc_root }}

</virtualhost>

Let’s get Started

Installing WebServer Software

- hosts: webserver

vars_files:

- variables

tasks:

- name: "installing httpd"

package:

name: "httpd"

state: present

Creating document root



file:

state: directory

dest: "{{ doc_root }}" - name: "Creating Document root"state: directorydest: "{{ doc_root }}"

Configuration File

here we are copying the webserver configuration file in /etc/httpd/conf.d which is the secondary conf file of the Apache webserver. This configuration will override the main file configuration.

template module first parses the file for variables and jinja syntax and replaces it with its values and then uploads the file to the managed node. This is a simple difference between the copy and template module.

Copy WebPages To document root.



copy:

content: "<h1> WebPage </h1>"

dest: "{{ doc_root }}/index.html" - name: "Copy webpages"content: " WebPage "dest: "{{ doc_root }}/index.html"

Start webserver Service.

“enabled: yes” makes the service permanent so we don’t need to start against after boot.



service:

name: "httpd"

state: started

enabled: yes - name: "web Server start"name: "httpd"state: startedenabled: yes

Adding FirewallRule



firewalld:

port: "{{ port_number }}/tcp"

permanent: yes

state: enabled

immediate: yes - name: "Adding Firewall rule port {{ port_number }}"port: "{{ port_number }}/tcp"permanent: yesstate: enabledimmediate: yes

Handlers

We can trigger the handlers using notify when a configuration file of daemon has been changed. so we can avoid unnecessary restarts. when the template module does the changes in the conf file then notify triggers the handler.

handlers:

- name: "server restart"

service:

name: "httpd"

state: restarted - name: "server restart"service:name: "httpd"state: restarted

Final Output:

GitHub Link:https://github.com/rohitraut3366/Configure-Webserver-Using-Ansible.git

Thank you for reading!!😇😇

Connect with me On LinkedIn for further queries.