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
- name: setup Apache virtualhost
ansible.builtin.template:
src: "templates/httpd.conf.j2"
dest: "/etc/httpd/conf.d/{{ http_conf }}"
- name: httpd service enabled
ansible.builtin.service:
name: httpd
enabled: true
state: restarted
- name: open firewall
ansible.posix.firewalld:
service: http
state: enabled
immediate: true
permanent: true
`
- templates/httpd.conf.j2
``bash
<VirtualHost *:{{ http_por