def gen_host_definitions(environment, outputdir=None): """ Generates host file suitable for icinga monitoring Example: python generate_monitoring_host_file.py --environment prod --outputdir= """ ansible_inventory_file = os.path.join(project_dir, 'environs', environment, 'ansible', 'inventory.cfg') loader = DataLoader() group_dict = { 'all': Group(name='all'), 'ungrouped': Group(name='ungrouped') } parser = InventoryParser(loader=loader, groups=group_dict, filename=ansible_inventory_file) hostname_dict = parser.hosts hostname_list = hostname_dict.values() hostnames = {} for hostname in hostname_list: x = get_ipaddress(str(hostname)) if x: hostnames[str(hostname)] = x host_definition = Environment().from_string(host_template).render( hostnames=hostnames) project_host_cfg = Path(outputdir, 'project_hosts.cfg') project_host_cfg = project_host_cfg.expand() with open(str(project_host_cfg), 'w') as f: f.write(host_definition) f.close()
def normalize_element(self, path_val): path = Path(path_val) # /foo if path.isabsolute(): return path.expand() # foo else: return Path(self.root, path_val)