# How to run a command/task on another host and use output on current target hosts

**URL:** <https://forum.ansible.com/t/how-to-run-a-command-task-on-another-host-and-use-output-on-current-target-hosts/15241>\
**Category:** Ansible Project\
**Created:** [May 15, 2014, 12:34am UTC](https://forum.ansible.com/t/how-to-run-a-command-task-on-another-host-and-use-output-on-current-target-hosts/15241 "2014-05-15T00:34:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![edrozenberg](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@edrozenberg](https://forum.ansible.com/u/edrozenberg)\
**Post date:** [May 15, 2014, 12:34am UTC](https://forum.ansible.com/t/how-to-run-a-command-task-on-another-host-and-use-output-on-current-target-hosts/15241/1 "2014-05-15T00:34:37Z")

</div>

Hello,

I’m working through implementing the following scenario:

- A central software repo host stores various software packages

- Use Ansible to install the latest version of a particular package on  
various target hosts by downloading the latest package version from  
the repo host to the target hosts and installing it.

In order to do this the playbook needs to do the following:

1. Get the latest package name/version from the software repo host  
using a command like:

find . -name "{{ pkgname }}-[0-9]_" -print | sort | tail -1 | sed 's,\[1\]_/,’

1. Using the results of the above command (for ex. apache-2.4.1-x86\_64-1.txz),  
do a wget on the target hosts and then install this package.

I have not been able to figure out how to get this result data from another host to  
use in the playbooks being applied to the target hosts. I looked at the “delegate\_to”  
option - if that is indeed the correct approach, I was not able to get it to work due  
to an error I’m getting when using “delegate\_to: myhost:portnum” (note - portnum for  
the delegate host is different from the portnum for the current target host).

fatal: [myhost] =\> SSH Error: data could not be sent to the remote host.  
Make sure this host can be reached over ssh FATAL: all hosts have already failed – aborting

Thanks,  
–Ed

* * *

1. ^/

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 15, 2014, 1:30am UTC](https://forum.ansible.com/t/how-to-run-a-command-task-on-another-host-and-use-output-on-current-target-hosts/15241/2 "2014-05-15T01:30:23Z")

</div>

Can you please share the output of an ansible-playbook run with “-vvvv” when you hit this scenario.

Output from the last task will be sufficient.

Please also include the version of ansible you are using.

---

<div class="post-metadata">

**Author:** ![edrozenberg](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@edrozenberg](https://forum.ansible.com/u/edrozenberg)\
**Post date:** [May 15, 2014, 1:44am UTC](https://forum.ansible.com/t/how-to-run-a-command-task-on-another-host-and-use-output-on-current-target-hosts/15241/3 "2014-05-15T01:44:10Z")

</div>

Hello Michael,

This is using 1.7 git.

The output is as follows:

PLAY [work] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
 ESTABLISH CONNECTION FOR USER: root  
 REMOTE\_MODULE setup  
 EXEC [‘ssh’, ‘-C’, ‘-vvv’, ‘-o’, ‘ControlMaster=auto’, ‘-o’, ‘ControlPersist=60s’, ‘-o’, ‘ControlPath=/Users/eduardr/.ansible/cp/ansible-ssh-%h-%p-%r’, ‘-o’, ‘KbdInteractiveAuthentication=no’, ‘-o’, ‘PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey’, ‘-o’, ‘PasswordAuthentication=no’, ‘-o’, ‘User=root’, ‘-o’, ‘ConnectTimeout=10’, ‘work1’, u"/bin/sh -c ‘LC\_CTYPE=en\_US.UTF-8 LANG=en\_US.UTF-8 /usr/bin/python’"]  
ok: [work1]

TASK: [basic | software directory create] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
 ESTABLISH CONNECTION FOR USER: root  
 REMOTE\_MODULE file path=/srv/software owner=‘root’ group=‘root’ mode=0755 state=‘directory’  
 EXEC [‘ssh’, ‘-C’, ‘-vvv’, ‘-o’, ‘ControlMaster=auto’, ‘-o’, ‘ControlPersist=60s’, ‘-o’, ‘ControlPath=/Users/eduardr/.ansible/cp/ansible-ssh-%h-%p-%r’, ‘-o’, ‘KbdInteractiveAuthentication=no’, ‘-o’, ‘PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey’, ‘-o’, ‘PasswordAuthentication=no’, ‘-o’, ‘User=root’, ‘-o’, ‘ConnectTimeout=10’, ‘work1’, u"/bin/sh -c ‘LC\_CTYPE=en\_US.UTF-8 LANG=en\_US.UTF-8 /usr/bin/python’"]  
ok: [work1] =\> {“changed”: false, “gid”: 0, “group”: “root”, “item”: “”, “mode”: “0755”, “owner”: “root”, “path”: “/srv/software”, “size”: 4096, “state”: “directory”, “uid”: 0}

TASK: [basic | package find name of latest version] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
repo1:12022 ESTABLISH CONNECTION FOR USER: root  
repo1:12022 REMOTE\_MODULE command find . -name “ansible-[0-9]_" -print | sort | tail -1 | sed 's,\[1\]_/,’ chdir=/srv/software/slackware/14.1 #USE\_SHELL  
repo1:12022 EXEC [‘ssh’, ‘-C’, ‘-vvv’, ‘-o’, ‘ControlMaster=auto’, ‘-o’, ‘ControlPersist=60s’, ‘-o’, ‘ControlPath=/Users/eduardr/.ansible/cp/ansible-ssh-%h-%p-%r’, ‘-o’, ‘KbdInteractiveAuthentication=no’, ‘-o’, ‘PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey’, ‘-o’, ‘PasswordAuthentication=no’, ‘-o’, ‘User=root’, ‘-o’, ‘ConnectTimeout=10’, ‘-6’, ‘repo1:12022’, u”/bin/sh -c ‘LC\_CTYPE=en\_US.UTF-8 LANG=en\_US.UTF-8 /usr/bin/python’"]  
fatal: [work1] =\> SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh

FATAL: all hosts have already failed – aborting

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
to retry, use: --limit @/Users/eduardr/site.yaml.retry

work1 : ok=2 changed=0 unreachable=1 failed=0

Thanks,  
–Ed

* * *

1. ^/

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 15, 2014, 11:27am UTC](https://forum.ansible.com/t/how-to-run-a-command-task-on-another-host-and-use-output-on-current-target-hosts/15241/4 "2014-05-15T11:27:14Z")

</div>

Ah.

““delegate\_to: myhost:portnum””

Delegate\_to takes a hostname but is not meant to take “hostname:port” syntax. Rather, define the ansible host record in inventory and configure the portnumber.

–Michael

---

<div class="post-metadata">

**Author:** ![edrozenberg](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@edrozenberg](https://forum.ansible.com/u/edrozenberg)\
**Post date:** [May 15, 2014, 11:24pm UTC](https://forum.ansible.com/t/how-to-run-a-command-task-on-another-host-and-use-output-on-current-target-hosts/15241/5 "2014-05-15T23:24:21Z")

</div>

Hello Michael,

Thanks! That worked great. Thought I’d tried that previously but I might have had hostname.domain in the inventory and only hostname in delegate\_to so it didn’t match perfectly.

Glad to have found the delegate\_to functionality - absolutely necessary in certain cases. One of those things you know you need when you need it :). Had I read about it previously in the manual it would have been hard to imagine what it could be used for.

Regards,  
–Ed
