I'm trying to clean up some of our expected errors and make them more understandable to the operators running the playbooks. The code below works just fine, but I would like to assign a custom message to each of the failure conditions. For example, if var1 does not equal 4 then I'd like to present a message that says "There were not 4 entries in the lldp neighbor for neighbor x"
- name: Verify Number of bundles for output of show lldp neigh
set_fact:
var1: "{{lldp_spine3.data | regex_findall('ae1')}}"
var2: "{{lldp_spine3.data | regex_findall('ae2')}}"
failed_when: (var1 != 4) or (var2 | length != 4)
I know I can split those up and follow the logic below but I'd like to know if I can accomplish this in a single play.
- fail:
msg: "There were not 4 entries in the lldp neighbor for neighbor x"
when: var1|length != 4
- fail:
msg: "There were not 4 entries in the lldp neighbor for neighbor y"
when: var2|length != 4
There is assert module for that:
- assert:
that: var2|length == 4
msg: 'lldp should have four neighbors'
Few more tricks:
Some assertion can be run against localhost and only once (f.e. ansible version), there is run_once: true for that.
Add tags: [always] to guarantee that assertion task is executed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With