Configuring Apache HTTPD Server with an Idempotent Ansible Playbook
The required ansible playbook, inventory file, ansible configuration file etc., at GitHub link:
asks1012/ARTH_Task_11.3 (github.com)
For running the ansible playbook httpd.yml, there are some prerequisites :
- The content of configuration file for ansible (/etc/ansible/ansible.cfg) is as follows :
[defaults]
inventory = /root/inventory.txt
host_key_checking = false
command_warnings = false
deprecation_warnings = false
2. Place the following content as inventory file in the location : /root/inventory.txt
Replace the IP’s, usernames and passwords of the target nodes :
[web]
192.168.43.71 ansible_user=root ansible_ssh_pass=122020
192.168.43.57 ansible_user=root ansible_ssh_pass=122020
3. Next step is to place the following content as httpd configuration file in the location : /root/file.conf (This will be used by ansible playbook as httpd configuration file)
Listen 8080
<virtualhost {{ ansible_facts['default_ipv4']['address'] }}:8080>
documentroot {{ document_root }}
</virtualhost>
The following Ansible playbook written by me for configuring httpd software and starting webserver. The important part is, this ansible playbook is idempotent. It will restart httpd service only when there is any change in the content of configuration file of httpd.
- hosts: web
vars_prompt:
- name: mount_path
prompt: Enter the directory to mount dvd
private: no
- name: document_root
prompt: Enter the directory for document root of httpd
private: no
tasks:
- name: Creating directory for mouting dvd
file:
path: "{{ mount_path }}"
state: directory
- name: Mounting dvd
mount:
src: /dev/cdrom
path: "{{ mount_path }}"
state: mounted
fstype: iso9660
- name: Configuring yum repository-1
yum_repository:
baseurl: "{{ mount_path }}/AppStream"
name: dvd1
file: dvd
gpgcheck: no
description: appstream_repo
- name: Configuring yum repository-2
yum_repository:
baseurl: "{{ mount_path }}/BaseOS"
name: dvd2
file: dvd
gpgcheck: no
description: baseos_repo
- name: Installing httpd software
package:
name: "httpd"
state: present
- name: Creating document root directory
file:
path: "{{ document_root }}"
state: directory
- name: Copying html file
copy:
dest: "{{ document_root }}/httpd.html"
content: This is web server configured through idempotent ansible playbook
- name: Enabling port 8080 in firewalld
firewalld:
port: 8080/tcp
state: enabled
permanent: yes
immediate: yes
- name: Copying httpd configuration file
template:
dest: /etc/httpd/conf.d/server.conf
src: /root/file.conf
notify: restart httpd
- name: Starting httpd server
service:
name: httpd
state: started
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
Run the playbook with the command :
ansible-playbook httpd.yml
Video of Running the playbook :
Finally, we configured the apache web server on the ansible target nodes.
Thank You