2

I need to check connectivity of hosts on port 43. I can manually login and check connectivity by:

nc -v `hostname` 43
nc -v `hostname_1` 43

Is there any ansible script that goes through all the servers and saves the output of connectable hosts in one file and not connectable in another file?

2
  • if you add all the hosts in the inventory, you can run the tasks against all of them, catch the ones which return an error and put them into the "bad" group for later. Oct 29 '19 at 14:57
  • yeah i added all of them in inventory, but had trouble writing ansible script and filter them out. using wait_for module Oct 29 '19 at 14:59
2

Maybe a simple shell script would be more useful. If must to use ansible, it could be something like that (assuming you have all servers in group testall):

- hosts: testall
  connection: local
  gather_facts: false
  tasks:

    - wait_for:
        timeout: 2
        port: 43
        host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'
      register: waitfor
      ignore_errors: true

    - copy:
        content: |
          {% for host in groups['testall'] %}
          {% if hostvars[host].waitfor.failed %}{{ hostvars[host].inventory_hostname }} {% endif %}
          {% endfor %}
        dest: /tmp/bad_file
      delegate_to: localhost

    - copy:
        content: |
          {% for host in groups['testall'] %}
          {% if not hostvars[host].waitfor.failed %}{{ hostvars[host].inventory_hostname }} {% endif %}
          {% endfor %}
        dest: /tmp/good_file
      delegate_to: localhost

hosts that have this port opened will be in /tmp/good_file, others are in /tmp/bad_file

2
  • thank you it works Mar 19 '20 at 22:36
  • @user12189187 glad it helped, can you please mark it as a solution in this case?
    – DominiCane
    Mar 31 '20 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.