How to Install and Configure Apache using Ansible Playbook on CentOS 8

Apache is most popular and open source web server to server html, php and others. It released by Apache foundation under GNU license. The Apache binary is available in CentOS 7 default repository. This article describes How to Install Apache/HTTPD Web server in CentOS 7.

This article describes How to Install Apache using Ansible Playbook on CentOS8.
If Ansible not installed, Follow the article Howto Install Ansible Master Node on CentOS 8.
Step 1. Update Host Inventory: Update the specific client machines IP address under the specific group into the Ansible Host Inventory file:
For example, a node (192.168.43.30) is added under webserverstest group in the ansible host inventory file for apache installation and configuration using ansible:
[[email protected] ~]# vi /etc/ansible/hosts
# This is the default ansible ‘hosts’ file.
……………………………………………………..
[webserverstest]                 #Group Name
192.168.43.30                     #Ansible client machine to manage by Ansible
…………………………………………………….
Step 2. Creating Ansible Playbook: For best practice, switch with the normal user and create a directory in user’s home directory to store ansible playbooks:
[[email protected] my-playbooks]# su – ansible-user
Last login: Sun Apr 26 23:48:51 IST 2020 on pts/2
[[email protected] ~]$ mkdir my-playbooks
[[email protected] ~]$ cd my-playbooks
[[email protected] my-playbooks]$
Every playbooks starts with “- – – “ , Hosts and authentication details. So put the following contents and modules in beginning of the playbook:
[[email protected] my-playbooks]$ vi apache-installation.yml
– hosts: webserverstest
  remote_user: ansible-user
  become: yes
  gather_facts: no
Once the above entry done in the playbook, put the tasks list in the file. dnf ansible module install latest version of apache:
  tasks:
  – name: Apache latest version installation
    dnf:
      name: httpd
      state: latest
Service module enable service to start on across reboot the machine:
  – name: Enable service to start on boot up
    service:
      name: httpd
      state: started
Firewalld module create a firewall rule to access the service:
  – name: Create firewall rule for apache service
    firewalld:
      service: http
      zone: public
      permanent: yes
      immediate: yes
      state: enabled
Finally, the handlers restart the apache service:
  handlers:
  – name: Restart apache service
    service:
      name: httpd
      state: restarted
Step 3. Review the Playbook: After completing the ansible playbook will looks as below:

[[email protected] my-playbooks]$ vi apache-installation.yml
– hosts: webserverstest
  remote_user: ansible-user
  become: yes
  gather_facts: no
  tasks:
  – name: Apache latest version installation
    dnf:
      name: httpd
      state: latest
  – name: Enable service to start on boot up
    service:
      name: httpd
      state: started
  – name: Create firewall rule for apache service
    firewalld:
      service: http
      zone: public
      permanent: yes
      immediate: yes
      state: enabled
  handlers:
  – name: Restart apache service
    service:
      name: httpd
      state: restarted
Step 4. Apache Installation using Playbook: Finally run the following command to install apache on client machines (verify the host inventory before execution of any playbook):
[[email protected] my-playbooks]$ ansible-playbook apache-installation.yml
PLAY [webserverstest] *****************************************************
TASK [Apache latest version installation] *************************************
changed: [192.168.43.30]
TASK [Enable service to start on boot up] ************************************
changed: [192.168.43.30]
TASK [Create firewall rule for apache service] *********************************
changed: [192.168.43.30]
PLAY RECAP ***********************************************************
192.168.43.30              : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[[email protected] my-playbooks]$
Step 5. Verify Installation and Configuration: Apache installation and configuration is done using ansible playbook. Now open any browser and hit the server IP address or hostname to access the apache default page as show in below screenshot:

Done! Apache default installation is successfully done using ansible playbook.

Leave a Reply