Ask Your Question

How can ansible be used to check ports for multiple locations with multiple endpoints?

asked 2023-01-25 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-06-07 06:00:00 +0000

pufferfish gravatar image

Ansible can be used to check ports for multiple locations with multiple endpoints by utilizing the built-in wait_for module. This module allows you to specify the endpoint IP address and port to check, as well as the timeout and number of retries.

Here's an example playbook that checks ports for multiple locations with multiple endpoints:

- name: Check ports for multiple locations with multiple endpoints
  hosts: all

      - name: Location 1
      - name: Location 2

    - name: Check ports for endpoints in each location
        host: "{{ endpoint }}"
        port: "{{ port }}"
        state: "started"
        delay: 10
        timeout: 60
        retries: 6
      loop: "{{ locations | json_query('[].endpoints[]') }}"
        loop_var: endpoint

        port: 22  # the port to check on each endpoint

      register: port_checks

    - name: Display results of port checks
        msg: "Endpoint {{ item.item }} in {{ item.location }}: {{ item.status }}"
      loop: "{{ port_checks.results }}"
        label: "{{ item.item }}"
          location: "{{ locations | json_query('[?endpoints.contains(@, `%s`)].name' % item.item) | first }}"
          status: "{{ 'Success' if item.rc == 0 else 'Failure' }}"

In this playbook, we define a list of locations that each have their own list of endpoints. We then loop over each endpoint and use the wait_for module to check if the specified port is open. We register the results of each check and then display them in a debug message.

To run this playbook, you would need to specify the hosts to run it on and make any necessary modifications to the locations and port variables.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2023-01-25 11:00:00 +0000

Seen: 1 times

Last updated: Jun 07 '22