Thinking abouta better 'only_if' ... just not doing it quite yet

So only_if is pretty flexible and it will stay around. We'll need it.

However I've never really been comfortable with asking people to write
Python expressions in a language that intentionally seeks to AVOID
such expressions.
This small corner of ansible is asking folks to understand a
programming language we obviously want to hide.

As such, I think "easy conditionals" becomes an interesting mini-theme
for the 0.9 release.

Tentative syntax follows.

Imagine if you could do things without Python or quoting:

- action: ...
  when_defined: foo

when_defined: some.var.foo

when_equal: command.rc 1

when_successful: command_register_result

when_failed: command_register_result

when_greater_than: some.var.foo 12

when_all_of:
      - equal: result 0
      - find: some_command.stdout some_string

when_one_of:
      - equal: result.rc 0
      - equal: result2.rc 1

And, like before, you should be able to save 'when' tests in vars, like so:

when: $is_rhel6

Internally, these would compile down to only_if expressions, but would
not require writing Python, quoting, or remembering what data types to
cast things to. As a result, we keep conditional logic
simple, and make things easier to read.

--Michael

This is nice because it’s more readable. Code is powerful though and I think it’s always good to make sure you can drop down to code if you need to. One thing I was trying to do was do an if/else to decide which value to use for a var … e.g.

some_var: $is_rhe ? /etc/somepath : /etc/someotherpath

and wasn’t sure if there was a good way to do that or not.

This is nice because it’s more readable. Code is powerful though and I think it’s always good to make sure you can drop down to code if you need to. One thing I was trying to do was do an if/else to decide which value to use for a var … e.g.

some_var: $is_rhe ? /etc/somepath : /etc/someotherpath

and wasn’t sure if there was a good way to do that or not.

How about this: use a bash shell script to

  • Wrap around all “static” playbooks
  • Handle complex conditionals and use the here doc mechanism to create a playbook as needed
  • Run the all playbooks
    Regards,

Chin Fang

Generating playbooks via bash seems terrible to me.

Completely defeats the point of Ansible being readable and auditable
and easy to work on.

--Michael

Those examples looks totally awesome!

Many thanks to all python fluent people who makes ansible grow in
interesting directions.

/andreas