Skip to content

Commit

Permalink
Disallow duplicate site keys within a host's wordpress_sites (#910)
Browse files Browse the repository at this point in the history
If a user loads both staging and production sites on a single host,
site keys must differ between environments to prevent conflict
that would otherwise occur in resources built from site keys
(e.g., Nginx conf filepaths).
  • Loading branch information
fullyint committed Nov 12, 2017
1 parent 2d53e81 commit 112a887
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,4 +1,5 @@
### HEAD
* Disallow duplicate site keys within a host's `wordpress_sites` ([#910](https://github.com/roots/trellis/pull/910))
* Fix `raw_vars` functionality for Ansible 2.4.1 ([#915](https://github.com/roots/trellis/pull/915))
* Enable Virtualbox ioapic option ([#913](https://github.com/roots/trellis/pull/913))
* Dynamically increase `ansible_group_priority` for selected env ([#909](https://github.com/roots/trellis/pull/909))
Expand Down
20 changes: 20 additions & 0 deletions roles/common/defaults/main.yml
@@ -1,5 +1,25 @@
ntp_timezone: Etc/UTC

env_groups: "{{ ['development', 'staging', 'production'] | intersect(group_names) }}"

envs_with_wp_sites: "{{
lookup('filetree', playbook_dir + '/group_vars') |
selectattr('path', 'match', '(' + env_groups | join('|') + ')/wordpress_sites\\.yml$') |
map(attribute='path') | map('regex_replace', '([^/]*)/.*', '\\1') | list
}}"

site_keys_by_env_pair: "[
{% for env_pair in envs_with_wp_sites | combinations(2) | list %}
{
'env_pair': {{ env_pair }},
'site_keys': {{
(vars[env_pair[0] + '_sites'].wordpress_sites | default({})).keys() | intersect(
(vars[env_pair[1] + '_sites'].wordpress_sites | default({})).keys())
}}
},
{% endfor %}
]"

apt_packages_default:
python-software-properties: "{{ apt_package_state }}"
python-pycurl: "{{ apt_package_state }}"
Expand Down
22 changes: 22 additions & 0 deletions roles/common/tasks/main.yml
@@ -1,4 +1,26 @@
---
- block:
- name: Load wordpress_sites.yml vars into <env>_sites vars
include_vars:
file: group_vars/{{ item }}/wordpress_sites.yml
name: "{{ item }}_sites"
with_items: "{{ envs_with_wp_sites }}"
when: envs_with_wp_sites | count > 1

- name: Fail if there are duplicate site keys within host's wordpress_sites
fail:
msg: >
If you put multiple environments on `{{ inventory_hostname }}`, `wordpress_sites`
must use different site keys per environment. Adjust the following site keys that
are duplicated between the `{{ item.env_pair | join('` and `') }}` groups:
{{ item.site_keys | to_nice_yaml | indent(2) }}
when: item.site_keys | count
with_items: "{{ site_keys_by_env_pair }}"

when:
- env_groups | count > 1
- validate_site_keys | default(true) | bool

- name: Validate wordpress_sites
fail:
msg: "{{ lookup('template', 'wordpress_sites.j2') }}"
Expand Down

0 comments on commit 112a887

Please sign in to comment.