From 41aaad69aabd4b874797e58b9ddb5548bea34345 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Fri, 11 Oct 2013 16:36:48 -0400 Subject: [PATCH] Make add_host clear the inventory pattern cache, add some more aliases. Fixes #4442. --- lib/ansible/inventory/__init__.py | 4 ++++ lib/ansible/runner/action_plugins/add_host.py | 8 ++++++-- library/inventory/add_host | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index a02c9936fd..6a9e6b79cc 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -266,6 +266,10 @@ class Inventory(object): self._pattern_hosts[pattern] = results return self._pattern_hosts[pattern] + def clear_pattern_cache(self): + ''' called exclusively by the add_host plugin to allow patterns to be recalculated ''' + self._pattern_hosts = {} + def groups_for_host(self, host): results = [] groups = self.get_groups() diff --git a/lib/ansible/runner/action_plugins/add_host.py b/lib/ansible/runner/action_plugins/add_host.py index 5f379fdb72..98814346b5 100644 --- a/lib/ansible/runner/action_plugins/add_host.py +++ b/lib/ansible/runner/action_plugins/add_host.py @@ -68,14 +68,14 @@ class ActionModule(object): # only groups can be added directly to inventory inventory._hosts_cache[new_name] = new_host allgroup.add_host(new_host) - + # Add any variables to the new_host for k in args.keys(): if not k in [ 'name', 'hostname', 'groupname', 'groups' ]: new_host.set_variable(k, args[k]) - groupnames = args.get('groupname', args.get('groups', '')) + groupnames = args.get('groupname', args.get('groups', args.get('group', ''))) # add it to the group if that was specified if groupnames != '': for group_name in groupnames.split(","): @@ -88,6 +88,10 @@ class ActionModule(object): result['new_groups'] = groupnames.split(",") result['new_host'] = new_name + + # clear pattern caching completely since it's unpredictable what + # patterns may have referenced the group + inventory.clear_pattern_cache() return ReturnData(conn=conn, comm_ok=True, result=result) diff --git a/library/inventory/add_host b/library/inventory/add_host index 75c602324a..4fd4e1eb15 100644 --- a/library/inventory/add_host +++ b/library/inventory/add_host @@ -10,12 +10,12 @@ description: version_added: "0.9" options: name: - aliases: [ 'hostname' ] + aliases: [ 'hostname', 'host' ] description: - The hostname/ip of the host to add to the inventory, can include a colon and a port number. required: true groups: - aliases: [ 'groupname' ] + aliases: [ 'groupname', 'group' ] description: - The groups to add the hostname to, comma separated. required: false