# best way to test for a condition and report it

**URL:** <https://forum.ansible.com/t/best-way-to-test-for-a-condition-and-report-it/31031>\
**Category:** Ansible Project\
**Created:** [August 13, 2019, 1:39pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-condition-and-report-it/31031 "2019-08-13T13:39:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ed\_Greenberg](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@Ed\_Greenberg](https://forum.ansible.com/u/Ed_Greenberg)\
**Post date:** [August 13, 2019, 1:39pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-condition-and-report-it/31031/1 "2019-08-13T13:39:55Z")

</div>

Hi, I have an ansible module that adds and removes servers from the load balancer. If I do:

ansible localhost -m modulename -a ‘servername=foo inbalancer=present’

it adds the module to the load balancer and reports changed if it wasn’t there before, and not changed if it was already there.

Same for ‘inbalancer=absent’ It takes the server out of the load balancer and reports changed or not changed.

So I know if the server is in the balancer when I start through this section of the code.

I’d like to have the module set a variable that can be (a) reported on the output and (b) tested for in a playbook. I’m more interested in reporting, so I can have a shell script that reports the state of my load balancer and all it’s servers.

After much reading, I think I need to ask for an approach.

Thanks,

Ed Greenbert

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [August 15, 2019, 2:34am UTC](https://forum.ansible.com/t/best-way-to-test-for-a-condition-and-report-it/31031/2 "2019-08-15T02:34:47Z")

</div>

Ansible is really designed to put a system configuration into a state, so checking for a possible state is a bit trickier.

But, you could run the playbook in check-only mode, then register the result of the change, then use the results “changed” flag in your script to get the report.

For instance, in check-only mode you can say “inbalancer=absent” then any that register “changed == true” you know are not absent, and any that are “changed == false”, you know are already absent.

This is harder if your state has multiple possibilities (e.g. network port speeds: 10Mbit, 100Mbit, 1Gbit, 10Gbit), but still do-able with the external script.

Dan
