# Failing to open socket due to port in unicode type

**URL:** <https://forum.ansible.com/t/failing-to-open-socket-due-to-port-in-unicode-type/30163>\
**Category:** Ansible Project\
**Created:** [April 22, 2019, 9:25pm UTC](https://forum.ansible.com/t/failing-to-open-socket-due-to-port-in-unicode-type/30163 "2019-04-22T21:25:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doron\_B](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@Doron\_B](https://forum.ansible.com/u/Doron_B)\
**Post date:** [April 22, 2019, 9:25pm UTC](https://forum.ansible.com/t/failing-to-open-socket-due-to-port-in-unicode-type/30163/1 "2019-04-22T21:25:46Z")

</div>

Hi Community,

I’m facing some very basic problem. I’m trying to run open telnet session with a device. However, when I’m filling the port dynamically using jinja template, the port is passed as unicode thus I get the following error:  
error: getaddrinfo() argument 2 must be integer or string

This is my playbook, check how the port is assigned. Please note that when Ansible pass on the port value it will use ‘unicode’ type rather than integer or string thus getaddrinfo failes:

`

- hosts: all  
connection: local  
gather\_facts: no  
vars:  
nodes: “{{ node\_list }}”  
tasks:
- name: telnet emulator  
telnet:  
host: 10.8.120.141  
send\_newline: yes  
port: “{{ item.console|int }}”  
user: myuser  
password: mypassword  
login\_prompt: "localhost login: "  
password\_prompt: "Password: "  
prompts:
- “[~#]”  
command:
- ls  
loop: “{{ nodes|flatten(levels=1)|default() }}”  
when: item.node\_type == ‘qemu’

`

This is the inventory file:

`  
10.8.120.141

`

This is how I run the play:

`  
ansible-playbook -vvvv --extra-vars “node\_list=[{‘node\_type’:‘qemu’,‘console’:5022},{‘node\_type’:‘qemu’,‘console’:5002},{‘node\_type’:‘qemu’,‘console’:5004},{‘node\_type’:‘qemu’,‘console’:5020}]” telnet.yaml -i inventory

`

And this is the error I get:

`

# ansible-playbook -vvvv --extra-vars “node\_list=[{‘node\_type’:‘qemu’,‘console’:5022},{‘node\_type’:‘qemu’,‘console’:5002},{‘node\_type’:‘qemu’,‘console’:5004},{‘node\_type’:‘qemu’,‘console’:5020}]” telnet.yaml -i inventory

ansible-playbook 2.7.10  
config file = None  
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]  
ansible python module location = /usr/lib/python2.7/site-packages/ansible  
executable location = /usr/bin/ansible-playbook  
python version = 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]  
No config file found; using defaults  
setting up inventory plugins  
/tmp/inventory did not meet host\_list requirements, check plugin documentation if this is unexpected  
/tmp/inventory did not meet script requirements, check plugin documentation if this is unexpected  
Parsed /tmp/inventory inventory source with ini plugin  
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/default.pyc

PLAYBOOK: telnet.yaml \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
1 plays in telnet.yaml

PLAY [all] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
META: ran handlers

TASK [telnet emulator] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /tmp/telnet.yaml:12  
The full traceback is:  
Traceback (most recent call last):  
File “/usr/lib/python2.7/site-packages/ansible/executor/task\_executor.py”, line 106, in run  
item\_results = self.\_run\_loop(items)  
File “/usr/lib/python2.7/site-packages/ansible/executor/task\_executor.py”, line 343, in \_run\_loop  
res = self.\_execute(variables=task\_vars)  
File “/usr/lib/python2.7/site-packages/ansible/executor/task\_executor.py”, line 612, in \_execute  
result = self.\_handler.run(task\_vars=variables)  
File “/usr/lib/python2.7/site-packages/ansible/plugins/action/telnet.py”, line 61, in run  
tn = telnetlib.Telnet(host, port, timeout)  
File “/usr/lib64/python2.7/telnetlib.py”, line 211, in **init**  
self.open(host, port, timeout)  
File “/usr/lib64/python2.7/telnetlib.py”, line 227, in open  
self.sock = socket.create\_connection((host, port), timeout)  
File “/usr/lib64/python2.7/socket.py”, line 553, in create\_connection  
for res in getaddrinfo(host, port, 0, SOCK\_STREAM):  
error: getaddrinfo() argument 2 must be integer or string

fatal: [10.8.120.141]: FAILED! =\> {  
“msg”: “Unexpected failure during module execution.”,  
“stdout”: “”  
}  
to retry, use: --limit @/tmp/telnet.retry

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
10.8.120.141 : ok=0 changed=0 unreachable=0 failed=1

`

I’ve added print(type(port)) to /usr/lib/python2.7/site-packages/ansible/plugins/action/telnet.py file. This was the printed type:  
\<type ‘unicode’\>

Why is this port value being passed as unicode, even if I try to convert it to integer?

Doron

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 23, 2019, 2:24am UTC](https://forum.ansible.com/t/failing-to-open-socket-due-to-port-in-unicode-type/30163/2 "2019-04-23T02:24:12Z")

</div>

All template results default to unicode strings, this is something  
Jinja2 does by default., but this is probably something we should fix  
in the telnet plugin by forcing port to always be an int.

I'm unsure why telnet lib does not take a 'unicode string' as a 'string'.

---

<div class="post-metadata">

**Author:** ![Doron\_B](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@Doron\_B](https://forum.ansible.com/u/Doron_B)\
**Post date:** [April 23, 2019, 5:21pm UTC](https://forum.ansible.com/t/failing-to-open-socket-due-to-port-in-unicode-type/30163/3 "2019-04-23T17:21:48Z")

</div>

Hi Brian,

Any WA for the meantime? At the moment, I’m unable to use telnet module.

Doron

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 23, 2019, 8:33pm UTC](https://forum.ansible.com/t/failing-to-open-socket-due-to-port-in-unicode-type/30163/4 "2019-04-23T20:33:08Z")

</div>

apply the following patch:

diff --git a/lib/ansible/plugins/action/telnet.py  
b/lib/ansible/plugins/action/telnet.py  
index 785681e0a2..a9aea69b5c 100644  
--- a/lib/ansible/plugins/action/telnet.py  
+++ b/lib/ansible/plugins/action/telnet.py  
@@ -56,7 +56,7 @@ class ActionModule(ActionBase):

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if isinstance(commands, list) and commands:

- tn = telnetlib.Telnet(host, port, timeout)  
+ tn = telnetlib.Telnet(host, int(port), timeout)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output =   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try:
