Ansible Pilot

Deploy a web server apache httpd on RedHat-like systems - Ansible modules yum, copy, service firewalld

How to automate the deployment of a web server apache httpd on RedHat-like systems with custom web page taking care of downloading, installing, and enabling the service instantly and on boot and open the relevant firewall ports with Ansible modules yum, copy, service firewalld. RedHat Enterprise Linux, CentOS, CentOS Stream, Fedora, ClearOS, Oracle Linux, EuroLinux, Fermi Linux, EulerOS, ROSA Linux, Springdale Linux, Asianux

How to deploy a webserver apache httpd on RedHat-like systems with Ansible?

I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Deploy a web server apache httpd on RedHat-like systems

Today we’re talking about how to Deploy a web server apache httpd on RedHat-like Linux systems. The full process requires four steps that you could automate with different Ansible modules. Firstly you need to install the httpd package and dependency using the ansible.builtin.yum Ansible module. Secondly, you need to create the custom index.html with ansible.builtin.copy Ansible module. You could upgrade this step using the template module. Thirsty you need to start the httpd service and enable on boot and all the dependant using the ansible.builtin.service Ansible module. Fourthly you need to open the relevant firewall service-related ports using the ansible.posix.firewalld Ansible module.

demo

Deploy a web server apache httpd on RedHat-like systems with Ansible Playbook.

code

---
- name: setup webserver
  hosts: all
  become: true
  tasks:
    - name: httpd installed
      ansible.builtin.yum:
        name: httpd
        state: latest
    - name: custom index.html
      ansible.builtin.copy:
        dest: /var/www/html/index.html
        content: |
                    Custom Web Page
    - name: httpd service enabled
      ansible.builtin.service:
        name: httpd
        enabled: true
        state: started
    - name: open firewall
      ansible.posix.firewalld:
        service: http
        state: enabled
        immediate: true
        permanent: true

execution

ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory services/httpd_redhat.yml
PLAY [setup webserver] ****************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [httpd installed] ****************************************************************************
changed: [demo.example.com]
TASK [custom index.html] **************************************************************************
changed: [demo.example.com]
TASK [httpd service enabled] **********************************************************************
changed: [demo.example.com]
TASK [open firewall] ******************************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

idempotency

ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory services/httpd_redhat.yml
PLAY [setup webserver] ****************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [httpd installed] ****************************************************************************
ok: [demo.example.com]
TASK [custom index.html] **************************************************************************
ok: [demo.example.com]
TASK [httpd service enabled] **********************************************************************
ok: [demo.example.com]
TASK [open firewall] ******************************************************************************
ok: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

before execution

ansible-pilot $ ssh [email protected]
Last login: Sat Feb 12 10:08:51 2022 from 192.168.0.100
[[email protected] ~]$ sudo su
[[email protected] devops]# cat /etc/os-release 
NAME="Red Hat Enterprise Linux"
VERSION="8.5 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.5"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.5 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8::baseos"
HOME_URL="https://www.redhat.com/"
DOCUMENTATION_URL="https://access.redhat.com/documentation/red_hat_enterprise_linux/8/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.5
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.5"
[[email protected] devops]# dnf list installed httpd
Updating Subscription Management repositories.
Error: No matching Packages to list
[[email protected] devops]# rpm -qa | grep httpd
[[email protected] devops]# cat /var/www/html/index.html
cat: /var/www/html/index.html: No such file or directory
[[email protected] devops]# ls -al /var/www
ls: cannot access '/var/www': No such file or directory
[[email protected] devops]#

after execution

ansible-pilot $ ssh [email protected]
Last login: Sat Feb 12 10:12:47 2022 from 192.168.0.100
[[email protected] ~]$ sudo su
[[email protected] devops]# dnf list installed httpd
Updating Subscription Management repositories.
Installed Packages
httpd.x86_64      2.4.37-43.module+el8.5.0+13806+b30d9eec.1       @rhel-8-for-x86_64-appstream-rpms
[[email protected] devops]# rpm -qa | grep httpd
httpd-tools-2.4.37-43.module+el8.5.0+13806+b30d9eec.1.x86_64
httpd-filesystem-2.4.37-43.module+el8.5.0+13806+b30d9eec.1.noarch
httpd-2.4.37-43.module+el8.5.0+13806+b30d9eec.1.x86_64
redhat-logos-httpd-84.5-1.el8.noarch
[[email protected] devops]# cat /var/www/html/index.html 
Custom Web Page
[[email protected] devops]#

web server apache httpd on RedHat-like system

code with ❤️ in GitHub

Recap

Now you know how to deploy a web server apache httpd on RedHat-like systems with Ansible. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 100+ Automation Examples For Linux and Windows System Administrator and DevOps

Want to keep this project going? Please donate

Trustpilot
Follow me

Subscribe not to miss any new releases

February 12, 2022

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress