ansibleansible-lint

How to nicely split on multiple lines long conditionals with OR on ansible?


I already know that if you have long conditionals with and between them you can use lists to split them on multiple lines.

Still, I am not aware of any solution for the case where you have OR between them.

Practical example from real life:

when: ansible_user_dir is not defined or ansible_python is not defined or ansible_processor_vcpus is not defined

This line is ugly and hard to read, and clearly would not fit a 79 column.

How can we rewrite it to make it easier to read?


Solution

  • Use the YAML folding operator >

    when: >
      ansible_user_dir is not defined or
      ansible_python is not defined or
      ansible_processor_vcpus is not defined
    

    As the ansible documentation states:

    Values can span multiple lines using | or >. Spanning multiple lines using a Literal Block Scalar | will include the newlines and any trailing spaces. Using a Folded Block Scalar > will fold newlines to spaces; it’s used to make what would otherwise be a very long line easier to read and edit. In either case the indentation will be ignored.

    Additional info can be found here: