patternMinor
Check connectivity of 5000 + servers from ansible and get list of hosts which are connectable and which are not
Viewed 0 times
connectableconnectivitycheckhostsserversarewhichget5000and
Problem
I need to check connectivity of hosts on port 43. I can manually login and check connectivity by:
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?
nc -v `hostname` 43
nc -v `hostname_1` 43Is 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?
Solution
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
hosts that have this port opened will be in
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: localhosthosts that have this port opened will be in
/tmp/good_file, others are in /tmp/bad_fileCode Snippets
- 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: localhostContext
StackExchange DevOps Q#9606, answer score: 2
Revisions (0)
No revisions yet.