pulse-mind.ansible-role-apache-vhosts
Ansible Role: Apache vhosts
Purpose
This role works with the geerlingguy.apache role, along with jdauphant.nginx and viasite-ansible.site.
The jdauphant.nginx
role manages websites in separate files and can easily remove sites.
Geerlingguy mentioned the reason for having a single vhosts.conf
file.
The viasite-ansible.site
role sets up sites in a complex way, including user creation and configuring services like nginx, apache, and mysql. Each site is treated as its own role, so I need separate apache vhosts for each site as well.
This role allows you to use the geerlingguy.apache
role differently.
Other roles called "apache-vhosts" that I have found are too complex, similar to how viasite-ansible.site
works.
Features
- Add websites using several playbooks
- Explicitly remove websites
- Each website can use a different template
Limitations
- Only works with Debian, Ubuntu, and CentOS
- SSL is not configured (we handle SSL with nginx)
Feel free to report any issues!
Usage
Before using this role, you need to install Apache with the geerlingguy.apache
role or another method.
Some variable names and values match those in geerlingguy's role (check the default and vars directories), so you don't have to define them twice.
Adding Sites:
Define the site as follows:
apache_vhosts_sites:
www.local.dev:
servername: "www.local.dev"
serveralias: "local.dev"
documentroot: "/var/www/html"
extra_parameters: |
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This is similar to how geerlingguy defines it:
apache_vhosts_sites:
- servername: "www.local.dev"
serveralias: "local.dev"
documentroot: "/var/www/html"
extra_parameters: |
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Custom Template for a Site:
apache_vhosts_sites:
www.local.dev:
servername: "www.local.dev"
template: "custom.conf.j2"
Removing Sites:
To remove a site, use:
apache_vhosts_remove_sites:
- www.local.dev
Example Playbook
- hosts: all
roles:
- viasite-ansible.apache-vhosts
vars:
apache_vhosts_sites:
foo:
servername: "local.dev"
documentroot: "/var/www/html"
bar:
servername: "local2.dev"
documentroot: "/var/www/html"
templated_site:
template: tests/custom_template.conf.j2
servername: "other.dev"
somevariable: "somevalue"
apache_vhosts_remove_sites:
- baz
ansible-galaxy install pulse-mind.ansible-role-apache-vhosts