set custom ssh port when using delegate_to

1,506 views
Skip to first unread message

Raphael Ebner

unread,
Feb 25, 2021, 3:30:18 PM2/25/21
to Ansible Project
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

Juerg Ritter

unread,
Feb 25, 2021, 3:46:25 PM2/25/21
to ansible...@googlegroups.com, rap...@ebner-sachrang.de
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:

I hope that helps!

Regards, Juerg

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/fbd59df7-f298-4f7a-90a0-64b7f94f8a24n%40googlegroups.com.


--

Jürg Ritter, RHCE

Consultant

Red Hat Switzerland

Europaallee 41

CH-8021 Zürich

jri...@redhat.com
M: +41-(0)79-715-09-90

Raphael Ebner

unread,
Feb 25, 2021, 4:12:49 PM2/25/21
to Ansible Project
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

Juerg Ritter

unread,
Feb 25, 2021, 4:23:15 PM2/25/21
to ansible...@googlegroups.com
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:

Hope this helps!
-- Juerg



Raphael Ebner

unread,
Feb 26, 2021, 11:38:42 AM2/26/21
to Ansible Project
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

Juerg Ritter

unread,
Feb 26, 2021, 4:13:48 PM2/26/21
to ansible...@googlegroups.com
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

Vladimir Botka

unread,
Feb 26, 2021, 4:28:39 PM2/26/21
to Juerg Ritter, ansible...@googlegroups.com
Hi!

On Fri, 26 Feb 2021 22:13:19 +0100
Juerg Ritter <jri...@redhat.com> wrote:

> - name: Remote Port Test
> hosts: localhost
> tasks:
> - name: delegate command
> command: whoami
> delegate_to: rhel8
> vars:
> ansible_ssh_port: 2222

Intuitively, I'd expect `delegate_facts: True` is needed to change the
port of rhel8.
https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#delegating-facts

--
Vladimir Botka

Juerg Ritter

unread,
Feb 26, 2021, 4:56:24 PM2/26/21
to Vladimir Botka, ansible...@googlegroups.com
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

Vladimir Botka

unread,
Feb 26, 2021, 6:21:06 PM2/26/21
to Juerg Ritter, ansible...@googlegroups.com
> > On Fri, Feb 26, 2021 at 10:28 PM Vladimir Botka <vbo...@gmail.com> wrote:
> > Intuitively, I'd expect `delegate_facts: True` is needed to change the
> > port of rhel8.

> On Fri, 26 Feb 2021 22:55:56 +0100
> Juerg Ritter <jri...@redhat.com> wrote:
> 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.

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.

--
Vladimir Botka

Raphael Ebner

unread,
Feb 27, 2021, 4:02:25 AM2/27/21
to Ansible Project
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

Reply all
Reply to author
Forward
0 new messages