ANSIBLE-4

How to configure apache webserver using ansible?

Configure Apache server using ansible-playbook

Shubham Rasal [SRE]
4 min readDec 17, 2020

--

Introduction

Redhat Ansible

Ansible is an open-source automation tool by which we can automate all cloud provisioning, configuration management, application deployment,intra-service orchestration, and many more IT needs.

Ansible is an open-source community project sponsored by Red Hat, it’s the simplest way to automate IT. Ansible is the only automation language that can be used across entire IT teams from systems and network administrators to developers and managers.
You can read more about ansible in the below article and how it helps in DevOps.

Apache Webserver

Apache is an open-source and free web server software that powers around 40% of websites around the world. The official name is Apache HTTP Server, and it’s maintained and developed by the Apache Software Foundation. It allows website owners to serve content on the web — hence the name “webserver.” It’s one of the oldest and most reliable web servers, with the first version released more than 20 years ago, in 1995.

What do we want to do?

  1. Install an Apache web server on the managed node
  2. Configure webserver. change the document root and port number.
  3. Restart the webserver

but restarting HTTPD Service is not idempotence in nature and also consumes more resources so we have to suggest a way to rectify this and solve challenges in the Ansible playbook.

Action

Create entry of managed node in the inventory file.

You can refer to this article on how to create ssh key and use ssh public key authentication.

Create a new group named “webserver” and add a managed node server IP address there.

Write an ansible-playbook

This is my directory structure.

vars.yml file has all the variables where webserver.yml is ansible-playbook. creatorsbyheart.conf.j2 is a jinja template for changing document root and port number. my site is source-code-directory.

here we have to mention the project or website name for creating the conf file in the configuration directory.
source_code_path is the source code directory location.
webserver_conf_file is the jinja template file location
and port_number and document_root_location for configuration the webserver on that location.

Now let’s take a look at the conf file for our website

here we are reading the port number and document root from the vars.yml file through ansible-playbooks.

Here we are configuring the apache server using ansible.

In ansible, If we restart service using ansible service module… it restarts the service again. i.e it is not ideompotenet in nature. because we only to restart the webserver only if anything is changed.

so for that, we have used handlers that only get notified when we change the conf file. This way we are restarting the webserver each time we run the playbook can be avoided.

I have configured only for the RedHat family you can change variables and software names according to your managed node os_family.

You can find the above playbook on this GitHub repository. Bookmark or star it for future use.

If you have any doubts or something improvement needed in this blog, please feel free to reach out to me on my LinkedIn account.

I hope you learned something new and find ansible more interesting.
Let me know your thoughts about ansible and how do plan to use ansible?

Thank you.

About the writer:
Shubham loves technology, challenges, is open to learning and reinventing himself. He loves to share his knowledge. He is passionate about constant improvements.
He writes blogs about
Cloud Computing, Automation, DevOps, AWS, Infrastructure as code.
Visit his Medium home page to read more insights from him.

--

--