Ansible v2.4.0.0
I'm installing Gitlab-CE where I run the following in an Ansible task. As you can see, some of the processes are down, but they eventually come up.
# gitlab-ctl status
run: gitlab-workhorse: 0s, normally up
run: logrotate: 1s, normally up
down: nginx: 0s, normally up
down: postgresql: 1s, normally up
run: redis: 0s, normally up
run: sidekiq: 0s, normally up
run: unicorn: 0s, normally up
How can I write an Ansible wait_for task to check when all the services are in the run state? IOW I only want to proceed to the next task when I see this
# gitlab-ctl status
run: gitlab-workhorse: 0s, normally up
run: logrotate: 1s, normally up
run: nginx: 0s, normally up
run: postgresql: 1s, normally up
run: redis: 0s, normally up
run: sidekiq: 0s, normally up
run: unicorn: 0s, normally up
wait_for - Waits for a condition before continuing. — Ansible Documentation. You are reading an unmaintained version of the Ansible documentation. Unmaintained Ansible versions can contain unfixed security vulnerabilities (CVE).
To pause/wait/sleep per host, use the ansible. builtin. wait_for module. You can use ctrl+c if you wish to advance a pause earlier than it is set to expire or if you need to abort a playbook run entirely.
Both modules execute commands on target nodes but in a sensible different way. The command modules execute commands on the target machine without using the target shell, it simply executes the command. The target shell is for example the popular bash , zsh , or sh .
To capture the output, you need to specify your own variable into which the output will be saved. To achieve this, we use the 'register' parameter to record the output to a variable. Then use the 'debug' module to display the variable's content to standard out.
You can use retries
/until
method:
- command: gitlab-ctl status
register: cmd_res
retries: 5
until: cmd_res.stdout_lines | reject('search','^run') | list | count == 0
We drop any line from output that starts with run
and count them. Proceed to the next task only when there are no such lines left.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With