I use file.replace and would like to use this not on a single file, but on a list of files. The list of files comes from globbing.

Example: I want to modify /etc/ImageMagick*/policy.xml.

The file can be /etc/ImageMagick-6/policy.xml or /etc/ImageMagick/policy.xml or ...

AFAIK "name" in "file.replace" can only be one single file name.

How to solve this with salt?

Related discussion at salt-users list: https://groups.google.com/forum/#!topic/salt-users/75OpiflpObA


You could do it in two steps:

  1. Get a list of files that match your search with file.find
  2. Make a loop to apply your function file.replace to each files in that list

Note: This is only the methodology to achieve your goal, I don't have the technical solution ready for you.

  • Looks good, but a working solution would be great. – guettli Jan 17 at 14:55
  • Did you manage to do what you wanted ? – jayooin Jan 21 at 11:30
  • no, I found something (and wrote it as answer). But this solution has a drawback: The file needs to already exist before the salt run starts, since jinia gets executed before the salt run starts. (see below) – guettli Jan 29 at 8:43
  • file.find creates a list of files. But AFAIK this list of files gets created before the first action/requisite gets executed. This means it does not work, since one salt run should do two things: Install RPM, then change a file which was created by installing the rpm. Please correct me if I am wrong. – guettli Feb 12 at 10:00

This would work:

    - source: salt://tmp/dummy

{% for file in salt['cmd.run']('ls -1 /etc/foo*.test').splitlines() %}
{{ file }}_dummy_to_real:
    - name: {{ file }}
    - pattern: dummy
    - repl: hitme
{% endfor %}

But it only works, if /etc/foo.test already exists before salt starts executing.

If /etc/foo.test gets created during the same salt run, then this will fail.

  • The solution has the fundamental issue, that jinja gets executed too early :-( – guettli Feb 15 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.