HiveBrain v1.2.0
Get Started
← Back to all entries
patternyamlMinor

Is it possible to use a list with 'file.exists' in Salt?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
filewithsaltpossibleexistslistuse

Problem

I asked a question recently regarding the most efficient and scalable way to check which files/services exist in multiple environments. Thanks to an answer I have successfully set up roles for all of my salt minions.

I am now in the process of writing my first salt state which will check that all expected files/services are present for all minions with the apiserver role.

I am using the file.exists feature, however as you can expect there are 100's of files/services I wish to check, I'm sure there must be a way of listing these files/services in a list or string? I can't find anything in the documentation under file.exists but it is possible in other file.'s

Here is the current state of my init.sls file:

# This state will check all minions identified as 'apiserver' for files and/or services which should exist

# This section checks the 'checkservices.sh' is present in /opt (used for Jenkins check jobs)

checkservices.sh:
  file.exists:
    - name: /opt/checkservices.sh

# This section checks for expected files/scripts within /opt/apiv2/

apiv2.properties:
  file.exists:
    - name: /opt/apiv2/apiv2.properties

jvm.options:
  file.exists:
    - name: /opt/apiv2/jvm.options

restart.sh:
  file.exists:
    - name: /opt/apiv2/restart.sh

status.sh:
  file.exists:
    - name: /opt/apiv2/status.sh

shutdown.sh:
  file.exists:
    - name: /opt/apiv2/shutdown.sh

start.sh:
  file.exists:
    - name: /opt/apiv2/start.sh

# This section checks for 'bootstrap.jar' in /opt/apiv2/repo/

bootstrap.jar:
  file.exists:
    - name: /opt/apiv2/repo/bootstrap.jar


My second question is; is my current method (above) considered bad practice or is it just an ugly/inefficient way of doing it?

Solution

Yes - there are at least two options available for managing files in the manner you describe. The first such way is to manage the entire directory using file.directory:

/opt/apiv2:
  file.directory:
    - user: root
    - group: root
    - dir_mode: 755
    - file_mode: 644
    - recurse:
      - user
      - group
      - mode


The second way can manage a manifest of several files at many paths is to use the source_hash feature of file.managed:

This allows you to provide a source tar file and a file containing md5sum hashes:

apiv2-0.7.3.tar.gz:
  file.managed:
    - name: /tmp/apiv2-0.7.3.tar.gz
    - source: salt:///apiv2/distrib/apiv2-0.7.3.tar.gz
    - source_hash: salt:///apiv2/distrib/manifest-0.7.3.hash


You would then create a text file on your salt server /srv/salt/apiv2/distrib/manifest-0.7.3.hash with contents similar to:

37b51d194a7513e45b56f6524f2d51f2    /opt/apiv2/apiv2.properties
acbd18db4cc2f85cedef654fccc4a4d8    /opt/apiv2/repo/bootstrap.jar
73feffa4b7f6bb68e44cf984c85f6e88    /opt/apiv1/apiv1.properties


Your might even be able to use templating,

apiv2-0.7.3.tar.gz:
  file.managed:
    - name: /tmp/apiv2-0.7.3.tar.gz
    - source: salt:///apiv2/distrib/apiv2-0.7.3.tar.gz
    - source_hash: salt:///apiv2/distrib/manifest-0.7.3.hash
    - template: jinja


however Salt Stack might want to push down the file each time because after the templates are rendered, the hash will have changed, so the creators might just have elected to throw an error if you try these two features together. In short, Your Mileage May VaryTM

Code Snippets

/opt/apiv2:
  file.directory:
    - user: root
    - group: root
    - dir_mode: 755
    - file_mode: 644
    - recurse:
      - user
      - group
      - mode
apiv2-0.7.3.tar.gz:
  file.managed:
    - name: /tmp/apiv2-0.7.3.tar.gz
    - source: salt:///apiv2/distrib/apiv2-0.7.3.tar.gz
    - source_hash: salt:///apiv2/distrib/manifest-0.7.3.hash
37b51d194a7513e45b56f6524f2d51f2    /opt/apiv2/apiv2.properties
acbd18db4cc2f85cedef654fccc4a4d8    /opt/apiv2/repo/bootstrap.jar
73feffa4b7f6bb68e44cf984c85f6e88    /opt/apiv1/apiv1.properties
apiv2-0.7.3.tar.gz:
  file.managed:
    - name: /tmp/apiv2-0.7.3.tar.gz
    - source: salt:///apiv2/distrib/apiv2-0.7.3.tar.gz
    - source_hash: salt:///apiv2/distrib/manifest-0.7.3.hash
    - template: jinja

Context

StackExchange DevOps Q#1762, answer score: 1

Revisions (0)

No revisions yet.