Loading...

Watch: Configure Apache Vhost on RedHat Using Ansible Playbook

Automate Apache virtual host setup on RedHat systems with Ansible. Learn to manage web server installation, configuration, and firewall settings.

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

I'm going to show you a live Playbook with some simple Ansible code.

This Playbook is quick and dirty but shows you the basics of Ansible automation technology that you could use in your System Administrator every day.

I'm Luca Berton and welcome to today's episode of Ansible Pilot.

Deploy a web server apache httpd virtualhost on RedHat-like systems

  • install packages => ansible.builtin.yum
  • document root => ansible.builtin.file
  • custom index.html => ansible.builtin.copy
  • Apache virtualhost => ansible.builtin.template
  • start service => ansible.builtin.service
  • open firewall => ansible.posix.firewalld

Today we're talking about how to Deploy a web server apache httpd on RedHat-like Linux systems.

The full process requires six 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 document root with the right permission with the ansible.builtin.file module.

Thirsty, you need to create the custom index.html with ansible.builtin.copy Ansible module. You could upgrade this step using the template module.

Fourthly, you need to set up Apache configuration for the specific virtual host using the ansible.builtin.template module.

Fifthly, you need to start the httpd service and enable it on boot and all the dependant using the ansible.builtin.service Ansible module.

Sixthly you need to open the relevant firewall service-related ports using the ansible.posix.firewalld Ansible module.

## Playbook

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

code

  • httpd_redhat_vhost.yml

```yaml

---

  • name: setup webserver with vhost

hosts: all

become: true

vars:

app_user: "apache"

http_host: "example.com"

http_conf: "example.com.conf"

http_port: "80"

tasks:

- name: httpd installed

ansible.builtin.yum:

name: httpd

state: latest

- name: document root exist

ansible.builtin.file:

path: "/var/www/{{ http_host }}"

state: directory

owner: "{{ app_user }}"

mode: '0755'

setype: "httpd_sys_content_t"

- name: custom index.html

ansible.builtin.copy:

dest: "/var/www/{{ http_host }}/index.html"

content: |

Custom Web Page

- na

Read the full tutorial: Configure Apache Vhost on RedHat Using Ansible Playbook