You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止Ansible中Jinja2替换?替换Markdown标签报错求助

问题描述

我需要将所有Markdown笔记中的{{和}}替换为{% raw %}{{和}}{% endraw %},避免Hexo插件渲染时进行模板化处理。

我编写的Ansible代码如下:

- name: 获取文章文件列表
  find:
    paths: "{{ hexo_data_post_file_dir }}"
  register: hexo_data_post_file_list

- name: 替换特殊字符
  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 %}' }}"

尽管我尝试用"{{ '{{' }}"让Jinja2不渲染{{,但仍然出现报错:

TASK [setup_docker_hexo : 替换特殊字符] ********************************************************************************************************************************************
Tuesday 27 December 2022  11:58:56 +0800 (0:00:00.491)       0:00:13.572 ******
fatal: [uranus-debian]: FAILED! => {"msg": "模板渲染字符串时出错:unexpected 'end of template'. String: {{. unexpected 'end of template'}"
解决方法

问题根源是Jinja2的嵌套解析逻辑,你当前的写法仍会触发错误解析。可以通过以下几种方式修复:

方法1:用Jinja2原生raw块包裹特殊符号

直接在Playbook中用{% raw %}和{% endraw %}包裹需要保留的模板字符,彻底阻止Jinja2解析:

- name: 获取文章文件列表
  find:
    paths: "{{ hexo_data_post_file_dir }}"
  register: hexo_data_post_file_list

- name: 替换特殊字符
  replace:
    path: "{{ item[0]['path'] }}"
    regexp: "{{ item[1]['pattern'] }}"
    replace: "{{ item[1]['string'] }}"
  with_nested:
    - "{{ hexo_data_post_file_list['files'] }}"
    - - pattern: {% raw %}{{ '{{' }}{% endraw %}
        string: {% raw %}{{ '{% raw %}{{' }}{% endraw %}
      - pattern: {% raw %}{{ '}}' }}{% endraw %}
        string: {% raw %}{{ '}}{% endraw %}' }}{% endraw %}

方法2:使用单引号避免Jinja2渲染

Ansible中,单引号包裹的字符串不会被Jinja2解析,因此可以简化写法:

- name: 获取文章文件列表
  find:
    paths: "{{ hexo_data_post_file_dir }}"
  register: hexo_data_post_file_list

- name: 替换特殊字符
  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 %}' }}'

方法3:预先定义转义变量

先定义好转义后的变量,再在任务中引用,提升代码可读性:

- name: 定义转义变量
  set_fact:
    jinja_left: '{{ "{{" }}'
    jinja_right: '{{ "}}" }}'
    raw_left: '{{ "{% raw %}{{" }}'
    raw_right: '{{ "}}{% endraw %}" }}'

- name: 获取文章文件列表
  find:
    paths: "{{ hexo_data_post_file_dir }}"
  register: hexo_data_post_file_list

- name: 替换特殊字符
  replace:
    path: "{{ item[0]['path'] }}"
    regexp: "{{ item[1]['pattern'] }}"
    replace: "{{ item[1]['string'] }}"
  with_nested:
    - "{{ hexo_data_post_file_list['files'] }}"
    - - pattern: "{{ jinja_left }}"
        string: "{{ raw_left }}"
      - pattern: "{{ jinja_right }}"
        string: "{{ raw_right }}"

内容的提问来源于stack exchange,提问作者Chen Du

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 13:55:36