Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saltstack: creating directory only if does not exists

Currently I have the following rule for creating a directory

/init/dir:
  file.recurse:
    - source:  salt://init_dir/init
    - user:  name
    - group:  group
    - name:  /path/init
    - dir_mode: 2775
    - file_mode: 777

Now I want to create a directory on new minions only if the directory does not exists already.

like image 273
cmidi Avatar asked Jun 25 '15 20:06

cmidi


2 Answers

While your example does work, it's not necessary. file.directory will only attempt to create the directory if it doesn't exist.

like image 128
Utah_Dave Avatar answered Nov 19 '22 04:11

Utah_Dave


Turned out to be pretty easy and well documented in the salt-stack documentation Below is what I came up with.

{% if not salt['file.directory_exists' ]('/home/init_dir') %}
/home/init_dir:
  file.directory:
    - user:  user
    - name:  /home/init_dir
    - group:  group
    - mode:  755
{% else %}
  cmd.run:
    - name: echo "Directory exists"
{% endif %}
like image 8
cmidi Avatar answered Nov 19 '22 03:11

cmidi