From 4172d68dc3f07c61b8b0ea96b5de2cc0c7413948 Mon Sep 17 00:00:00 2001 From: Sloane Hertel Date: Mon, 11 Mar 2019 11:43:31 -0500 Subject: [PATCH] use composed vars in constructed groups (#53152) * changelog * combine provided variables and host vars inside of constructing groups to take into account composed variables let composed variables "win" * fix whitespace * Allow user to control hash behavior --- .../53152-create-groups-using-composed-variables.yaml | 2 ++ lib/ansible/plugins/inventory/__init__.py | 3 +++ 2 files changed, 5 insertions(+) create mode 100644 changelogs/fragments/53152-create-groups-using-composed-variables.yaml diff --git a/changelogs/fragments/53152-create-groups-using-composed-variables.yaml b/changelogs/fragments/53152-create-groups-using-composed-variables.yaml new file mode 100644 index 0000000000..7afb92d4a1 --- /dev/null +++ b/changelogs/fragments/53152-create-groups-using-composed-variables.yaml @@ -0,0 +1,2 @@ +bugfixes: + - inventory plugins - Fix creating groups from composed variables by getting the latest host variables diff --git a/lib/ansible/plugins/inventory/__init__.py b/lib/ansible/plugins/inventory/__init__.py index 8bc9950fe4..45a8917c93 100644 --- a/lib/ansible/plugins/inventory/__init__.py +++ b/lib/ansible/plugins/inventory/__init__.py @@ -34,6 +34,7 @@ from ansible.module_utils.parsing.convert_bool import boolean from ansible.module_utils.six import string_types from ansible.template import Templar from ansible.utils.display import Display +from ansible.utils.vars import combine_vars display = Display() @@ -366,6 +367,7 @@ class Constructable(object): ''' helper to create complex groups for plugins based on jinja2 conditionals, hosts that meet the conditional are added to group''' # process each 'group entry' if groups and isinstance(groups, dict): + variables = combine_vars(variables, self.inventory.get_host(host).get_vars()) self.templar.set_available_variables(variables) for group_name in groups: conditional = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % groups[group_name] @@ -389,6 +391,7 @@ class Constructable(object): for keyed in keys: if keyed and isinstance(keyed, dict): + variables = combine_vars(variables, self.inventory.get_host(host).get_vars()) try: key = self._compose(keyed.get('key'), variables) except Exception as e: