Skip to content

Instantly share code, notes, and snippets.

@apenney
Created June 18, 2015 13:30
Show Gist options
  • Save apenney/4b62ab242a6464178324 to your computer and use it in GitHub Desktop.
Save apenney/4b62ab242a6464178324 to your computer and use it in GitHub Desktop.
Hi,
We've run into an issue (and a pattern really) using ansible that we've been unable to solve and I'm hoping the list can teach us the "ansible way".
The background is that we want to create a number of monitors automatically, based on certain information. The solution we have today is a variable:
monitor_exchanges:
- name: exchange1
demand_partners:
- 'dp1'
- 'dp2'
And the consumer:
- name: Create datadog monitors
datadog_monitor:
state: "present"
type: "query alert"
name: '{{item.0.name}}-{{item.1}} over delivery'
query: "avg({{monitor_interval}}):100*(avg:requests.outbound.transmitted.count{sp_name:{{item.0.name}}, dp_name:{{item.1}}} - avg:demand_partner.rps{sp_name:{{item.0.name}}, dp_name:{{item.1}}})/avg:demand_partner.rps{sp_name:{{item.0.name}}, dp_name:{{item.1}}} > {{monitor_threshold}}"
message: "test"
api_key: "{{datadog_api_key}}"
app_key: "{{datadog_app_key}}"
with_subelements:
- monitor_exchanges
- demand_partners
when: monitor_exchanges is defined
This then creates, for each exchange a monitor per demand partner. That part is fairly easy and works fine but we wanted to externalize the 'query'/'name'/'message' part of the above thing, so that we'd have something more like:
- name: Create monitors
datadog_monitor:
state: "present"
type: "query alert"
query: {{item.something.query}}
name: {{item.something.query}}
message: {{item.something.query}}
We kept trying to do this with two datastructures, the one from above and then
something like "monitors" that would be a list of hashes containing each monitor to create. I couldn't come up with the right combination of iteration to make it work however, because I really needed to iterate over two separate things in
different ways, I couldn't just list the two structures, I needed to do something more like:
with_nested:
- with_subelements:
- monitor_exchanges
- demand_partners
- monitors
Is there another way I can model this that makes sense? I really just want to iterate over a list of monitors and then for each monitor iterate over the `monitor_exchanges` list to pull all the appropriate data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment