Skip to content

Commit

Permalink
Disallow duplicate site keys within a host's wordpress_sites
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 Oct 30, 2017
1 parent dd4f154 commit fb588dd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
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 fb588dd

Please sign in to comment.