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_urlAnd 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 timeout
seconds. 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
How to Add an Icon to The Bash Prompt
How to Redirect Ip Address Using Iptables
How to Access Environment Variables Inside .Gdbinit and Inside Gdb Itself
Why I Am Not Getting Signal Sigkill on Kill -9 Command in Bash
Is There Any Difference Between Executable Binary Files Between Distributions
Automatically Sync Two Amazon S3 Buckets, Besides S3Cmd
How to Remount The /Proc Filesystem in a Docker as a R/W System
What Does '-Oom-Kill-Disable' Do for a Docker Container
How to Parse Http Headers Using Bash
How to Compile Intel MAC Binaries on Linux
Expect, Interact and Then Again Expect
What's The Difference Between Insmod and Modprobe
How to Redirect Http to Https Using Gcp Load Balancer
Bash Script That Creates a Directory Structure
In Linux, Physical Memory Pages Belong to The Kernel Data Segment Are Swappable or Not