Skip to content

Commit

Permalink
Use delegated_to field for play context remote_addr, if none other ex…
Browse files Browse the repository at this point in the history
…ists

Fixes ansible#12527
  • Loading branch information
jimi-c committed Sep 30, 2015
1 parent 40bdf09 commit 3bd71d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/ansible/playbook/play_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@

__all__ = ['PlayContext']

try:
from __main__ import display
display = display
except ImportError:
from ansible.utils.display import Display
display = Display()

# the magic variable mapping dictionary below is used to translate
# host/inventory variables to fields in the PlayContext
# object. The dictionary values are tuples, to account for aliases
Expand Down Expand Up @@ -296,6 +303,16 @@ def set_task_and_variable_override(self, task, variables, templar):
# the host name in the delegated variable dictionary here
delegated_host_name = templar.template(task.delegate_to)
delegated_vars = variables.get('ansible_delegated_vars', dict()).get(delegated_host_name, dict())
# make sure this delegated_to host has something set for its remote
# address, otherwise we default to connecting to it by name. This
# may happen when users put an IP entry into their inventory, or if
# they rely on DNS for a non-inventory hostname
for address_var in MAGIC_VARIABLE_MAPPING.get('remote_addr'):
if address_var in delegated_vars:
break
else:
display.warning("no remote address found for delegated host %s, using its name by default" % delegated_host_name)
delegated_vars['ansible_host'] = delegated_host_name
else:
delegated_vars = dict()

Expand Down
1 change: 1 addition & 0 deletions lib/ansible/vars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ def get_vars(self, loader, play=None, host=None, task=None, include_hostvars=Tru
include_delegate_to=False,
include_hostvars=False,
)

all_vars['ansible_delegated_vars'] = delegated_host_vars

if self._inventory is not None:
Expand Down

0 comments on commit 3bd71d0

Please sign in to comment.