diff --git a/solar/solar/extensions/ansible.py b/solar/solar/extensions/ansible.py index b36f6671..7ae66237 100644 --- a/solar/solar/extensions/ansible.py +++ b/solar/solar/extensions/ansible.py @@ -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):