Configuration of Apache Web Server with Authentication using Ansible-Playbook (Redhat CLI)

Prakash Agarwal
4 min readDec 3, 2020

I am assuming, You know Basics of Ansible and have it in your system, Let’s just start configuring web server with Ansible-Playbook.

First of all, Let’s know something about Ansible-Playbook.

  • Playbooks record and execute Ansible’s configuration, deployment, and orchestration functions.
  • At a basic level, playbooks can be used to manage configurations of and deployments to remote machines.
  • Ansible Playbooks offer a repeatable, re-usable, simple configuration management and multi-machine deployment system, one that is well suited to deploying complex applications.

That’s Enough for Now.

Now let’s starts the fun part (Practical Part), Setting up the Ansible-Playbook.

Start with creating a .yml file. (Here, We are using YAML file format for Playbook) .

Refer for more (YAML syntax): https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html

As we are using only one system as Target Node, I manually wrote the IP in HOSTS or we can also use one file called “INVENTORY” which consists of all Target Nodes’ IP Addresses.

In TASKS, we write all the operations, we need to do at the target node.

PACKAGE is used for applications to install.

So in the very first step, We installed the “HTTPD” in our Target Node.

Now, As we all know, In Apache Web Server, all files are stored at “/var/www/html” , so we will copy one file using COPY tag in our Target Node, with name “index.html”.

DEST stands for destination, where file is going to copy and CONTENT tag is for, what will be stored in the file! We can also use SRC(Source) tag for directly copying one complete file from Control Node to Target Node.

Now, As we also have to add Authentication at the Web-End, We need to update the Configuration file of Apache Web Server, which is at “/etc/httpd/conf/httpd.conf”. REPLACE tag is used to replace some content from a file, here REGEXP is the present content of file and REPLACE tag showing the content which is to be updated with PATH tag which is showing the Address of the file.

Ansible-Playbook
.htaccess file for Authentication setup

We also have to copy one file “.htaccess” with COPY tag, as we discussed earlier, here we are copying one complete file from Control Node to Target Node with SRC tag which have content of Authentication of Web Server such as Authentication Type and Authentication file(which will contain valid username and password).

Now with HTPASSWD tag, we will create a file at “/etc/www.passwd” and will give username and password for the authentication of web server in it.

With SERVICE tag, We will change the current state of Web Server and start it with all the updated content and authentication.

Now as we will run the playbook ws.yml with cmd:

ansible-playbook <ws.yml>

At last, This is successful, that means, at our Target Node, Web Server is successfully started with Authentication. We can check it at the Target node as:

When we redirect to the page, We need to authenticate our access with username and password which we provided in /www.passwd file.

And after authentication, We will get output as:

This is h0w, we can say, we successfully configured an Apache Web Server with help of Ansible-Playbook.

Now the benefit of this is, We can configure as many Target Nodes as we want with just attaching their IP addresses to the yml file or inventory file with Just this one PLAYBOOK. This automation saves a lot of time.

--

--

Prakash Agarwal

Technical Writer | Content Creator | Storyteller | Engineer | Investor