How would you go about using Ansible to confirm whether a service is running on a specific port?
For example:
I understand that there are the service
and wait_for
commands, which individually check if a service is running and if a port is in use - but I've not found anything so far to check if a particular service is listening on a particular port. service
and wait_for
will indicate there's a service and a port, but there's no guarantee that the port is taken by that particular service - it could be taken by anything. wait_for
, as I understand it, simply checks if it's being used.
There is a regex_search
parameter on wait_for
which mentions searching in a socket connection for a particular string, but as I understand it this is simply reading any information that comes down that socket rather than having any access to what is sending that information.
How can we go about this?
Just run the task service: name=httpd state=started with the option --check . This tells you, if the service needs to be started, which means that it is down. If the task shows no change, it is up already. Save this answer.
Ansible Tower uses port 8080 on the Tower server to stream live updates of playbook activity and other events to the client browser.
As documented here, ansible does not support telnet as connection plugin. This means you cannot use ansible to connect to a remote machine via telnet and execute any of the ansible modules.
There are a couple of ways of interpreting your question, so I'm going to try to answer them both:
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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With