Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow duplicate site keys within a host's wordpress_sites #910

Merged
merged 1 commit into from
Nov 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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