一千萬個為什麽

搜索

salt-stack:正則表達式include_pat的捕獲組



復制文件時是否可以使用包含include_pat的捕獲組?

我想做那樣的事情:

copy_template:
    file.recurse:
    - name: '{{ service_path }}\$1'
    - template: jinja
    - include_pat: [email protected]^(.*)\.template$
    - source: {{ pillar['locations']['systemtoolsfolder'] }}\

轉載註明原文: salt-stack:正則表達式include_pat的捕獲組

一共有 1 個回答:

捕獲組是反向引用類型。 Saltstack,基於python,使用re.match對象進行正則表達式匹配,似乎支持反向引用,因此允許這些反向引用。

但是,您無法將 - include_pat 字段中的反向引用 \ 1 的值傳遞回 - name ,因為 - 名稱字段永遠不會被 re.match 對象處理。即使它是,變量範圍也會阻止您在 re.match 調用之間共享反向引用。

相反,您需要執行以下操作之一:

  • 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.

例如:

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