Configure Apache Webserver Using Ansible

Rohit Raut
3 min readDec 4, 2020

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

  1. hosts: โ€œhostsโ€ is an attribute, which specifies the hosts against which the plays will be run by ansible.
  2. 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

- name: "Creating Document root"
file:
state: directory
dest: "{{ 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.

- name: "Configuration File"
template:
src: "./webserver.j2"
dest: "/etc/httpd/conf.d/new_conf.conf"
notify: "server restart"

Copy WebPages To document root.

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

Start webserver Service.

โ€œenabled: yesโ€ makes the service permanent so we donโ€™t need to start against after boot.

- name: "web Server start"
service:
name: "httpd"
state: started
enabled: yes

Adding FirewallRule

- name: "Adding Firewall rule port {{ port_number }}"
firewalld:
port: "{{ port_number }}/tcp"
permanent: yes
state: enabled
immediate: 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

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.

--

--