Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active October 20, 2016 20:29
Show Gist options
  • Save halberom/8057404 to your computer and use it in GitHub Desktop.
Save halberom/8057404 to your computer and use it in GitHub Desktop.
custom facts - very hacky
# located in /etc/ansible/library
#!/usr/bin/env python
#
# Get cloudstack zone name/id and add to ansible facts
# requires cloudmonkey >= v5 with json output set
import json
import os
# ugly.. use subprocess
str_output = os.popen('cloudmonkey list zones').read()
# remove ansi escape char
str_output = str_output.replace('\x1b[?1034h', '')
# create the dict structure, must contain a key ansible_facts, within which we'll
# add our custom facts
facts = {
'ansible_facts': {
'cloudstack': {
'zone': {}
}
}
}
try:
decoded = json.loads(str_output)
except (ValueError, KeyError, TypeError):
print "JSON format error"
sys.exit(1)
# if there are zone's, loop through and add to facts dict
if decoded['count'] > 0:
for item in decoded['zone']:
facts['ansible_facts']['cloudstack']['zone'][item['name']] = item['id']
print json.dumps(facts)
- hosts: application-hosts
gather_facts: False
tasks:
- name: add cloudstack zone info to facts
action: get_cloudstack_zones
- debug: msg="{{ cloudstack.zone['test'] }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment