Salt: in pillar, check for file existence on minion

Created on 30 Aug 2018  路  5Comments  路  Source: saltstack/salt

Description of Issue/Question

Is it possible to check in a pillar for a directory / file on the minion? It seems that both salt['cmd.retcode']('test -d /path/to/dir') and salt['file.directory_exists']('/path/to/dir') checks the master for existence of the directory / file.

Setup

I'm using the apache-formula with apache.vhosts.standard.

apache:
  sites:
{%- for domain in pillar['domains'] %}
    {{ domain }}-80:
      enabled: True
      ServerName: {{ domain }}
      ServerAlias: www.{{ domain }}
      ServerAdmin: admin@{{ domain }}
      LogLevel: warn
      ErrorLog: ${APACHE_LOG_DIR}/{{ domain }}-error.log
      CustomLog: ${APACHE_LOG_DIR}/{{ domain }}-full.log
  {%- if salt['file.directory_exists']('/etc/letsencrypt/live/' ~ domain) %}
      DocumentRoot: False
    {{ domain }}-443:
      enabled: True
      port: '443'
      ServerName: {{ domain }}
      ServerAlias: www.{{ domain }}
      ServerAdmin: admin@{{ domain }}
      SSLCertificateFile: /etc/letsencrypt/live/{{ domain }}/fullchain.pem
      SSLCertificateKeyFile: /etc/letsencrypt/live/{{ domain }}/privkey.pem
      LogLevel: warn
      ErrorLog: ${APACHE_LOG_DIR}/{{ domain }}-error.log
      CustomLog: ${APACHE_LOG_DIR}/{{ domain }}-full.log
  {%- endif %}
      DocumentRoot: /var/www/{{ domain }}
      Directory:
        '/var/www/{{ domain }}':
          Options: -Indexes +FollowSymLinks +MultiViews
          Order: allow,deny
          AllowOverride: None
{%- endfor %}

I want to deploy the vhost for https if the letsencrypt cert is already deployed.

Versions Report

# salt --versions-report
Salt Version:
           Salt: 2017.7.5

Dependency Versions:
           cffi: 0.8.6
       cherrypy: 3.5.0
       dateutil: 2.2
      docker-py: Not Installed
          gitdb: 0.5.4
      gitpython: 0.3.2 RC1
          ioflo: Not Installed
         Jinja2: 2.9.4
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: 0.21.1
           Mako: 1.0.0
   msgpack-pure: Not Installed
 msgpack-python: 0.4.2
   mysql-python: 1.2.3
      pycparser: 2.10
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.9 (default, Jun 29 2016, 13:08:31)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 14.4.0
           RAET: Not Installed
          smmap: 0.8.2
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.0.5

System Versions:
           dist: debian 8.7 
         locale: UTF-8
        machine: x86_64
        release: 3.16.0-4-amd64
         system: Linux
        version: debian 8.7
Question info-needed

Most helpful comment

@Ch3LL
Thank you very much. This works like a charm and is way faster than what I did before.

The code looks like this now.

{%- set id = grains.get('id') %}
{%- set domain = 'example.com' %}
{%- set data = salt.saltutil.cmd(id, fun='file.directory_exists', kwarg={'path': '/etc/letsencrypt/live/' ~ domain}) %}
{%- for junk,values in data.iteritems() %}
  {%- if values.ret %}
{{ domain }}: {{ values.ret }}
  {%- endif %}
{%- endfor %}

All 5 comments

how about this: https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.saltutil.html#salt.modules.saltutil.cmd

you can target the minion you want and run a command

@Ch3LL
What would be the syntax inside a jinja if-clause?

What I did now is this:

{%- set id = grains.get('id') %}
{%- if salt['cmd.retcode']('salt ' ~ id ~ ' cmd.run "test -d /etc/letsencrypt/live/' ~ domain ~ '"') == 0 %}

If you have a list of a domains it takes forever. I dont' thing this is a good solution.

heres an example:

[root@1c41d29c2834 /]# cat /srv/pillar/test.sls
{%- set file = salt.saltutil.cmd('*', fun='file.file_exists', kwarg={'path': '/etc/passwd'}) %}
file: {{ file }}

obviously you will want to change the target '*' to whichever target you need.

oh to note you will still need to filter out the return, so you can get the return True or False

@Ch3LL
Thank you very much. This works like a charm and is way faster than what I did before.

The code looks like this now.

{%- set id = grains.get('id') %}
{%- set domain = 'example.com' %}
{%- set data = salt.saltutil.cmd(id, fun='file.directory_exists', kwarg={'path': '/etc/letsencrypt/live/' ~ domain}) %}
{%- for junk,values in data.iteritems() %}
  {%- if values.ret %}
{{ domain }}: {{ values.ret }}
  {%- endif %}
{%- endfor %}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

golmaal picture golmaal  路  3Comments

qiushics picture qiushics  路  3Comments

mooperd picture mooperd  路  3Comments

erwindon picture erwindon  路  3Comments

icycle77 picture icycle77  路  3Comments