3

I have a formula that reads in the pillar a list of items to create some config files, like this:

fileA
  config:
    - some other config
    - ...
fileB
  config:
    - other configs

the problem is, in the parent folder there is a lot of temporary files and other created by the system.

How can I remove all the files not managed by my script? Fot the time being I am doing like this

directory_clean:
  file.directory:
    - name: {{ directory }}
    - clean: True

But this way all my files are being removed and added again. Is there a better solution?

Nicos Karalis
  • 3,724
  • 4
  • 33
  • 62
  • you could do a `{% for item not in directory%}..... - name: {{ file }}`.I hope the directory represents all the keys from the pillar file – tudoricc Jul 29 '15 at 07:01
  • `directory` represents a path to a directory, kinda like `/var/logs`, would that solution work for that? – Nicos Karalis Jul 29 '15 at 14:17

1 Answers1

4

Depending on how your salt tree is set up, you should be able to do this with file.recurse:

manage_directory:
  file.recurse:
    - name: /etc/something
    - source: salt://something/files
    - clean: True
    - template: jinja  # if needed

This assumes there is a directory in your salt tree containing all and only the files you want.

Andrew
  • 4,058
  • 4
  • 25
  • 37
  • 1
    but the files are generated from a template, how can I do this? – Nicos Karalis Aug 01 '15 at 02:15
  • If you mean multiple files each from their own template, the template option works with file.recurse too (edited example). If you mean multiple files generated from the *same* template, then it might be more complicated....if you can give an example of the file paths and template paths, that may help. – Andrew Aug 01 '15 at 02:51
  • 1
    Currently we are using like so: pillar has an array of filenames, organizer.sls manage each file from pillar using the same template – Nicos Karalis Aug 01 '15 at 02:53
  • Okay. Yeah, recurse won't work for that. If it's just a matter of not having miscellaneous system files mixed in with the ones you're managing, maybe you could put them in their own folder somewhere else? – Andrew Aug 04 '15 at 20:48