0

Below are couple of IP addresses and their telnet response (output)

telnet 10.9.9.112 22
Trying 10.9.9.112......

telnet 10.9.9.143 22
Trying 10.9.9.143.....
telnet: connect to address 10.9.9.143: Connection refused.

For the first IP 10.9.9.112 there is no connection and firewall blocks any connection from source to destination. The output simply says Trying .... and stays that way without printing anything else.

For the second IP 10.9.9.143 i get Connection refused immediately in the output and the control back to the prompt.

I wish to grab both scenarios in when condition and perform different activities for both the cases.

I tried to use Ansible's telnet module but I don't know how to grab both the different outputs in the registered variable.

In my case it prints the same message for both the IPs.

Ansible output for first ip:

TASK [debug] *************************************
ok: [localhost] => {
   "msg": "HERE:{u'msg': u'Timeout when waiting for 10.9.9.112', u'failed': True, 'changed': False, u'elapsed': 4}"

Ansible Output for second ip:

TASK [debug] *************************************
ok: [localhost] => {
   "msg": "HERE:{u'msg': u'Timeout when waiting for 10.9.9.143', u'failed': True, 'changed': False, u'elapsed': 3}"

The only difference I see is the value for elapsed.

Here is my playbook.

---
- name: "Play 1"
  hosts: localhost
  tasks: 
    - wait_for:
        hosts: "{{ item }}"
        port: 22
        state: started
        delay: 3
        timeout: 90
      ignore_errors: yes
      register: telnetout
      loop:
        - 10.9.9.112
        - 10.9.9.143

    - debug:
        msg: "HERE: {{ telnetout }}"
Ashar
  • 2,942
  • 10
  • 58
  • 122
  • Looks like, because your timeout is too short, (`timeout: 3`) it **always** times out and you never ends up in the cases you produced via your shell trials. – β.εηοιτ.βε Oct 06 '20 at 13:35
  • Note that the default is [`300`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/wait_for_module.html#parameter-timeout) and the value is expressed in sconds, so waiting only 3 seconds seems really short. – β.εηοιτ.βε Oct 06 '20 at 13:38
  • @β.εηοιτ.βε I increased the timeout to 90 which is sufficient however, the ansible `telnet` module still does not get `Connection refused` in the output. So, i guess that is not the solution. – Ashar Oct 06 '20 at 14:15
  • Do you still get the same output? If not, could you revise your question? – β.εηοιτ.βε Oct 06 '20 at 14:16
  • Yes, i get the same output .... The problem persists.@β.εηοιτ.βε – Ashar Oct 06 '20 at 14:32
  • @β.εηοιτ.βε should i fallback to `raw` module instead of `telnet` module? – Ashar Oct 06 '20 at 17:19
  • Maybe use the [`telnet`](https://docs.ansible.com/ansible/2.9/modules/telnet_module.html) module to start with? – β.εηοιτ.βε Oct 06 '20 at 18:36
  • @β.εηοιτ.βε There is no answer to the issue i raised here with the `telnet` module. How will using the `telnet` module help unless we have some sort of solution ? – Ashar Oct 06 '20 at 19:23

1 Answers1

0

telnet module unfortunately does not record Connection Refused message in the output.

We have to use raw module instead like below.

---
- name: "Play 1"
  hosts: localhost
  tasks: 
    - raw: "timeout --signal=9 2 telnet {{ item }} 22"
      ignore_errors: yes
      register: telnetout
      loop:
        - 10.9.9.112
        - 10.9.9.143

- debug:
    msg: "HERE: {{ telnetout }}"
Ashar
  • 2,942
  • 10
  • 58
  • 122