is it possible to have a capture group with include_pat when copying files ?

I would like to do something like that:

    - name: '{{ service_path }}\$1'
    - template: jinja
    - include_pat: E@^(.*)\.template$
    - source: {{ pillar['locations']['systemtoolsfolder'] }}\

1 Answer 1


A capture group is a type of backreference. Saltstack, being python based, uses the re.match object to do regular expression matching, which appears to support backreferences, 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 file rename after your file.recurse and add to your file.recurse an 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 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:

    - name: '{{ service_path }}\template1.conf'
    - template: jinja
    - source: {{ pillar['locations']['systemtoolsfolder'] }}\template1.conf.template
    - name: '{{ service_path }}\template2.conf'
    - template: jinja
    - source: {{ pillar['locations']['systemtoolsfolder'] }}\template2.conf.template
and so forth
  • 1
    {{ 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
    – Dragouf
    Commented Mar 16, 2018 at 16:16
  • 1
    You should add this to your original question to help clarify it, ping me or flag it for moderator to reopen once done.
    – Tensibai
    Commented Mar 22, 2018 at 14:56

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