Last active
October 20, 2016 20:29
-
-
Save halberom/8057404 to your computer and use it in GitHub Desktop.
custom facts - very hacky
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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