failed_when sequence logic

81 views
Skip to first unread message

Tom Paine

unread,
Jul 1, 2015, 2:31:14 PM7/1/15
to ansible...@googlegroups.com
Having trouble understanding if there's a bug in failed_when, or my understanding is incorrect -

a false and a true are OR'ing in a YAML sequence to false:

Fails (failed when evals to true):
      failed_when:
        - '"300 OK" not in command_result.stderr'


Passes (failed when evals to false):
      failed_when:
        - command_result.rc != 0

Both together pass (failed when evals to false):
      failed_when:
        - '"300 OK" not in command_result.stderr'
        - command_result.rc != 0


Spent an hour going back a forth trying to find a syntax error. failed_when passes the task when it evaluates to false, I've seen a post from Michael DeHaan saying sequences are treated like ORs.

What am I not seeing?

Matt Martz

unread,
Jul 1, 2015, 2:46:05 PM7/1/15
to ansible...@googlegroups.com
When you give when, changed_when or failed_when a list, the items are ANDed, not ORed.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c8528625-14dc-40dc-b3c7-b10c8b617c0e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Tom Paine

unread,
Jul 1, 2015, 2:46:55 PM7/1/15
to ansible...@googlegroups.com
Simplified, I've tried:

failed_when:
  - true
  - false

evals to false. Surely a bug?

This is the post where sequences are said to evaluate as OR

Matt Martz

unread,
Jul 1, 2015, 2:50:57 PM7/1/15
to ansible...@googlegroups.com
No, pretty sure that is correct, here is the output from the operation of 'True and False' in Python:

In [1]: True and False
Out[1]: False

Like I said, they are ANDed not ORed.

If you want to use an or, you are going to have to write it out such as:

failed_when:  true or false



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Tom Paine

unread,
Jul 1, 2015, 3:02:43 PM7/1/15
to ansible...@googlegroups.com
Not convinced it's good syntax, for a failure_when I'd expect an OR.


But ok.

My issue is that I now I want to OR a bunch of multiline statements:

      failed_when:
        - >
          "long string with characters that need escaping"
          over many $%^&*
          lines
        - >
          and another
          long
          line
        - etc
        - etc

How to OR over multiline sequences?

Tom Paine

unread,
Jul 1, 2015, 3:20:24 PM7/1/15
to ansible...@googlegroups.com
Ok, got it.

failed_when: >
 
"foo
 bar"

 
not in x or
 
"baz
 bar"

 
not in y

etc. Thanks for the help Matt. Hottest July day on record with no air-con wasn't helping either :)
Reply all
Reply to author
Forward
0 new messages