Search code examples
ansibletelnetexit-codeansible-2.x

Ansible - telnet fails - command / shell module - rc return code 1 failed: true


This question does have some history from this post: Auto exit Telnet command back to prompt without human intervention ^] quit close exit code 1

Btw, I'm trying to test a port using telnet command where the port can be OPEN or NOT. How can I do the same in Ansible?

I tried the following but (whether port is open or not open/it's invalid), Ansible always says it 'failed': true in the results or output dictionary variable and return code 'rc': 1

I tried command module and shell module as well but it didn't work.

HOW should I do to get Ansible run this telnet command successfully so that next actions (defined in the playbook's tasks) can work.

My playbook snippet:

- name: Ensure collector agent's proxy_address and proxy_port is open
  #command: "echo -e \"^]\\nclose\"| telnet {{ proxy_address }} {{ proxy_port }}"
  #shell: "echo -e \"^]\\nclose\" | telnet `hostname` {{ proxy_port }}"
  shell: "telnet `hostname` {{ proxy_port }}"
  register: telnet_result
  ignore_errors: yes
  tags:
   - giga

- debug: msg="telnet output result was = {{ telnet_result }}"
  tags:
   - giga

Running ansible-playbook using the following command, gives me the follwoing error (if I use either of the shell commands listed above).

PS: File myplaybook.yml is just calling a role mytask.

ansible-playbook -i inventory -l localhost myplaybook.yml --extra-vars "proxy_port=2878" -t giga -vvv

Output:

TASK [mytask : Ensure collector agent's proxy_address and proxy_port is open] ***
task path: /home/vagrant/aks/ansible/roles/mytask/tasks/install_telegraf.yml:3
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: vagrant
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488 `" && echo ansible-tmp-1483580659.49-61726964686488="` echo $HOME/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488 `" ) && sleep 0'
<localhost> PUT /tmp/tmpoVYTlY TO /home/vagrant/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488/command
<localhost> EXEC /bin/sh -c 'chmod u+x /home/vagrant/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488/ /home/vagrant/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488/command && sleep 0'
<localhost> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-asjgqzogcakikhnvwamwruivfbutphtz; LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/vagrant/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488/command; rm -rf "/home/vagrant/.ansible/tmp/ansible-tmp-1483580659.49-61726964686488/" > /dev/null 2>&1'"'"' && sleep 0'
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "telnet `hostname` 2878", "delta": "0:00:00.006042", "end": "2017-01-05 01:44:19.590362", "failed": true, "invocation": {"module_args": {"_raw_params": "telnet `hostname` 2878", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "command"}, "rc": 1, "start": "2017-01-05 01:44:19.584320", "stderr": "Connection closed by foreign host.", "stdout": "Trying 127.0.0.1...\r\nConnected to myvagrant.\r\nEscape character is '^]'.", "stdout_lines": ["Trying 127.0.0.1...", "Connected to myvagrant.", "Escape character is '^]'."], "warnings": []}
...ignoring

TASK [mytask : debug] *************************************
task path: /home/vagrant/aks/ansible/roles/mytask/tasks/install_telegraf.yml:12
ok: [localhost] => {
    "msg": "telnet output result was = {u'changed': True, u'end': u'2017-01-05 01:44:19.590362', u'stdout': u\"Trying 127.0.0.1...\\r\\nConnected to myvagrant.\\r\\nEscape character is '^]'.\", u'cmd': u'telnet `hostname` 2878', 'failed': True, u'delta': u'0:00:00.006042', u'stderr': u'Connection closed by foreign host.', u'rc': 1, 'stdout_lines': [u'Trying 127.0.0.1...', u'Connected to myvagrant.', u\"Escape character is '^]'.\"], u'start': u'2017-01-05 01:44:19.584320', u'warnings': []}"
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0   

Solution

  • failed_when: <your_condition>, for example:

    failed_when: false
    

    Besides, you seem like trying to mimic wait_for module.