7

The Ansible module index 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.

  • 3
    What does "available" mean? If there is a service listening on that port? If there is not? If it's in the acceptable range? – Xiong Chiamiov Jun 2 '17 at 15:02
  • 1
    Ok, the next question is, why do you want to know? What are you using this information to do? – Xiong Chiamiov Jun 4 '17 at 19:15
  • @XiongChiamiov updated – 030 Jun 4 '17 at 21:07
10

I think you're looking for the wait_for module. It will allow you to check and make an action when a certain port is available and more.

  • the state stopped should be used? – 030 Jun 5 '17 at 10:08
  • 1
    if I understand correctly your particular use case - yes. – 13dimitar Jun 5 '17 at 10:23
1

Based on @13nilux' answer 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'
-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 or run the same command by cron.

  • yeah, but what happens if port 80 is unavailable? Then the certificate renewal process fails – 030 Jun 5 '17 at 9:45
  • @030 manual mode doesn't require any ports, because it's manual. http method uses existing web server. – chupasaurus Jun 5 '17 at 12:35
  • 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. – 030 Jun 5 '17 at 14:10
  • @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. – chupasaurus Jun 5 '17 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.