vic
vic

Reputation: 45

looping over shell commands in ansible

I have to restart systemd unit in my playbook and wait until service is active, so my "check and wait" logic must be as following in "pseudo-code":

for iterations=0; iterations < 10; iterations++ {
    status = systemctl is-active myservice
        if (status == active)
            break
        sleep 3s
    }
}
if status != active {
    failure: exit playbook
}

Seems like in ansible playbook i can iterate over the results of shell command but after looking into "loops" in documentation: http://docs.ansible.com/ansible/latest/playbooks_loops.html#iterating-over-the-results-of-a-program-execution I couldn't find how to repeat command itself, based on output. Can it be done?

Upvotes: 1

Views: 3777

Answers (2)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68329

There are do-until loops in Ansible:

- shell: systemctl is-active myservice
  register: results
  until: results | success
  retries: 10
  delay: 3

Upvotes: 4

gile
gile

Reputation: 6006

I would use service or systemd and wait_for modules for this purpose.

Upvotes: 0

Related Questions