0

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

guettli
  • 3,113
  • 14
  • 59
  • 110

2 Answers2

1

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.

jayooin
  • 294
  • 1
  • 8
  • Looks good, but a working solution would be great. – guettli Jan 17 '19 at 14:55
  • Did you manage to do what you wanted ? – jayooin Jan 21 '19 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 '19 at 08: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 '19 at 10:00
0

This would work:

/etc/foo.test:
  file.managed:
    - source: salt://tmp/dummy

{% for file in salt['cmd.run']('ls -1 /etc/foo*.test').splitlines() %}
{{ file }}_dummy_to_real:
  file.replace:
    - 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.

guettli
  • 3,113
  • 14
  • 59
  • 110