def apply_to_targets(runbooks, config, dbc):
''' Match hosts with runbooks '''
targets = dbc.get_target()
logger.debug("Found targets: {0}".format(json.dumps(targets)))
for target in targets.keys():
# Create runbook dictionary if it doesn't exist
if "runbooks" not in targets[target].keys():
logger.debug("Creating runbook dictionary in target config")
targets[target]['runbooks'] = {}
logger.debug("Identifying runbooks for target {0}".format(target))
for matcher in runbooks.keys():
if fnmatch.fnmatch(targets[target]['hostname'], matcher):
for runbook in runbooks[matcher].keys():
logger.debug("Checking if {0} is already applied".format(runbook))
if runbook not in targets[target]['runbooks'].keys():
try:
targets[target]['runbooks'][runbook] = render_runbooks(
runbooks[matcher][runbook],
targets[target]['facts'])
except Exception as e:
logger.warn("Could not apply runbook {0} to target {1}: {2}".format(
runbook,
targets[target]['hostname'],
e.message
))
dbc.save_target(target=targets[target])
msg = {
'msg_type' : 'runbook_add',
'runbook' : runbook,
'target' : target}
logger.debug("Adding runbook policy {0} to target {1}".format(
runbook, target))
count = dbc.notify("monitors", msg)
logger.info("Notified {0} of runbook changes to target {1}".format(
count, target))
else:
logger.debug("{0} is already applied to target {1}".format(runbook, target))
return True
评论列表
文章目录