Merge pull request #23 from xarses/tagging
Fix nodes generation for inventory
This commit is contained in:
commit
cea745189d
@ -13,8 +13,11 @@ ANSIBLE_INVENTORY = """
|
||||
{% endfor %}
|
||||
|
||||
{% for res in resources %}
|
||||
[{{ res.id }}]
|
||||
{% for node in nodes %} {{node['name']}} {% endfor %} {% endfor %}
|
||||
[{{ res.id }}]
|
||||
{% for node in nodes_mapping[res.id] %}
|
||||
{{node['name']}}
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
"""
|
||||
|
||||
|
||||
@ -47,7 +50,7 @@ class AnsibleOrchestration(base.BaseExtension):
|
||||
result_resources = self._get_resources_with_tags(node_tags)
|
||||
resources.extend(result_resources)
|
||||
|
||||
return resources
|
||||
return dict((r['id'], r) for r in resources).values()
|
||||
|
||||
def _get_resources_with_tags(self, tags):
|
||||
resources = []
|
||||
@ -65,7 +68,26 @@ class AnsibleOrchestration(base.BaseExtension):
|
||||
@property
|
||||
def inventory(self):
|
||||
temp = Template(ANSIBLE_INVENTORY)
|
||||
return temp.render(nodes=self.nodes, resources=self.resources)
|
||||
return temp.render(
|
||||
nodes_mapping=self._make_nodes_services_mapping(),
|
||||
resources=self.resources,
|
||||
nodes=self.nodes)
|
||||
|
||||
def _make_nodes_services_mapping(self):
|
||||
mapping = {}
|
||||
for resource in self.resources:
|
||||
mapping[resource['id']] = self._get_nodes_for_resource(resource)
|
||||
|
||||
return mapping
|
||||
|
||||
def _get_nodes_for_resource(self, resource):
|
||||
resource_tags = set(resource['tags'])
|
||||
nodes = []
|
||||
for node in self.nodes:
|
||||
if resource_tags <= set(node['tags']):
|
||||
nodes.append(node)
|
||||
|
||||
return nodes
|
||||
|
||||
@property
|
||||
def vars(self):
|
||||
|
Loading…
Reference in New Issue
Block a user