Ask Your Question
4

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
2

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

  vars:
    locations:
      - name: Location 1
        endpoints:
          - 192.168.1.10
          - 192.168.1.20
          - 192.168.1.30
      - name: Location 2
        endpoints:
          - 192.168.2.10
          - 192.168.2.20
          - 192.168.2.30

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

      vars:
        port: 22  # the port to check on each endpoint

      register: port_checks

    - name: Display results of port checks
      debug:
        msg: "Endpoint {{ item.item }} in {{ item.location }}: {{ item.status }}"
      loop: "{{ port_checks.results }}"
      loop_control:
        label: "{{ item.item }}"
        vars:
          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

Stats

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

Seen: 2 times

Last updated: Jun 07 '22