1

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'] }}\

closed as unclear what you're asking by Pierre.Vriens, Tensibai Mar 22 '18 at 14:53

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2

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:

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
  • 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 Mar 16 '18 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 Mar 22 '18 at 14:56

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