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

Chris F picture Chris F · Nov 22, 2017 · Viewed 10.8k times · Source

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

Answer

Konstantin Suvorov picture Konstantin Suvorov · Nov 23, 2017

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.