Answers: <p>You should use <a href="https://docs.saltstack.com/en/latest/topics/jinja/index.html" rel="nofollow noreferrer">Jinja</a> to <a href="http://jinja.pocoo.org/docs/2.10/templates/#for" rel="nofollow noreferrer">loop</a> over a list of files and pattern/replacement pairs.</p>
  
  <pre><code>{% set files = ['/a/foo.sh', '/b/bar.sh', '/b/baz.sh'] %}
  
  {% for file in files %}
  {% for p, r in [('/aaa/bbb', '/aaa/ccc'), ('/xxx/yyy', '/xxx/zzz')] %}
  replace {{ p }} in {{ file }}:
     file.replace:
        - name: {{ file }}
        - pattern: {{ p }}
        - repl: {{ r }}
  {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>Keep in mind the identifiers need to be unique, that's why variables in <code>replace {{ p }} in {{ file }}:</code> are used.</p>
  
Answers: CC BY-SA 4.0
Answers: 2018-12-06T10:13:24.440Z
Answers: A. Z. Thank you, your answer works. However for a variable input under     `{% for p, r in [('/aaa/bbb', '/aaa/{{ var }}'), ('/xxx/yyy', '/xxx/{{ var }}')] %}`
  where {{ var }} is a variable defined in jinja, the replacement fails. Can I pass variable input instead of a hardcoded value?
Answers: Narasimha
Answers: CC BY-SA 4.0
Answers: 2018-12-05T00:37:28.503Z
Answers: 2018-12-05T00:37:28.503Z
Answers: A. Z.
Body: <p>I am using saltstack state pattern and replace to match a pattern and replace with a new pattern. My init.sls file executes below:</p>
  
  <pre><code>`modify_script.sh:
     file.replace:
        - name: /path/to/script.sh
        - pattern: /aaa/bbb
        - repl: /aaa/ccc
        - pattern: /xxx/yyy
        - repl: /xxx/zzz`
  </code></pre>
  
  <p>The above works good if I use a single pattern and replace. This fails for multiple patterns and replaces as shown above. How to achieve multiple pattern replace for two or more patterns in a single file?</p>
  
  <p>Secondly, I have multiple files in a directory which requires a pattern match and replace. </p>
  
  <p><code>modify_script.sh:
         file.replace:
            - name: /path/to/dir [having multiple files which require update]
            - pattern: /aaa/bbb
            - repl: /aaa/ccc</code></p>
  
  <p>Can I match the patterns for all the files under directory? I wouldn't want to match and replace a single file individually. 
  Please advise.</p>
  
Tags: <salt>
Title: SaltStack states pattern and replace for multiple files under directory and for multiple line matches
UserDisplayName: Narasimha
ThreadText: # SaltStack states pattern and replace for multiple files under directory and for multiple line matches
  
  (Asked by: username_0 on 2018-12-04)
  
  I am using saltstack state pattern and replace to match a pattern and replace with a new pattern. My init.sls file executes below:
  
  ```
  `modify_script.sh:
     file.replace:
        - name: /path/to/script.sh
        - pattern: /aaa/bbb
        - repl: /aaa/ccc
        - pattern: /xxx/yyy
        - repl: /xxx/zzz`
  
  ```
  
  The above works good if I use a single pattern and replace. This fails for multiple patterns and replaces as shown above. How to achieve multiple pattern replace for two or more patterns in a single file?
  
  Secondly, I have multiple files in a directory which requires a pattern match and replace.
  
  `modify_script.sh:
  file.replace:
  - name: /path/to/dir [having multiple files which require update]
  - pattern: /aaa/bbb
  - repl: /aaa/ccc`
  
  Can I match the patterns for all the files under directory? I wouldn't want to match and replace a single file individually.
  Please advise.
  
  **Tags:** salt
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-12-05. Score: 1
  
  You should use [Jinja](https://docs.saltstack.com/en/latest/topics/jinja/index.html) to [loop](http://jinja.pocoo.org/docs/2.10/templates/#for) over a list of files and pattern/replacement pairs.
  
  ```
  {% set files = ['/a/foo.sh', '/b/bar.sh', '/b/baz.sh'] %}
  
  {% for file in files %}
  {% for p, r in [('/aaa/bbb', '/aaa/ccc'), ('/xxx/yyy', '/xxx/zzz')] %}
  replace {{ p }} in {{ file }}:
     file.replace:
        - name: {{ file }}
        - pattern: {{ p }}
        - repl: {{ r }}
  {% endfor %}
  {% endfor %}
  
  ```
  
  Keep in mind the identifiers need to be unique, that's why variables in `replace {{ p }} in {{ file }}:` are used.
  
  > **Comments:**
  > **username_0**: A. Z. Thank you, your answer works. However for a variable input under     `{% for p, r in [('/aaa/bbb', '/aaa/{{ var }}'), ('/xxx/yyy', '/xxx/{{ var }}')] %}` where {{ var }} is a variable defined in jinja, the replacement fails. Can I pass variable input instead of a hardcoded value?  
  
  ---
  
  