-
-
Save 3rdman/394e779310b6504773d67e0fa8e8db4b to your computer and use it in GitHub Desktop.
Hiera lookup module for Ansible
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
--- | |
- name: Load NRPE variables from Hiera | |
connection: local | |
hiera: path=lib/hiera/bin/hiera key="{{ item.value }}" fact="{{ item.key }}" source=hiera.yaml | |
args: | |
context: | |
environment: "{{ environment }}" | |
sitecode_lc: "{{ sitecode_lc }}" | |
with_dictionary: | |
nrpe_server_port: "nrpe_server_port" | |
nrpe_user: "nrpe_user" | |
nrpe_group: "nrpe_group" | |
nrpe_command_timeout: "nrpe_command_timeout" | |
nrpe_connection_timeout: "nrpe_connection_timeout" | |
nrpe_dont_blame: "nrpe_dont_blame" | |
nrpe_allowed_hosts: "nrpe_allowed_hosts" | |
nrpe_commands: "icinga::nrpe::commands" |
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
#!/bin/env python | |
import os | |
import subprocess | |
import json | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
name=dict(aliases=['key']), | |
fact=dict(required=False), | |
path=dict(required=False, default="hiera"), | |
context=dict(required=False, default={}, type='dict'), | |
source=dict(required=False, default=None) | |
) | |
) | |
params = module.params | |
out = {} | |
if not params['fact']: | |
params['fact'] = params['name'] | |
try: | |
pargs = [ | |
params['path'], | |
'-f', 'json' | |
] | |
if params['source']: | |
pargs.extend(['-c', params['source']]) | |
pargs.append(params['name']) | |
pargs.extend([r'%s=%s' % (k, v) for k, v in params['context'].iteritems()]) | |
p = subprocess.Popen(pargs, stdout=subprocess.PIPE) | |
res, err = p.communicate() | |
if res is None: | |
res = "" | |
else: | |
res = json.loads(res) | |
out['ansible_facts'] = {} | |
out['ansible_facts'][params['fact']] = res | |
module.exit_json(**out) | |
except Exception, e: | |
module.fail_json(msg=str(e)) | |
from ansible.module_utils.basic import * | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment