in chef_inventory.py [0:0]
def list_nodes(self):
groups = {}
hostvars = {}
data = self.read_cache()
for name, node in data.iteritems():
# make sure node is configured/working
if ( "ipaddress" in node["automatic"].keys() ):
if name not in hostvars:
hostvars[name] = {}
hostvars[name]['ansible_ssh_host'] = self.ip_for_node(node)
if name.startswith('fe-'):
hostvars[name]['ansible_ssh_port'] = '2222'
hostvars[name]['ansible_python_interpreter'] = '/usr/bin/python3'
else:
continue
# create a list of environments
environment = "chef_environment_%s" % self.to_safe(node["chef_environment"])
if environment not in groups:
groups[environment] = []
groups[environment].append(name)
if ('roles' in node["automatic"]) and ( node["automatic"]["roles"] is not None):
for r in self.check_key(node["automatic"], "roles"):
role = "role_%s" % self.to_safe(r)
if role not in groups:
groups[role] = []
groups[role].append(node["automatic"]["fqdn"])
if 'expanded_run_list' in node["automatic"]:
for r in self.check_key(node['automatic'], 'expanded_run_list'):
recipe = "recipe_%s" % self.to_safe(r)
if recipe not in groups: groups[recipe] = []
groups[recipe].append(self.ip_for_node(node))
if node["normal"]["tags"] is not None:
for tag in self.check_key(node['normal'], 'tags'):
tag = "tag_%s" % self.to_safe(tag)
if tag not in groups: groups[tag] = []
groups[tag].append(self.ip_for_node(node))
# remove any duplicates
groups = {key : list(set(items)) for (key, items) in groups.iteritems() }
meta = { "_meta" : { "hostvars" : hostvars } }
groups.update(meta)
print(self.json_format_dict(groups, pretty=True))