Configuration of Webserver Using Ansible

What is Ansible?

What is Webserver?

Document Root

What is HTTP Authentication?

In this article, we are going to learn how Ansible can be used to Configure Webserver.

🔰Idempotence feature of Ansible🔰

🔰Ansible Playbooks🔰

🔰Ansible Handlers🔰

🔰Ansible Vault🔰

To create any playbook it’s important to set a goal, the steps needed to complete the task.

🔰Following are the steps needed for creating playbook for Configuration of Webserver🔰

🔰Ansible Document🔰

ansible-doc <module_name>

🔰Creating Ansible Playbook🔰

👉Step-1 : Create a folder and Mount the dvd

- file:
state: directory
path: /<Name_of_dvd>
- mount:
src: "/dev/cdrom"
path: <Name_of_dvd>
state: "mounted"
fstype: "iso9660"

👉Step-2 : Configure yum

- yum_repository:
name: "dvd1"
baseurl: <Name_of_dvd>/AppStream
description: "yum dvd1 for package"
gpgcheck: no
- yum_repository:
name: "dvd2"
baseurl: <Name_of_dvd>/BaseOS
description: "yum dvd2 for package"
gpgcheck: no

👉Step-3 : Install httpd software

- package:
name: httpd
state: present

👉Step-4 : Create Document Root Folder

- file:
state: directory
path: /var/www/http

👉Step-5 : Change Document Root Folder and Port Number

- template:
dest: "/etc/httpd/conf.d/demo.conf"
src: "demo.conf"
notify: restart_service

👉Step-6 : Copy webpage to Document Root

- copy:
dest: "/var/www/http/index.html"
content: "Webserver Configured Using Ansible!!!"

👉Step-7 : Allow HTTP Authentication in httpd.conf file

- replace:
path: "/etc/httpd/conf/httpd.conf"
regexp: "AllowOverride None"
replace: "AllowOverride Authconfig"
notify: restart_service

👉Step-8 : Create .htaccess file to control access to document root directory

- file:
state: touch
path: "{{ doc_root }}/.htaccess"
- copy:
dest: "{{ doc_root }}/.htaccess"
src: "/root/playbook/.htaccess"
notify: restart_service

👉Step-9: Create User for Authentication using htpasswd file

- package:
name: pyhton3
state: present
- pip:
name: passlib
state: present
- htpasswd:
path: "/etc/www.passwd"
name: "{{ username }}"
password: "{{ password }}"

👉Step-10 : Start httpd service

- service:
name: httpd
state: started

👉Step-11 : Set Firewall Rules

- firewalld:
port: <port>/tcp
state: enabled
permanent: yes
immediate: yes


- name: restart_service
name: httpd
state: restarted

🔰Variable File🔰

ansible-vault create <vault-name>
Cannot Read Vault file Without Password
ansible-vault view <vault-name>

🔰Complete Playbook🔰

🔰Running Ansible Playbook🔰

ansible <IP_of_managed_node> -m ping
ansible-playbook <Playbook_name> --syntax-check --ask-vault-pass
ansible-playbook <Playbook_name>  --ask-vault-pass
Handler doesn’t run as there is No Changes occur!!
Handler Runs only Once even if notified twice!!

🔰After Running Ansible Playbook🔰

Asking for User Authentication

A Technical Writer