ansible/ansible — pull request #7048 by renard (2014-04-18 00:08:50+00:00)
Title: Honor ansible_private_key_file for delegated hosts

##### Issue Type:

Bugfix Pull Request

##### Ansible Version:

All

##### Environment:

N/A

##### Summary:

When running playbooks in an inventory-less environment
ansible_private_key_file is not honored for delegated hosts. This
patch fixed that issue.

##### Steps To Reproduce:

Simple playbook:

````
- hosts: "all"
user: ansible
sudo: yes
sudo_user: root

vars:
- ansible_ssh_private_key_file: ../.ssh/ansible

tasks:
- name: test delegated
ping: ""
delegate_to: delegated-host.example.com
````

`ansible-playbook -i target.example.com, test.yml -vvvvv` will produce:

##### Expected Results:

With the patch:

`````
TASK: [test delegated] ********************************************************
ESTABLISH CONNECTION FOR USER: ansible
REMOTE_MODULE ping
EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'IdentityFile=../.ssh/ansible', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=ansible', '-o', 'ConnectTimeout=10', 'delegated-host.example.com', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1397779436.94-6033870923105 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1397779436.94-6033870923105 && echo $HOME/.ansible/tmp/ansible-tmp-1397779436.94-6033870923105'"]
PUT /tmp/tmpTF1ZSn TO /var/lib/ansible/.ansible/tmp/ansible-tmp-1397779436.94-6033870923105/ping
EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'IdentityFile=../.ssh/ansible', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=ansible', '-o', 'ConnectTimeout=10', 'delegated-host.example.com', '/bin/sh -c \'sudo -k && sudo -H -S -p "[sudo via ansible, key=bioohyqkycfxhtrpgdosyaoyveusieag] password: " -u root /bin/sh -c \'"\'"\'echo SUDO-SUCCESS-bioohyqkycfxhtrpgdosyaoyveusieag; /usr/bin/python /var/lib/ansible/.ansible/tmp/ansible-tmp-1397779436.94-6033870923105/ping; rm -rf /var/lib/ansible/.ansible/tmp/ansible-tmp-1397779436.94-6033870923105/ >/dev/null 2>&1\'"\'"\'\'']
ok: [target.example.com] => {"changed": false, "ping": "pong"}
`````

##### Actual Results:

Without the patch

`````
GATHERING FACTS ***************************************************************
ESTABLISH CONNECTION FOR USER: ansible
REMOTE_MODULE setup
EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'IdentityFile=../.ssh/ansible', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=ansible', '-o', 'ConnectTimeout=10', 'target.example.com', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1397779303.75-87764599171703 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1397779303.75-87764599171703 && echo $HOME/.ansible/tmp/ansible-tmp-1397779303.75-87764599171703'"]
PUT /tmp/tmpMstUct TO /var/lib/ansible/.ansible/tmp/ansible-tmp-1397779303.75-87764599171703/setup
EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'IdentityFile=../.ssh/ansible', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=ansible', '-o', 'ConnectTimeout=10', 'target.example.com', '/bin/sh -c \'sudo -k && sudo -H -S -p "[sudo via ansible, key=baqrymoikypfmsraluxihemembjubbrg] password: " -u root /bin/sh -c \'"\'"\'echo SUDO-SUCCESS-baqrymoikypfmsraluxihemembjubbrg; /usr/bin/python /var/lib/ansible/.ansible/tmp/ansible-tmp-1397779303.75-87764599171703/setup; rm -rf /var/lib/ansible/.ansible/tmp/ansible-tmp-1397779303.75-87764599171703/ >/dev/null 2>&1\'"\'"\'\'']
ok: [target.example.com]

TASK: [test delegated] ********************************************************
ESTABLISH CONNECTION FOR USER: ansible
REMOTE_MODULE ping
EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=ansible', '-o', 'ConnectTimeout=10', 'delegated-host.example.com', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1397779307.22-52434613811698 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1397779307.22-52434613811698 && echo $HOME/.ansible/tmp/ansible-tmp-1397779307.22-52434613811698'"]
`````

The delegated host does not have the `'-o', 'IdentityFile=../.ssh/ansible'` ssh option set.

### Commit log

If a delegated host is not found in the inventory specified
private_key_file for primary host was not used.

This allows running playbooks without having to define any inventory at
all and to use the same ssh private key for both primary host and
delegated one.