From a69e59ab2b217ad2874af273c1d87a9b7f749cf9 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Tue, 16 Oct 2012 20:57:37 -0400 Subject: [PATCH] Allows delegate_to to use inventory host/port info. --- lib/ansible/inventory/__init__.py | 2 +- lib/ansible/runner/__init__.py | 24 ++++++++++++++++--- .../runner/connection_plugins/paramiko_ssh.py | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 3e0f44859a..104e61bdd0 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -286,7 +286,7 @@ class Inventory(object): host = self.get_host(hostname) if host is None: - raise Exception("host not found: %s" % hostname) + raise errors.AnsibleError("host not found: %s" % hostname) return host.get_variables() def add_group(self, group): diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 2e7cf510a3..62b9ca432a 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -384,14 +384,32 @@ class Runner(object): actual_host = host try: delegate_to = inject.get('delegate_to', None) - alternative_host = inject.get('ansible_ssh_host', None) + + # the delegated host may have different SSH port configured, etc + # and we need to transfer those, and only those, variables + if delegate_to is not None: + delegate_vars = {} + try: + delegate_vars = inject['hostvars'][delegate_to] + for (k,v) in delegate_vars.iteritems(): + if k.startswith('ansible_ssh_'): + inject[k] = v + except errors.AnsibleError: + # host not listed in inventory, it's ok + pass + + # the host record may just be an alias in case of tunnels + alternative_host = inject.get('ansible_ssh_host', None) + if delegate_to is not None and not alternative_host: actual_host = delegate_to - elif alternative_host is not None: - actual_host = alternative_host + + # connect conn = self.connector.connect(actual_host, port) + if delegate_to is not None or alternative_host is not None: conn._delegate_for = host + except errors.AnsibleConnectionFailed, e: result = dict(failed=True, msg="FAILED: %s" % str(e)) return ReturnData(host=host, comm_ok=False, result=result) diff --git a/lib/ansible/runner/connection_plugins/paramiko_ssh.py b/lib/ansible/runner/connection_plugins/paramiko_ssh.py index b0f8c51e4e..e20e507a57 100644 --- a/lib/ansible/runner/connection_plugins/paramiko_ssh.py +++ b/lib/ansible/runner/connection_plugins/paramiko_ssh.py @@ -53,7 +53,7 @@ class Connection(object): user = self.runner.remote_user - vvv("ESTABLISH CONNECTION FOR USER: %s" % user, host=self.host) + vvv("ESTABLISH CONNECTION FOR USER: %s on PORT %s TO %s" % (user, self.port, self.host), host=self.host) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())