Reputation: 103
I am trying to use ansible to download a certificate from an API, and here is what I'm using:
- name: Get Shipa API address
shell: kubectl --namespace=shipa-system get svc shipa-ingress-nginx -o jsonpath="{.status.loadBalancer.ingress[0].ip}"
environment:
TERM: dumb
register: shipa_api
until: shipa_api.stdout | ipaddr
retries: 5
delay: 90
This works great to wait until the API IP is available and on the next step, I do this:
- name: Add Shipa as a target on CLI
shell: shipa target add {{ cluster_name }} {{ shipa_apiaddr }} -s
args:
executable: /bin/bash
no_log: False
This step calls that API IP and tries to retrieve a certificate from IP, but the problem is that even though the IP may be available, the API service might not be running yet, so I get this as a response:
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "shipa target add s12-rc4-t2 35.247.97.88 -s", "delta": "0:00:00.164987", "end": "2020-12-18 11:53:23.177987", "msg": "non-zero return code", "rc": 1, "start": "2020-12-18 11:53:23.013000", "stderr": "Error: Get \"https://35.247.97.88:8081/certificates/ca\": dial tcp 35.247.97.88:8081: connect: connection refused", "stderr_lines": ["Error: Get \"https://35.247.97.88:8081/certificates/ca\": dial tcp 35.247.97.88:8081: connect: connection refused"], "stdout": "", "stdout_lines": []}
Is there a way for Ansible to check if the API service is available before trying the next step "Add Shipa as a target on CLI" so it doesn't fail?
Upvotes: 2
Views: 1296
Reputation: 39119
You can use the module wait_for
in order to wait for a specific IP/port couple to accept connections.
So something like:
- wait_for:
host: 35.247.97.88
port: 8081
delay: 10
So in your case, I guess:
- wait_for:
## Depending on what is in the variable shipa_apiaddr
## (just the ip or ip:port)
# host: "{{ shipa_apiaddr.split(':').0 }}"
host: "{{ shipa_apiaddr }}"
## Depending on what is in the variable shipa_apiaddr
## (just the ip or ip:port)
## Not sure how you should prevent hardcoding it based on your question
# port: "{{ shipa_apiaddr.split(':').1 }}"
port: 8081
delay: 10
Upvotes: 3