Here are a few things that I have tried, which all failed:
1. Use the 'port' play attribute with a variable:
- hosts: all
port: $ssh_port
The same approach works with hosts, e.g. hosts: $hosts, but not with port, which gives me:
ValueError: invalid literal for int() with base 10: '${ssh_port}'
This would have been nice, especially if it could be overridden using the --extra-vars CLI option.
2. Use the --remote-port CLI option. Found out the
option was removed, although --user is available.
Like the OP, I want to set arbitrary ports for my hosts while still being able to bootstrap them when they are first deployed using the default ssh port. I want ssh tasks to still work using the new port so it can keep checking that everything is in place and sshd_config hasn't changed since initial deployment. After all, ansible's ability to bootstrap machines is one of its most attractive features. Inflexible ssh port setting is an annoyance.
Editing the inventory every time an initial deployment is done is just annoying.
I think a --port option would complement the --user option nicely and keep the playbooks intact. It should override any ports set in the inventory.
Any other solutions that do not involve editing the inventory?