Try to fix jinja cycle

Change-Id: Ib0a83f2763996d1764a570b97e14a4202f265f4e
diff --git a/collectd/client.sls b/collectd/client.sls
index e0fbf5d..4e07754 100644
--- a/collectd/client.sls
+++ b/collectd/client.sls
@@ -1,4 +1,4 @@
-{%- from "collectd/map.jinja" import client, service_grains with context %}
+{%- from "collectd/map.jinja" import client with context %}
 {%- if client.enabled %}
 
 include:
@@ -12,7 +12,22 @@
   - require:
     - pkg: collectd_client_packages
 
-{%- set plugins = service_grains.collectd.local_plugin %}
+{%- set service_grains = {'collectd': {'remote_plugin': {}, 'local_plugin': {}}} %}
+
+{%- for service_name in salt['pillar.ls']()|sort %}
+  {%- set service = salt['pillar.items'](service_name)[service_name] %}
+  {%- if service.get('_support', {}).get('collectd', {}).get('enabled', False) %}
+    {%- set grains_fragment_file = service_name+'/meta/collectd.yml' %}
+    {%- macro load_grains_file() %}{% include grains_fragment_file ignore missing %}{% endmacro %}
+    {%- set grains_yaml = load_grains_file()|load_yaml %}
+
+    {%- if grains_yaml is mapping %}
+      {%- set service_grains = salt['grains.filter_by']({'default': service_grains}, merge={'collectd': grains_yaml}) %}
+    {%- endif %}
+  {%- endif %}
+{%- endfor %}
+
+{%- set plugins = service_grains.collectd.get('local_plugin', {}) %}
 {%- include "collectd/_service.sls" %}
 
 {%- endif %}
diff --git a/collectd/map.jinja b/collectd/map.jinja
index 650c640..1077513 100644
--- a/collectd/map.jinja
+++ b/collectd/map.jinja
@@ -51,15 +51,3 @@
         'automatic_starting': True,
     }
 }, merge=salt['pillar.get']('collectd:remote_client')) %}
-
-{%- set service_grains = {'collectd': {'remote_plugin': {}, 'local_plugin': {}}} %}
-{%- for service_name, service in pillar.items() %}
-  {%- if service.get('_support', {}).get('collectd', {}).get('enabled', False) %}
-    {%- set grains_fragment_file = service_name+'/meta/collectd.yml' %}
-    {%- macro load_grains_file() %}{% include grains_fragment_file ignore missing %}{% endmacro %}
-    {%- set grains_yaml = load_grains_file()|load_yaml %}
-    {%- if grains_yaml is mapping %}
-      {%- set service_grains = salt['grains.filter_by']({'default': service_grains}, merge={'collectd': grains_yaml}) %}
-    {%- endif %}
-  {%- endif %}
-{%- endfor %}
diff --git a/collectd/meta/salt.yml b/collectd/meta/salt.yml
index 30f0f1f..c85972e 100644
--- a/collectd/meta/salt.yml
+++ b/collectd/meta/salt.yml
@@ -1,6 +1,19 @@
 grain:
   {%- if pillar.collectd.client is defined %}
-  {%- from "collectd/map.jinja" import service_grains with context -%}
+    {%- set service_grains = {'collectd': {'remote_plugin': {}, 'local_plugin': {}}} %}
+
+    {%- for service_name in salt['pillar.ls']()|sort %}
+      {%- set service = salt['pillar.items'](service_name)[service_name] %}
+      {%- if service.get('_support', {}).get('collectd', {}).get('enabled', False) %}
+        {%- set grains_fragment_file = service_name+'/meta/collectd.yml' %}
+        {%- macro load_grains_file() %}{% include grains_fragment_file ignore missing %}{% endmacro %}
+        {%- set grains_yaml = load_grains_file()|load_yaml %}
+
+        {%- if grains_yaml is mapping %}
+          {%- set service_grains = salt['grains.filter_by']({'default': service_grains}, merge={'collectd': grains_yaml}) %}
+        {%- endif %}
+      {%- endif %}
+    {%- endfor %}
   collectd:
     {{ service_grains|yaml(False)|indent(4) }}
   {%- endif %}