Mat Wilson
Mat Wilson

Reputation: 25

Ansible: How to loop a command and ignore the return code?

I'm trying to loop a command and wait for a certain phrase in stdout, but the task will prematurely fail because the command will return "1" if it can't connect. How can I make it ignore the return code?

- name: Wait for Fabric Creation to complete
  action: shell /usr/local/jboss/jboss-fuse/bin/client container-list
  register: result
  until: result.stdout.find("success")
  retries: 20
  delay: 10

Upvotes: 2

Views: 3868

Answers (1)

techraf
techraf

Reputation: 68459

It's not really a problem with the command failing (although the task result will be based on the return code of the shell script - this can be altered with failed_when).

The real problem is in the following condition itself:

until: result.stdout.find("success")

find returns -1 value if no match is found, so a boolean check actually passes when there is no success string in the stdout. The condition in until is thus met on the first run.


You need to change the condition to:

- name: Wait for Fabric Creation to complete
  action: shell /usr/local/jboss/jboss-fuse/bin/client container-list
  register: result
  until: result.stdout.find("success") != -1
  retries: 20
  delay: 10

Upvotes: 1

Related Questions