Answers: <p>Based on <a href="https://devops.stackexchange.com/a/1276/210">@13nilux' answer</a> the following code has been created:</p>
  
  <pre><code>- name: Check whether port 80 is available
    wait_for:
      port: 80
      state: stopped
      timeout: 10
  </code></pre>
  
  <p>When port 80 is listening the run will fail if for example nginx is listening:</p>
  
  <pre><code>TASK [role_under_test : Check whether port 80 is available] ********************
  
  fatal: [localhost]: FAILED! =&gt; {"changed": false, "elapsed": 10, "failed": true, "msg": "Timeout when waiting for 127.0.0.1:80 to stop."}
  
   [WARNING]: Could not create retry file
  
  '/etc/ansible/roles/role_under_test/tests/test.retry'.         [Errno 30] Read-
  
  only file system: u'/etc/ansible/roles/role_under_test/tests/test.retry'
  </code></pre>
  
Answers: CC BY-SA 3.0
Answers: 2017-06-05T16:26:53.783Z
Answers: 2017-06-05T16:32:01.057Z
Answers: 2017-06-05T16:32:01.057Z
Answers: 030
Answers: <p>Letsencrypt is long-time renamed to certbot. You can automate certificate renewal process by configuring a host which serves a domain(s) by installing certbot and configuring web server for handling a directory with auth tokens as <code>/.well-known/acme-challenge/</code> for domains under Let's Encrypt-signed certs, then using Ansible playbook for <a href="https://certbot.eff.org/docs/using.html#pre-and-post-validation-hooks" rel="nofollow noreferrer">manual renewal process</a> or run the same command by cron.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-06-05T09:45:56.723Z
Answers: yeah, but what happens if port 80 is unavailable? Then the certificate renewal process fails
Answers: 030
Answers: CC BY-SA 3.0
Answers: 2017-06-05T12:35:59.447Z
Answers: @030 manual mode doesn't require any ports, because it's manual. `http` method uses existing web server.
Answers: chupasaurus
Answers: CC BY-SA 3.0
Answers: 2017-06-05T14:10:05.363Z
Answers: Where do the `/path/to/http/authenticator.sh` and `/path/to/http/cleanup.sh` reside? Perhaps you could add this to the question to prevent that that information will be lost if the link is deprecated.
Answers: 030
Answers: CC BY-SA 3.0
Answers: 2017-06-05T15:17:19.097Z
Answers: @030 These are user-defined scripts which could be anywhere (with example content provided), but actual implementation could vary depenpding on method and web server configuration.
Answers: chupasaurus
Answers: CC BY-SA 3.0
Answers: 2017-06-05T03:54:58.457Z
Answers: 2017-06-05T03:54:58.457Z
Answers: chupasaurus
Answers: <p>I think you're looking for the <a href="http://docs.ansible.com/ansible/wait_for_module.html" rel="noreferrer"><code>wait_for</code> module</a>. It will allow you to check and make an action when a certain port is available and more.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-06-05T10:08:57.793Z
Answers: the state `stopped` should be used?
Answers: 030
Answers: CC BY-SA 3.0
Answers: 2017-06-05T10:23:15.823Z
Answers: if I understand correctly your particular use case - yes.
Answers: 13dimitar
Answers: CC BY-SA 3.0
Answers: 2017-06-02T13:35:09.423Z
Answers: 2017-06-05T06:07:47.107Z
Answers: 2017-06-05T06:07:47.107Z
Answers: 13dimitar
Body: <p><a href="http://docs.ansible.com/ansible/list_of_all_modules.html" rel="noreferrer">The Ansible module index</a> has been consulted, but no module was found that checks whether a port is available? Is there a module that supports this?</p>
  
  <p><strong>What does <code>available</code> mean?</strong></p>
  
  <p>Available means that a port, e.g. <code>80</code> is not in LISTEN state. The following indicates that port 111 is unavailable.</p>
  
  <pre><code>user@localhost ~ $ ss -nat
  State      Recv-Q Send-Q Local Address:Port    Peer Address:Port              
  LISTEN     0      128       *:111                   *:*
  </code></pre>
  
  <p><strong>What is the aim of this question?</strong></p>
  
  <p>The aim of this question is to find a way to stop an Ansible run when port 80 is already allocated by another process than certbot. Certbot requires port 80 to be available, otherwise the cert cannot be renewed. The issue can be found <a href="https://github.com/030/ansible-certbot/issues/15" rel="noreferrer">here</a>.</p>
  
Tags: <ansible><certbot>
Title: Is there an ansible module that check whether a port is not in LISTEN state?
UserDisplayName: 030
ThreadText: # Is there an ansible module that check whether a port is not in LISTEN state?
  
  (Asked by: username_0 on 2017-06-02)
  
  [The Ansible module index](http://docs.ansible.com/ansible/list_of_all_modules.html) has been consulted, but no module was found that checks whether a port is available? Is there a module that supports this?
  
  **What does `available` mean?**
  
  Available means that a port, e.g. `80` is not in LISTEN state. The following indicates that port 111 is unavailable.
  
  ```
  user@localhost ~ $ ss -nat
  State      Recv-Q Send-Q Local Address:Port    Peer Address:Port              
  LISTEN     0      128       *:111                   *:*
  
  ```
  
  **What is the aim of this question?**
  
  The aim of this question is to find a way to stop an Ansible run when port 80 is already allocated by another process than certbot. Certbot requires port 80 to be available, otherwise the cert cannot be renewed. The issue can be found [here](https://github.com/030/ansible-certbot/issues/15).
  
  **Tags:** ansible, certbot
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_2 on 2017-06-02. Score: 12 | Accepted answer
  
  I think you're looking for the [`wait_for` module](http://docs.ansible.com/ansible/wait_for_module.html). It will allow you to check and make an action when a certain port is available and more.
  
  > **Comments:**
  > **username_0**: the state `stopped` should be used?  
  > **username_2**: if I understand correctly your particular use case - yes.  
  
  ---
  
  ### Answer by username_0 on 2017-06-05. Score: 4
  
  Based on [@13nilux' answer](https://devops.stackexchange.com/a/1276/210) the following code has been created:
  
  ```
  - name: Check whether port 80 is available
    wait_for:
      port: 80
      state: stopped
      timeout: 10
  
  ```
  
  When port 80 is listening the run will fail if for example nginx is listening:
  
  ```
  TASK [role_under_test : Check whether port 80 is available] ********************
  
  fatal: [localhost]: FAILED! => {"changed": false, "elapsed": 10, "failed": true, "msg": "Timeout when waiting for 127.0.0.1:80 to stop."}
  
   [WARNING]: Could not create retry file
  
  '/etc/ansible/roles/role_under_test/tests/test.retry'.         [Errno 30] Read-
  
  only file system: u'/etc/ansible/roles/role_under_test/tests/test.retry'
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2017-06-05. Score: -1
  
  Letsencrypt is long-time renamed to certbot. You can automate certificate renewal process by configuring a host which serves a domain(s) by installing certbot and configuring web server for handling a directory with auth tokens as `/.well-known/acme-challenge/` for domains under Let's Encrypt-signed certs, then using Ansible playbook for [manual renewal process](https://certbot.eff.org/docs/using.html#pre-and-post-validation-hooks) or run the same command by cron.
  
  > **Comments:**
  > **username_0**: yeah, but what happens if port 80 is unavailable? Then the certificate renewal process fails  
  > **username_1**: @username_0 manual mode doesn't require any ports, because it's manual. `http` method uses existing web server.  
  > **username_0**: Where do the `/path/to/http/authenticator.sh` and `/path/to/http/cleanup.sh` reside? Perhaps you could add this to the question to prevent that that information will be lost if the link is deprecated.  
  > **username_1**: @username_0 These are user-defined scripts which could be anywhere (with example content provided), but actual implementation could vary depenpding on method and web server configuration.  
  
  ---
  
  