salt icon indicating copy to clipboard operation
salt copied to clipboard

Add module/function and top.sls syntax to check for sls existence

Open ryan-lane opened this issue 12 years ago • 30 comments

Currently, if I want to conditionally include an sls file, if the file exists, I need to do some ugly jinja magic:

base:
  # For each directory that can have pillar files
  {% for root in opts['pillar_roots']['base'] -%}
  # Set a variable to the path to check
  {% set service_group_sls = '{0}/{1}.sls'.format(root, grains['service_group']) -%}
  # Check to see if the file exists, if so, include that pillar file.
  {% if salt['file.file_exists'](service_group_sls) %}
  'service_group:{{ grains["service_group"] }}':
    - match: grain
    - {{ grains['service_group'] }}
  {% endif %}
  {% endfor -%}

What I really want to do is:

base:
  'service_group:{{ grains["service_group"] }}':
    - match: grain
    - ignore_missing: True
    - {{ grains['service_group'] }}

I also want to see if a pillar or state sls file exists from the cli, or from inside of a state, for conditional include:

salt-call state.sls_exists mystatssls
salt-call pillar.sls_exists mypillarsls

This way I can call individual states, but only if they actually exist.

ryan-lane avatar Oct 16 '14 18:10 ryan-lane

Hi @ryan-lane. I think this is a tremendous idea. I really like it. If possible, I'd really like to see this feature included in Lithium. I plan to push for it. :]

cachedout avatar Oct 17 '14 17:10 cachedout

Fixed for pillars in #19429

ryan-lane avatar Jan 07 '15 04:01 ryan-lane

I'm working on something that requires a similar approach and running into an issue that Salt doesn't ignore it if I try to include a state that does not exist.

It looks like for states the cp.show_states could come in handy. The proposed sls_exists can most likely be a wrapper around that probably?

syphernl avatar May 08 '15 12:05 syphernl

Too bad this was only implemented for Pillar! Generic support for SLS inclusion in top.sls (as the title states!) or SLS inclusion from within another SLS would have been epic. But that is no small patch, I'm sure. Thanks @ryan-lane for getting the Pillar part done! :+1:

brc avatar Jan 14 '16 18:01 brc

Yeah... The code for pillar and state top files are separate for some reason, even though they do the exact same thing. I was hoping to do both at the same time, but I only needed pillar and the state top file code looked more difficult to modify. I'm still hoping someone from SaltStackInc will finish this work up :)

ryan-lane avatar Jan 14 '16 19:01 ryan-lane

Bumping this, as it would really clean up a tedious top.sls in our code.

hartloosli avatar Jul 11 '16 01:07 hartloosli

Linking this to duplicate #20010

dgorissen avatar Sep 14 '16 17:09 dgorissen

#39186 is a duplicate as well.

ksoviero avatar Feb 06 '17 18:02 ksoviero

+1 I really need this functionality:

include:
  - ignore_missing: True
  - {{ "servers/%s" % grains.id.replace('.', '_') }}

zerthimon avatar Aug 31 '17 14:08 zerthimon

+1

fredericlebel avatar Jan 05 '18 18:01 fredericlebel

+1

konstest avatar Mar 12 '18 08:03 konstest

I tried adding support for it myself a while back, but SaltStack's code is... We'll, let's just say I still have nightmares about it.

ksoviero avatar Mar 12 '18 14:03 ksoviero

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

stale[bot] avatar Jun 25 '19 14:06 stale[bot]

Oh no you don't mr.stale-bot. This is still very much needed.

ksoviero avatar Jun 25 '19 14:06 ksoviero

Thank you for updating this issue. It is no longer marked as stale.

stale[bot] avatar Jun 25 '19 14:06 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

stale[bot] avatar Jan 08 '20 10:01 stale[bot]

This feature is still wanted.

OrangeDog avatar Jan 08 '20 10:01 OrangeDog

Thank you for updating this issue. It is no longer marked as stale.

stale[bot] avatar Jan 08 '20 10:01 stale[bot]

I could really make use of this feature as well.

solomongumbie avatar Jan 27 '20 15:01 solomongumbie

I would also like this feature

dth202 avatar Feb 11 '20 17:02 dth202