best way to test for a condition and report it

14 views
Skip to first unread message

Ed Greenberg

unread,
Aug 13, 2019, 9:39:55 AM8/13/19
to Ansible Project
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







Dan Linder

unread,
Aug 14, 2019, 10:34:48 PM8/14/19
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages