[openstack-dev] [openstack-ansible] Using jmespath more

jean-philippe@evrard.me jean-philippe at evrard.me
Mon Jul 30 08:04:47 UTC 2018


Hello,

According to the readability test here [1], contributors prefer reading a task like the following:

- name: Fail if service was deployed using a different installation method
  fail:
    msg: "Switching installation methods for OpenStack services is not supported"
  when:
    - ansible_local is defined
    - ansible_local.openstack_ansible is defined
    - ansible_local.openstack_ansible.aodh is defined
    - ansible_local.openstack_ansible.aodh.install_method is defined
    - ansible_local.openstack_ansible.aodh.install_method != aodh_install_method

as:

- name: Fail if service was deployed using a different installation method
  fail:
    msg: "Switching installation methods for OpenStack services is not supported"
  when:
    - (ansible_local | json_query("openstack_ansible.aodh.install_method")) is not ""
    - ansible_local.openstack_ansible.aodh.install_method != aodh_install_method

(Short explanation, json_query returns an empty string if path is not found, instead of having
an ansible failure, which is very welcomed. In the case above, if everything is defined, there will
be no empty string, and we can compare the string contents with the second when condition).

Another example avoiding the "is defined" dance:
Checking if install_method IS equal to "source" in the local facts could be simplified to:
when:
  - (ansible_local | json_query("openstack_ansible.aodh.install_method")) == 'source'

I hope this will inspire people to refactor some tedious to read tasks into more readable ones.
Thanks for your contributions!

Best regards,
Jean-Philippe Evrard (evrardjp)

[1]: https://etherpad.openstack.org/p/osa-readability-test




More information about the OpenStack-dev mailing list