Answers: <p>A capture group is a <a href="https://www.regular-expressions.info/refcapture.html" rel="nofollow noreferrer">type of backreference</a>. Saltstack, being python based, <a href="https://docs.python.org/2/library/re.html" rel="nofollow noreferrer">uses the re.match object</a> to do regular expression matching, which <a href="https://stackoverflow.com/questions/15163092/python-regex-backreference-a-matching-regex-group">appears to support backreferences</a>, so these backreferences are allowed.</p>
  <p>However, you be unable to pass the value of the backreference <code>\1</code> in the <code>- include_pat</code> field back to <code>- name</code> because the <code>- name</code> field is never processed by the <code>re.match</code> object. Even if it were, variable scope would prevent you from sharing the backreference between <code>re.match</code> calls.</p>
  <p>Instead, you will need to do one of the following:</p>
  <ul>
  <li><p>Rename all the files in <code>- source</code> from <code>myfile.conf.template</code> to <code>myfile.conf</code></p>
  </li>
  <li><p>Use cmd.run to do a <a href="https://blogs.technet.microsoft.com/heyscriptingguy/2013/11/22/use-powershell-to-rename-files-in-bulk/" rel="nofollow noreferrer">batch</a> file <a href="https://stackoverflow.com/questions/17271586/rename-multiple-files-in-cmd">rename</a> after your <code>file.recurse</code> and add to your <code>file.recurse</code> an <a href="https://docs.saltstack.com/en/latest/ref/states/requisites.html#onchanges" rel="nofollow noreferrer">onchanges</a> pointed to a previous event in order to defeat the idempotency (that is, to prevent it from writing out the .template files every time salt runs). You will also be sure to use a <a href="https://docs.saltstack.com/en/latest/ref/states/requisites.html#require" rel="nofollow noreferrer">require</a> and <code>onchanges</code> in your cmd.run that references <code>copy_template</code></p>
  <p>This will have the unfortunate side-effect that you will never be able to update your jinja templates. You will only be able to deploy them the first time - at least not without first deleting what is there, and then re-writing and re-copying from the source.</p>
  </li>
  <li><p>Use <code>file.managed</code> for every template file.</p>
  </li>
  </ul>
  <p>For example:</p>
  <pre><code>copy_template1:
    file.managed:
      - name: '{{ service_path }}\template1.conf'
      - template: jinja
      - source: {{ pillar['locations']['systemtoolsfolder'] }}\template1.conf.template
  copy_template2:
    file.managed:
      - name: '{{ service_path }}\template2.conf'
      - template: jinja
      - source: {{ pillar['locations']['systemtoolsfolder'] }}\template2.conf.template
  ...
  and so forth
  </code></pre>
  
Answers: CC BY-SA 3.0
Answers: 2018-03-16T16:16:21.153Z
Answers: {{ service_path }} is just a path. In my case minion is on windows so for example c:\myservice\.
  
  I just wanted all .template files from source to be copy without the extension .template but so keep the base filename.
  
  for example : myfile.conf.template to become myfile.conf
Answers: Dragouf
Answers: CC BY-SA 3.0
Answers: 2018-03-22T14:56:18.580Z
Answers: You should add this to your original question to help clarify it, ping me or flag it for moderator to reopen once done.
Answers: Tensibai
Answers: CC BY-SA 3.0
Answers: 2018-03-15T16:15:12.620Z
Answers: 2018-03-16T20:54:42.830Z
Answers: 2020-06-18T08:28:10.803Z
Answers: James Shewey
Body: <p>is it possible to have a capture group with include_pat when copying files ?</p>
  
  <p>I would like to do something like that:</p>
  
  <pre><code>copy_template:
      file.recurse:
      - name: '{{ service_path }}\$1'
      - template: jinja
      - include_pat: E@^(.*)\.template$
      - source: {{ pillar['locations']['systemtoolsfolder'] }}\
  </code></pre>
  
Tags: <salt>
Title: salt-stack: capture group for regex include_pat
UserDisplayName: Dragouf
ThreadText: # salt-stack: capture group for regex include_pat
  
  (Asked by: username_0 on 2018-03-15)
  
  is it possible to have a capture group with include\_pat when copying files ?
  
  I would like to do something like that:
  
  ```
  copy_template:
      file.recurse:
      - name: '{{ service_path }}\$1'
      - template: jinja
      - include_pat: E@^(.*)\.template$
      - source: {{ pillar['locations']['systemtoolsfolder'] }}\
  
  ```
  
  **Tags:** salt
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-15. Score: 2
  
  A capture group is a [type of backreference](https://www.regular-expressions.info/refcapture.html). Saltstack, being python based, [uses the re.match object](https://docs.python.org/2/library/re.html) to do regular expression matching, which [appears to support backreferences](https://stackoverflow.com/questions/15163092/python-regex-backreference-a-matching-regex-group), so these backreferences are allowed.
  
  However, you be unable to pass the value of the backreference `\1` in the `- include_pat` field back to `- name` because the `- name` field is never processed by the `re.match` object. Even if it were, variable scope would prevent you from sharing the backreference between `re.match` calls.
  
  Instead, you will need to do one of the following:
  
  * Rename all the files in `- source` from `myfile.conf.template` to `myfile.conf`
  * Use cmd.run to do a [batch](https://blogs.technet.microsoft.com/heyscriptingguy/2013/11/22/use-powershell-to-rename-files-in-bulk/) file [rename](https://stackoverflow.com/questions/17271586/rename-multiple-files-in-cmd) after your `file.recurse` and add to your `file.recurse` an [onchanges](https://docs.saltstack.com/en/latest/ref/states/requisites.html#onchanges) pointed to a previous event in order to defeat the idempotency (that is, to prevent it from writing out the .template files every time salt runs). You will also be sure to use a [require](https://docs.saltstack.com/en/latest/ref/states/requisites.html#require) and `onchanges` in your cmd.run that references `copy_template`
  
    This will have the unfortunate side-effect that you will never be able to update your jinja templates. You will only be able to deploy them the first time - at least not without first deleting what is there, and then re-writing and re-copying from the source.
  * Use `file.managed` for every template file.
  
  For example:
  
  ```
  copy_template1:
    file.managed:
      - name: '{{ service_path }}\template1.conf'
      - template: jinja
      - source: {{ pillar['locations']['systemtoolsfolder'] }}\template1.conf.template
  copy_template2:
    file.managed:
      - name: '{{ service_path }}\template2.conf'
      - template: jinja
      - source: {{ pillar['locations']['systemtoolsfolder'] }}\template2.conf.template
  ...
  and so forth
  
  ```
  
  > **Comments:**
  > **username_0**: {{ service_path }} is just a path. In my case minion is on windows so for example c:\myservice\.  I just wanted all .template files from source to be copy without the extension .template but so keep the base filename.  for example : myfile.conf.template to become myfile.conf  
  > **username_2**: You should add this to your original question to help clarify it, ping me or flag it for moderator to reopen once done.  
  
  ---
  
  