AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Set Up Apache Vhost on Debian with Ansible Playbook — Video Tutorial
Learn to configure an Apache virtual host on Debian using Ansible. Automate web server setup, document root creation, and firewall rules.
What You'll Learn
- How to deploy a webserver apache httpd virtual host on Debian-like systems with Ansible?
- Deploy a web server apache httpd virtual host on Debian-like systems
- code
- execution
- (almost) idempotency
- before execution
- after execution
- Conclusion
- Related Articles
Full Tutorial Content
How to deploy a webserver apache httpd virtual host on Debian-like systems with Ansible?
I'm going to show you a live Playbook with some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Deploy a web server apache httpd virtual host on Debian-like systems
- install packages => ansible.builtin.apt
- document root => ansible.builtin.file
- custom index.html => ansible.builtin.copy
- Apache virtualhost => ansible.builtin.template
- enable new site => ansible.builtin.command
- open firewall => community.general.ufw
- reload service => ansible.builtin.service
Today we're talking about how to Deploy a web server apache httpd on Debian-like Linux systems.
The full process requires seven steps that you could automate with different Ansible modules.
Firstly you need to install the `apache2` package and dependency using the `ansible.builtin.apt` 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 the `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.
Fifty, you need to enable a new site using the `a2ensite` via the `ansible.builtin.command` module.
Sixty, you need to start the `apache2` service and enable it on boot and all the dependant using the `ansible.builtin.service` Ansible module.
Seventy you need to open the relevant firewall service-related ports using the `community.general.ufw` Ansible module.
## Playbook
How to automate the deployment of a web server apache httpd virtual host on Debian-like systems with Ansible Playbook.
code
- httpd_debian_vhost.yml
```yaml
---
- name: setup webserver vhost
hosts: all
become: true
vars:
app_user: "www-data"
http_host: "example.com"
http_conf: "example.com.conf"
http_port: "80"
disable_default: true
tasks:
- name: apache installed
ansible.builtin.apt:
name: apache2
update_cache: true
state: latest
- name: document root exist
ansible.builtin.file:
path: "/var/www/{{ http_host }}"
state: directory
owner: "{{ app_user }}"
mode: '0755'
- name: custom index.html
ansible.builtin.copy:
dest: "/var/www/{{ http_host }}/index.html"
content: |
Custom Web Page
- name: set up Apache virtualhost
ansible.builtin.template:
src: "templates/apache.conf.j2"
dest: "/etc/apache2/sites-available/{{ http_conf }}"
- name: enable new site
ansible.builtin.command: "/usr/sbin/a2ensite {{ http_conf }}"
notify: reload Apache
- name: disable default Apache site
ansible.builtin.command: "/usr/sbin/a2dissite 000-default.conf"
when: disable_default
notify: reload Apache
- name: open firewa
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 7 min
- Category: installation
Read the full written article: Set Up Apache Vhost on Debian with Ansible Playbook
Related Video Tutorials
- Deploy Apache HTTPD on Docker Container with Ansible — Automate the deployment of Apache HTTPD in a Docker container using Ansible. Manage system packages, Docker images, and web content effortlessly.
- Set Up Apache Webserver on Debian with Ansible Playbook — Learn to deploy and configure an Apache web server on Debian using Ansible. Automate installation, custom index.html setup, and firewall rules.
- Deploy Apache HTTPD with Podman Using Ansible Playbook — Automate Apache HTTPD deployment in a Podman container with Ansible. Manage Podman installation, image pulling, and container configuration.
- Deploy Apache HTTPD Container with Podman via Ansible — Automate the deployment of Apache HTTPD in a Podman container using Ansible. Set up web root, custom index.html, and manage container settings.
- Efficient Web Server Setup Using Ansible Playbook — Set up a web server effortlessly with our Ansible playbook tutorial. Follow step-by-step instructions for installing and configuring HTTPD service.
- 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.