我的目标是将所有的{{
和}}
替换为{% raw %}{{
和}}{% endraw %}
在我所有的标记笔记中,以便我的hexo插件在渲染时不会尝试模板。
- name: Get file list of post files
find:
paths: "{{ hexo_data_post_file_dir }}"
register: hexo_data_post_file_list
- name: Replace special char
replace:
path: "{{ item[0]['path'] }}"
regexp: "{{ item[1]['pattern'] }}"
replace: "{{ item[1]['string'] }}"
with_nested:
- "{{ hexo_data_post_file_list['files'] }}"
- - pattern: "{{ '{{' }}"
string: "{{ '{% raw %}{{' }}"
- pattern: "{{ '}}' }}"
string: "{{ '}}{% endraw %}' }}"
尽管我使用"{{ '{{' }}"
要求jinja不渲染{{
,我仍然得到错误:
TASK [setup_docker_hexo : Replace special char] ********************************************************************************************************************************************
Tuesday 27 December 2022 11:58:56 +0800 (0:00:00.491) 0:00:13.572 ******
fatal: [uranus-debian]: FAILED! => {"msg": "template error while templating string: unexpected 'end of template'. String: {{. unexpected 'end of template'"}
给定文件
shell> cat test.txt.j2
{{ test_var }}
声明字符串不安全阻止模板。例如,
my_files:
- "{{ playbook_dir }}/test.txt.j2"
my_regex:
- pattern: !unsafe '{{ '
string: !unsafe '{% raw %}{{ {% endraw %}'
- pattern: !unsafe ' }}'
string: !unsafe '{% raw %} }}{% endraw %}'
测试扩展
- debug:
msg: "{{ item }}"
with_nested:
- "{{ my_files }}"
- "{{ my_regex }}"
为
TASK [debug] *********************************************************************************
ok: [localhost] => (item=['/export/scratch/tmp7/test-117/test.txt.j2', {'pattern': '{{ ', 'string': '{% raw %}{{ {% endraw %}'}]) =>
msg:
- /export/scratch/tmp7/test-117/test.txt.j2
- pattern: '{{ '
string: '{% raw %}{{ {% endraw %}'
ok: [localhost] => (item=['/export/scratch/tmp7/test-117/test.txt.j2', {'pattern': ' }}', 'string': '{% raw %} }}{% endraw %}'}]) =>
msg:
- /export/scratch/tmp7/test-117/test.txt.j2
- pattern: ' }}'
string: '{% raw %} }}{% endraw %}'
替换图案
- replace:
path: "{{ item.0 }}"
regexp: "{{ item.1.pattern }}"
replace: "{{ item.1.string }}"
with_nested:
- "{{ my_files }}"
- "{{ my_regex }}"
- debug:
msg: "{{ lookup('file', my_files.0) }}"
- debug:
msg: "{{ lookup('template', my_files.0) }}"
为
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: '{% raw %}{{ {% endraw %}test_var{% raw %} }}{% endraw %}'
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: |-
{{ test_var }}
声明变量
my_file: "{{ playbook_dir }}/test.txt"
test_var: foo bar
并使用模板
- template:
src: "{{ my_files.0 }}"
dest: "{{ my_file }}"
- debug:
msg: "{{ lookup('file', my_file) }}"
- debug:
msg: "{{ lookup('template', my_file) }}"
为
<一口>TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: '{{ test_var }}'
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: |-
foo bar
指出- 测试 的完整剧本示例
- hosts: localhost
vars:
my_files:
- "{{ playbook_dir }}/test.txt.j2"
my_regex:
- pattern: !unsafe '{{'
string: !unsafe '{% raw %}{{{% endraw %}'
- pattern: !unsafe '}}'
string: !unsafe '{% raw %}}}{% endraw %}'
my_file: "{{ playbook_dir }}/test.txt"
test_var: foo bar
tasks:
- debug:
msg: "{{ item }}"
with_nested:
- "{{ my_files }}"
- "{{ my_regex }}"
- replace:
path: "{{ item.0 }}"
regexp: "{{ item.1.pattern }}"
replace: "{{ item.1.string }}"
with_nested:
- "{{ my_files }}"
- "{{ my_regex }}"
- debug:
msg: "{{ lookup('file', my_files.0) }}"
- debug:
msg: "{{ lookup('template', my_files.0) }}"
- template:
src: "{{ my_files.0 }}"
dest: "{{ my_file }}"
- debug:
msg: "{{ lookup('file', my_file) }}"
- debug:
msg: "{{ lookup('template', my_file) }}"
- 剧本不是幂等的!