title: Ansible: Check if service is listening on a specific port
body: <p>How would you go about using Ansible to confirm whether a service is running on a specific port?</p>
  
  <p>For example:</p>
  
  <ul>
  <li>Is Apache running on port 80?</li>
  <li>Is MySQL listening on port 3912?</li>
  <li>Is Tomcat listening on port 8080?</li>
  </ul>
  
  <p>I understand that there are the <code>service</code> and <code>wait_for</code> 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. <code>service</code> and <code>wait_for</code> 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. <code>wait_for</code>, as I understand it, simply checks if it's being used.</p>
  
  <p>There is a <code>regex_search</code> parameter on <code>wait_for</code> 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.</p>
  
  <p>How can we go about this?</p>