# set custom ssh port when using delegate\_to

**URL:** <https://forum.ansible.com/t/set-custom-ssh-port-when-using-delegate-to/34685>\
**Category:** Ansible Project\
**Created:** [February 25, 2021, 8:30pm UTC](https://forum.ansible.com/t/set-custom-ssh-port-when-using-delegate-to/34685 "2021-02-25T20:30:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raphael\_Ebner](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@Raphael\_Ebner](https://forum.ansible.com/u/Raphael_Ebner)\
**Post date:** [February 25, 2021, 8:30pm UTC](https://forum.ansible.com/t/set-custom-ssh-port-when-using-delegate-to/34685/1 "2021-02-25T20:30:18Z")

</div>

Hi Community,

I need to delegate one task in my play to another remote linux ssh host with delegate\_to. So far so good. But now the problem: my remote host has a custom ssh port. When running the play I always get “Failed to connect to the host via ssh: ssh: connect to host x.x.x.x port 22: Connection timed out”. How can I specify at task level a custom ssh port for delegate\_to?

I already tried: “delegate\_to: IP:PORT” and setting “ansible\_port: PORT” at task level, but without success.

May someone has an idea how to handle this problem?

Regards,  
Raphael

---

<div class="post-metadata">

**Author:** ![Juerg\_Ritter](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Juerg\_Ritter](https://forum.ansible.com/u/Juerg_Ritter)\
**Post date:** [February 25, 2021, 8:45pm UTC](https://forum.ansible.com/t/set-custom-ssh-port-when-using-delegate-to/34685/2 "2021-02-25T20:45:52Z")

</div>

Hi Raphael,

Is the host you are trying to delegate the task to part of your inventory?

My next try would be to add this host in the inventory and specify the custom port for this particular host as a host variable. An example can be found here:  
[https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html#assigning-a-variable-to-one-machine-host-variables](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#assigning-a-variable-to-one-machine-host-variables)

I hope that helps!

Regards, Juerg

---

<div class="post-metadata">

**Author:** ![Raphael\_Ebner](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@Raphael\_Ebner](https://forum.ansible.com/u/Raphael_Ebner)\
**Post date:** [February 25, 2021, 9:12pm UTC](https://forum.ansible.com/t/set-custom-ssh-port-when-using-delegate-to/34685/3 "2021-02-25T21:12:48Z")

</div>

Hi Juerg,

Yes, the host is part of my Inventory.  
I tried it and set delegate\_to to the inventory hostname, which woks fine.

But I’ve multiple inventories, and all hosts of each inventory needs this delegated\_to task. How can I handle it for all other inventories, because I can’t add this host to all inventories?  
Have you an idea?

Regards,  
Raphael

---

<div class="post-metadata">

**Author:** ![Juerg\_Ritter](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Juerg\_Ritter](https://forum.ansible.com/u/Juerg_Ritter)\
**Post date:** [February 25, 2021, 9:22pm UTC](https://forum.ansible.com/t/set-custom-ssh-port-when-using-delegate-to/34685/4 "2021-02-25T21:22:47Z")

</div>

Hi Raphael,

Oh I see. One solution might be to use the add\_host module, which allows you to add hosts to your inventory on the fly (it won’t be persisted). Just add a task which adds your target host for the delegation along with the ansible\_port variable before your delegate\_to task.

The documentation can be found here:  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/add\_host\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/add_host_module.html)

Hope this helps!  
– Juerg

---

<div class="post-metadata">

**Author:** ![Raphael\_Ebner](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@Raphael\_Ebner](https://forum.ansible.com/u/Raphael_Ebner)\
**Post date:** [February 26, 2021, 4:38pm UTC](https://forum.ansible.com/t/set-custom-ssh-port-when-using-delegate-to/34685/5 "2021-02-26T16:38:42Z")

</div>

Hi Juerg

thank you very much, it is working fine with add\_host 🙂

Is that worth a feature request, to use remote\_port (similar naming to remote\_user) together with delegate\_to?

Best Regards,  
Raphael

---

<div class="post-metadata">

**Author:** ![Juerg\_Ritter](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Juerg\_Ritter](https://forum.ansible.com/u/Juerg_Ritter)\
**Post date:** [February 26, 2021, 9:13pm UTC](https://forum.ansible.com/t/set-custom-ssh-port-when-using-delegate-to/34685/6 "2021-02-26T21:13:19Z")

</div>

Hi Raphael,

Happy to hear you got it working! I just played around a bit more, and apparently, there is a way even without adding your host to the inventory. Here an example, which I ran on Ansible 2.9.17:

- name: Remote Port Test  
hosts: localhost  
tasks:
- name: delegate command  
command: whoami  
delegate\_to: rhel8  
vars:  
ansible\_ssh\_port: 2222

That said, I still think it is in general a good idea to add all the hosts which the playbook interacts with to the inventory and configure all the connection parameters there. You may wanna use delegate\_to in combination with connection plugins other than SSH (e.g. WinRM), which require different configuration parameters.

Happy weekend and best regards,  
Juerg

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 26, 2021, 9:28pm UTC](https://forum.ansible.com/t/set-custom-ssh-port-when-using-delegate-to/34685/7 "2021-02-26T21:28:21Z")

</div>

Hi!

---

<div class="post-metadata">

**Author:** ![Juerg\_Ritter](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Juerg\_Ritter](https://forum.ansible.com/u/Juerg_Ritter)\
**Post date:** [February 26, 2021, 9:55pm UTC](https://forum.ansible.com/t/set-custom-ssh-port-when-using-delegate-to/34685/8 "2021-02-26T21:55:56Z")

</div>

Hi Vladimir,

I’m not sure what you are getting at… The above example seems to work for me without setting delegate\_facts to True. We are not changing any facts or variables related to the rhel8 host in this example, the variable matters in the context of the task.

– Juerg

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 26, 2021, 11:20pm UTC](https://forum.ansible.com/t/set-custom-ssh-port-when-using-delegate-to/34685/9 "2021-02-26T23:20:47Z")

</div>

It works for me too. I wasn't sure which variables of the two hosts,  
involved in `delegate_to`, will be used. Thank you for the  
clarification.

---

<div class="post-metadata">

**Author:** ![Raphael\_Ebner](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@Raphael\_Ebner](https://forum.ansible.com/u/Raphael_Ebner)\
**Post date:** [February 27, 2021, 9:02am UTC](https://forum.ansible.com/t/set-custom-ssh-port-when-using-delegate-to/34685/10 "2021-02-27T09:02:25Z")

</div>

Hi Juerg,

I tried your example too. It also works in version 2.10.6.

I think for my use case it is the best way to add the remote host during the play with add\_host.

Thank you!  
Happy weekend and best regards,  
Raphael
