From 3add67b42230ad0a1c4fc2de14878b32d3861f44 Mon Sep 17 00:00:00 2001 From: Phil Nelson Date: Sun, 29 Oct 2017 00:56:04 -0600 Subject: [PATCH] Dynamically increase ansible_group_priority for selected env Otherwise when a host is in both the production and staging groups, Ansible will always use the staging group vars, even if a user specifies `-e env=production`. Without a differing ansible_group_priority, Ansible loads sibling groups in alphabetical order and variables from the last group loaded win out. --- CHANGELOG.md | 1 + lib/trellis/plugins/callback/vars.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5271f18bd9..7e565cd8c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ### HEAD +* Dynamically increase `ansible_group_priority` for selected env ([#909](https://github.com/roots/trellis/pull/909)) * Bump Ansible `version_tested_max` to 2.4.1.0 ([#911](https://github.com/roots/trellis/pull/911)) * Update wp-cli to 1.4.0 ([#906](https://github.com/roots/trellis/pull/906)) * [BREAKING] Normalize `apt` tasks ([#881](https://github.com/roots/trellis/pull/881)) diff --git a/lib/trellis/plugins/callback/vars.py b/lib/trellis/plugins/callback/vars.py index 42942bdca1..98773de710 100644 --- a/lib/trellis/plugins/callback/vars.py +++ b/lib/trellis/plugins/callback/vars.py @@ -89,6 +89,11 @@ def darwin_without_passlib(self): return True def v2_playbook_on_play_start(self, play): + env = play.get_variable_manager().get_vars(play=play).get('env', '') + env_group = next((group for key,group in play.get_variable_manager()._inventory.groups.iteritems() if key == env), False) + if env_group: + env_group.set_priority(20) + for host in play.get_variable_manager()._inventory.list_hosts(play.hosts[0]): hostvars = play.get_variable_manager().get_vars(play=play, host=host) self.raw_vars(play, host, hostvars)