Failing to open socket due to port in unicode type

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

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'.

Hi Brian,

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

Doron

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):

            if isinstance(commands, list) and commands:

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

                output =
                try: