From 37684d85f0619058efb93f900d3ccdc303e1244f Mon Sep 17 00:00:00 2001 From: Theo Chatzimichos Date: Nov 10 2017 12:07:46 +0000 Subject: Add support to ignore non-existing states in {salt,pillar}/top.sls In the past we needed to create a state that it was included in top.sls, even if it was empty. By adding the include_optional macro, we can now check if the file exists on the disk and then include it. This way we can get rid of all the empty files. --- diff --git a/pillar/macros.jinja b/pillar/macros.jinja new file mode 100644 index 0000000..3a7579c --- /dev/null +++ b/pillar/macros.jinja @@ -0,0 +1,7 @@ +{%- macro include_optional(sls_file) %} + {%- for root in opts['pillar_roots'][saltenv] -%} + {%- if salt['file.file_exists']('{0}/{1}/init.sls'.format(root, sls_file)) %} + - {{ sls_file }} + {% endif %} + {%- endfor %} +{%- endmacro %} diff --git a/pillar/top.sls b/pillar/top.sls index 3b5834a..39561e5 100644 --- a/pillar/top.sls +++ b/pillar/top.sls @@ -1,3 +1,4 @@ +{% from "macros.jinja" import include_optional with context %} {% set country = salt['grains.get']('country') %} {% set domain = salt['grains.get']('domain') %} {% set id = salt['grains.get']('id') %} diff --git a/salt/macros.jinja b/salt/macros.jinja new file mode 100644 index 0000000..64e566b --- /dev/null +++ b/salt/macros.jinja @@ -0,0 +1,7 @@ +{%- macro include_optional(sls_file) %} + {%- for root in opts['file_roots'][saltenv] -%} + {%- if salt['file.file_exists']('{0}/{1}/init.sls'.format(root, sls_file)) %} + - {{ sls_file }} + {% endif %} + {%- endfor %} +{%- endmacro %} diff --git a/salt/top.sls b/salt/top.sls index d40f1ba..7452241 100644 --- a/salt/top.sls +++ b/salt/top.sls @@ -1,3 +1,4 @@ +{% from "macros.jinja" import include_optional with context %} {% set roles = salt['pillar.get']('grains:roles', []) %} production: