Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: Check if service is listening on a specific port

Tags:

linux

ansible

How would you go about using Ansible to confirm whether a service is running on a specific port?

For example:

  • Is Apache running on port 80?
  • Is MySQL listening on port 3912?
  • Is Tomcat listening on port 8080?

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?

like image 293
Jonathan Wells Avatar asked Feb 19 '15 12:02

Jonathan Wells


People also ask

How do I check my service status in ansible?

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.

What port is in use for in ansible?

Ansible Tower uses port 8080 on the Tower server to stream live updates of playbook activity and other events to the client browser.

Can ansible use Telnet?

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.


1 Answers

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.

like image 127
larsks Avatar answered Oct 31 '22 11:10

larsks