# Changing ssh port with Ansible

**URL:** <https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [November 12, 2012, 3:56pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080 "2012-11-12T15:56:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 12, 2012, 3:56pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/1 "2012-11-12T15:56:32Z")

</div>

Nope (and also, do not want this), you'll have to update your  
inventory file to reference the new ports.

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [November 12, 2012, 7:06pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/2 "2012-11-12T19:06:11Z")

</div>

Michael DeHaan wrote:

> Nope (and also, do not want this), you'll have to update your  
> inventory file to reference the new ports.

You can do this on your own by creating an action\_plugin that returns a  
dict containing {'ansible\_facts': {'ansible\_ssh\_port': int(module\_args)}}  
for instance.

Daniel

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 12, 2012, 7:17pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/3 "2012-11-12T19:17:25Z")

</div>

Though why would you? Go write a plugin when you can edit a file?

-- Michael

---

<div class="post-metadata">

**Author:** ![Ahmad\_Khayyat](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Ahmad\_Khayyat](https://forum.ansible.com/u/Ahmad_Khayyat)\
**Post date:** [November 20, 2012, 8:42am UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/4 "2012-11-20T08:42:14Z")

</div>

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.

1. Use the --remote-port CLI option. Found out the [option was removed](https://groups.google.com/d/topic/ansible-project/NnnVSATK6HQ/discussion), 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?

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [November 20, 2012, 1:12pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/5 "2012-11-20T13:12:13Z")

</div>

It means the value was not templated, and because of that it was not an integer. I'll look into creating two patches, one for templating and the other for making sure the exception is properly handled as well.

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [November 20, 2012, 6:27pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/6 "2012-11-20T18:27:36Z")

</div>

Pull-requests:

&nbsp;&nbsp;- #1649 ([https://github.com/ansible/ansible/pull/1649](https://github.com/ansible/ansible/pull/1649))  
&nbsp;&nbsp;- #1650 ([https://github.com/ansible/ansible/pull/1650](https://github.com/ansible/ansible/pull/1650))

Kind regards,

---

<div class="post-metadata">

**Author:** ![Ahmad\_Khayyat](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Ahmad\_Khayyat](https://forum.ansible.com/u/Ahmad_Khayyat)\
**Post date:** [November 20, 2012, 6:54pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/7 "2012-11-20T18:54:03Z")

</div>

Thanks Dag.  
Will check it out.

---

<div class="post-metadata">

**Author:** ![Ahmad\_Khayyat](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Ahmad\_Khayyat](https://forum.ansible.com/u/Ahmad_Khayyat)\
**Post date:** [November 21, 2012, 9:09am UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/8 "2012-11-21T09:09:54Z")

</div>

I tried a fresh checkout which includes this patch:

> - #1649 ([https://github.com/ansible/ansible/pull/1649](https://github.com/ansible/ansible/pull/1649))

(Allow the remote port to be templated per play)

I still get the message:

site-packages/ansible/runner/ **init**.py", line 384, in \_executor\_internal\_inner  
actual\_port = int(actual\_port)  
ValueError: invalid literal for int() with base 10: ‘${ssh\_port}’

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [November 21, 2012, 10:46am UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/9 "2012-11-21T10:46:45Z")

</div>

Right, at this point only variables defined in the playbook are taken into account. I made a new patch moving the templating down to the runner.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[https://github.com/ansible/ansible/pull/1651](https://github.com/ansible/ansible/pull/1651)

And this time I tested it with host variables, not just playbook variables.

Thanks for the feedback,

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [November 21, 2012, 11:43am UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/10 "2012-11-21T11:43:32Z")

</div>

Ok, the official word is that using #1649 you can now do -e 'ssh\_port=22', or you can use ansible\_ssh\_port variable. The change in #1651 will not be merged. (Plenty of options, no need to create another one)

---

<div class="post-metadata">

**Author:** ![Ahmad\_Khayyat](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Ahmad\_Khayyat](https://forum.ansible.com/u/Ahmad_Khayyat)\
**Post date:** [November 21, 2012, 5:52pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/11 "2012-11-21T17:52:39Z")

</div>

Thanks Dag..

I commented on the pull request, explaining my use case. Here it is again  
for people following this thread:

The use case that I still cannot implement with current options is: by  
default use a non-standard ssh port, but allow resetting it back to port 22  
from the command line.

Can I do this without this patch?

I was hoping I can use every host's or group's ssh\_port as set in their  
host\_vars or group\_vars, then override them from the command line using -e  
ssh\_port=22.

---

<div class="post-metadata">

**Author:** ![Ahmad\_Khayyat](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Ahmad\_Khayyat](https://forum.ansible.com/u/Ahmad_Khayyat)\
**Post date:** [November 21, 2012, 8:15pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/12 "2012-11-21T20:15:52Z")

</div>

Thanks Dag…

I commented on the pull request, explaining my use case. Here it is again for people following this thread:

The use case that I still cannot implement with current options is: by default use a non-standard ssh port, but allow resetting it back to port 22 from the command line.

Can I do this without this patch?

I was hoping I can use every host’s or group’s ssh\_port as set in their host\_vars or group\_vars, then override them from the command line using -e ssh\_port=22.

---

<div class="post-metadata">

**Author:** ![Ahmad\_Khayyat](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Ahmad\_Khayyat](https://forum.ansible.com/u/Ahmad_Khayyat)\
**Post date:** [November 21, 2012, 11:12pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/13 "2012-11-21T23:12:56Z")

</div>

For future reference, I’d just like to point it that there is a way around this now.

You can set your default ports in the inventory, and override them from the command line using the --extra-vars option:  
ansible-playbook […] -e ansible\_ssh\_port=22

This used not to affect GATHERING FACTS (which may appear as though it did not affect anything since the failure would abort execution), but not it does, thanks to daniel\_hozac.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 23, 2012, 3:51pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/14 "2012-11-23T15:51:57Z")

</div>

FYI -- It's ansible\_ssh\_port, not ssh\_port

---

<div class="post-metadata">

**Author:** ![Ahmad\_Khayyat](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Ahmad\_Khayyat](https://forum.ansible.com/u/Ahmad_Khayyat)\
**Post date:** [November 23, 2012, 5:34pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/15 "2012-11-23T17:34:39Z")

</div>

Well, ssh\_port is defined in my group\_vars, and I was trying to use it, but  
'port: $ssh\_port' in the playbook doesn't work.  
I now do not have 'port:' in the playbook. Instead, I set my non-standard  
default port in the inventory and override it using -e ansible\_ssh\_port,  
which used not to work (GATHERING FACTS ignored it) but now it does work.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 23, 2012, 6:26pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/16 "2012-11-23T18:26:28Z")

</div>

Banana\_tree\_firetruck as a variable also does not work.

– Michael

---

<div class="post-metadata">

**Author:** ![Ahmad\_Khayyat](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Ahmad\_Khayyat](https://forum.ansible.com/u/Ahmad_Khayyat)\
**Post date:** [November 23, 2012, 6:34pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/17 "2012-11-23T18:34:44Z")

</div>

It's ansible\_ssh\_port, not Banana\_tree\_firetruck.

What are you getting at?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 23, 2012, 7:38pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/18 "2012-11-23T19:38:34Z")

</div>

You left off the ansible\_ which matters

– Michael

---

<div class="post-metadata">

**Author:** ![Ahmad\_Khayyat](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Ahmad\_Khayyat](https://forum.ansible.com/u/Ahmad_Khayyat)\
**Post date:** [November 23, 2012, 7:52pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/19 "2012-11-23T19:52:17Z")

</div>

I left it off initially because I was trying to use my own variable,  
ssh\_port, which I define in the group\_vars file tree, and use in the  
sshd\_config template. I was hoping I could reuse the same variable to  
specify the ssh connection port.

I am not trying to do that anymore, and am satisfied with ansible\_ssh\_port,  
although my attempts uncovered an issue with ansible\_ssh\_port, where it is  
not used for the GATHERING FACTS task, which happens to be the first task,  
so when it fails, execution aborts, and it \*looks\* like ansible\_ssh\_port is  
not being recognized at all. With that issue fixed (ansible\_ssh\_port is  
used for GATHERING FACTS now), ansible\_ssh\_port does what I want, and  
ssh\_port (my own variable) is only used for templating.

I also realize now that using the same template variable for connections  
will not work: if a remote machine is initially using a different port,  
trying to change it by setting my template variable to a different port  
will cause the connection to fail (if it uses the same variable to connect).

I hope I clarified things. Again, I was not expecting ssh\_port to do what  
ansible\_ssh\_port does. I was trying to over-use my own variable for things  
it shouldn't be doing.

---

<div class="post-metadata">

**Author:** ![Lee\_S](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@Lee\_S](https://forum.ansible.com/u/Lee_S)\
**Post date:** [November 29, 2012, 8:41pm UTC](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080/20 "2012-11-29T20:41:07Z")

</div>

I’ve found I can’t override ansible\_ssh\_port at all. My hosts file defines ansible\_ssh\_port at the top, set to my custom port.

I’ve tried overriding it to set it 22 for new hosts on the new host’s line in the hosts file, and also on the command line using -e ansible\_ssh\_port=22, and neither of these changes seem to have an effect. It always connects to the ansible\_ssh\_port defined at the top of the hosts file.

[Next page](https://forum.ansible.com/t/changing-ssh-port-with-ansible/12080.md?page=2)
