Skip to content

Commit

Permalink
Make add_host clear the inventory pattern cache, add some more aliases.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael DeHaan committed Oct 11, 2013
1 parent 9c9ec52 commit 41aaad6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/ansible/inventory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ def _hosts_in_unenumerated_pattern(self, pattern):
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()
Expand Down
8 changes: 6 additions & 2 deletions lib/ansible/runner/action_plugins/add_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **
# 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(","):
Expand All @@ -88,6 +88,10 @@ def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **
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)

Expand Down
4 changes: 2 additions & 2 deletions library/inventory/add_host
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 41aaad6

Please sign in to comment.