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?
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?
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.
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:
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.
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.