General comment for Trellis
If it turns out that the group_vars/all
defaults actually only apply to dev (I haven’t checked), they could be moved somewhere into group_vars/dev
, to avoid the confusion that they don’t apply to remotes.
Global env for @bjn
I haven’t tested, but I’m guessing you could create your custom env as a dictionary in group_vars/all/main.yml
:
custom_env:
env_var_1: value_1
env_var_2: value_2
Then, in your wordpress_sites
for production and staging…
wordpress_sites:
example.com:
...
env: "{{ custom_env }}"
Ansible combine filter examples
The above should avoid having to override the entire wordpress_sites
dictionary. If it doesn’t work, I think you can still use the combine
filter to amend portions of the dictionary (example in Trellis, Ansible docs, other reply at SO).
A while back I tried amending wordpress_sites
. Here are my notes:
I haven’t digested for whether this is a good idea yet, but the combine
filter could reduce redundancy for about half the vars in wordpress_sites
(e.g., local_path
, repo
, branch
, repo_subtree_path
, multisite
, etc.).
Trellis uses with_dict: wordpress_sites
in various places, but it could use…
with_dict: wordpress_sites | combine(wordpress_sites_for_group, recursive=True)
…where wordpress_sites
is defined in group_vars/all
(loaded with lots of values) and wordpress_sites_for_group
is defined in group_vars/<environment>
(only the values that need to be changed for environment/group).
Demo: Create this as combine.yml
and run ansible-playbook combine.yml
- name: Combine filter
gather_facts: false
hosts: localhost
vars:
wordpress_sites:
example.com:
site_hosts:
- example.com
env:
wp_env: production
bunch_of_global_vars: e.g., from group_vars/all
wordpress_sites_for_group:
example.com:
site_hosts:
- example.dev
env:
wp_env: development
specific_vars: e.g., from group_vars/development
tasks:
- debug:
msg: "{{ item }}"
with_dict: wordpress_sites | combine(wordpress_sites_for_group, recursive=True)
Result:
example.com:
site_hosts:
- example.dev
env:
wp_env: development
bunch_of_global_vars: e.g., from group_vars/all
specific_vars: e.g., from group_vars/development
The part to note in the result is that it has both specific_vars
and bunch_of_global_vars
and the other values were overridden by the dev group values.