Skip to content

Instantly share code, notes, and snippets.

@mrbanzai
Created January 30, 2014 21:31
Show Gist options
  • Save mrbanzai/8720298 to your computer and use it in GitHub Desktop.
Save mrbanzai/8720298 to your computer and use it in GitHub Desktop.
Hiera lookup module for Ansible
---
- 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"
#!/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