Ansible: Check If Service Is Listening on a Specific Port

Ansible: Check if service is listening on a specific port

There are a couple of ways of interpreting your question, so I'm going to try to answer them both:

Verifying a network service

If your goal is to verify that a particular port is serving a particular application protocol, I would check this by running an appropriate client.

  • For checking Apache and Tomcat, I would GET a specific url and check the result code. For example:

    - name: check if apache is running
    command: curl -sf http://webserver/check_url

    And similarly for Tomcat.

  • For checking MySQL, I would use the MySQL client:

    - name: check if mysql is running
    command: mysql -h dbhost -P dbport -e 'select 1'

Verifying what process owns a socket

If you actually wanted to see what process was holding a particular port open, I guess you could combine ss and grep, but that seems weird and unnecessary. Something like:

    - name: check if httpd has port 80 open
shell: ss -tp state listening sport = :80 | grep httpd

If you want to check a particular process id, you could so something similar with lsof:

    - name: check that pid {{apache_pid}} is listening on port 80
shell: lsof -p 1036 -P | grep 'TCP \*:80'

But again, I don't necessarily find these options particularly useful. The service checks in the earlier section seem to be more appropriate.

Test if a server is reachable from host and has port open with Ansible

There is wait_for module for this.

To check that target.host can access remote.host:8080:

- hosts: target.host
tasks:
- wait_for: host=remote.host port=8080 timeout=1
- debug: msg=ok

There are a lot of other examples in the documentation.

How to use the listen_ports_facts module, ansible

The gather_facts stage of your playbook runs uses the setup module. It does not run listen_ports_facts, so if you don't run the module explicitly you won't have those facts available.

From the docs, the listen_ports_module creates the following facts:

  • tcp_listen
  • udp_listen

Using Ansible 2.9.2, the following works just fine:

---
- gather_facts: false
hosts: localhost

tasks:
- listen_ports_facts:

- debug:
msg: "{{ tcp_listen }}"

- debug:
msg: "{{ udp_listen }}"

If you're trying to run the listen_ports_facts module and you're getting the error "no action detected in task", it may be that you're running an older version of Ansible that doesn't have the listen_ports_facts module. It first showed up in version 2.9.

Ansible Playbook should stop execution if port is open

You could use this task to check if the application is already running. If running, it will abort the playbook.

  - name: Check if service is running by querying the application port
wait_for:
port: 22
timeout: 10
state: stopped
msg: "Port 22 is accessible, application is already installed and running"
register: service_status

Basically, you are using the module with state: stopped, and ansible expects the port to somehow stop listening for timeoutseconds. if port stays up for 10 seconds (it will stay up since nobody stops the already installed application), ansible will exit with error.

change port to 23, you will see playbook would continue to next step:

  tasks:
- name: Check if service is running by querying the application port
wait_for:
port: 23
timeout: 10
state: stopped
msg: "Port 23 is accessible, application is already installed and running"
register: service_status

- name: print
debug:
var: service_status

you dont need the register clause, just added for my test.

hope it helps

Ansible: How to get service status by Ansible?

Use command module with service redis-server status and parse stdout.

Or use patched service module.



Related Topics



Leave a reply



Submit