Tuesday, 28 April 2020

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:

[root@LC-Ansible-Master ~]# 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
…………………………………………………….
[root@LC-Ansible-Master ~]#

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:

[root@LC-Ansible-Master my-playbooks]# su - ansible-user
Last login: Sun Apr 26 23:48:51 IST 2020 on pts/2
[ansible-user@LC-Ansible-Master ~]$ mkdir my-playbooks
[ansible-user@LC-Ansible-Master ~]$ cd my-playbooks
[ansible-user@LC-Ansible-Master my-playbooks]$

Every playbooks starts with “- - - “ , Hosts and authentication details. So put the following contents and modules in beginning of the playbook:

[ansible-user@LC-Ansible-Master 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:

[ansible-user@LC-Ansible-Master 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):

[ansible-user@LC-Ansible-Master 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

[ansible-user@LC-Ansible-Master 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.

1 comment:

  1. copied your code it didnt work (fatal: [host3]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/pyth on"}, "changed": false, "cmd": "dnf install -y python2-dnf", "msg": "[Errno 2] No such file o r directory", "rc": 2} )

    ReplyDelete