Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Ansible wait_for to check a command status with multiple lines?

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
like image 443
Chris F Avatar asked Nov 22 '17 20:11

Chris F


People also ask

What is Wait_for in Ansible?

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).

How do you add delays in Ansible?

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.

What is the difference between shell and command in Ansible?

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 .

How do you find the output in Ansible?

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.


1 Answers

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.

like image 179
Konstantin Suvorov Avatar answered Oct 22 '22 23:10

Konstantin Suvorov