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

Ansible 2.10.17拆分扩展变量出错,求正确实现方法

IOS-XR Ansible命令执行错误:变量拆分未解析问题解决

问题根源

你通过set_fact拆分trust/untrust变量的逻辑未正确生效,导致下发给设备的命令中残留Jinja2拆分语法,IOS-XR设备无法识别,触发% Invalid input detected at '^' marker.错误。

正确的变量拆分实现

假设传入的trust/untrust格式为标签名:附加内容,以下是正确的set_fact任务写法:

- name: 提取trust/untrust中的标签部分
  set_fact:
    trust_tag_filter: "{{ trust.split(':')[0] }}"
    untrust_tag_filter: "{{ untrust.split(':')[0] }}"

容错处理(应对变量为空或格式异常的情况):

  • name: 提取trust/untrust中的标签部分(带容错)
    set_fact:
    trust_tag_filter: "{{ trust.split(':')[0] if (trust is defined and trust | length > 0) else '' }}"
    untrust_tag_filter: "{{ untrust.split(':')[0] if (untrust is defined and untrust | length > 0) else '' }}"

验证变量解析结果

在执行iosxr_command前添加调试任务,确认变量是否正确生成:

- name: 调试查看标签变量
  debug:
    msg: "trust_tag: {{ trust_tag_filter }}, untrust_tag: {{ untrust_tag_filter }}"

运行后若输出显示正确的标签内容,说明变量拆分逻辑正常。

命令中正确引用变量

确保iosxr_command任务里的变量被Jinja2正确渲染,示例如下:

- name: 执行IOS-XR路由过滤查询
  iosxr_command:
    commands:
      - "show route-filter {{ filter }} tag {{ trust_tag_filter }} {{ untrust_tag_filter }}"

注意:命令字符串必须用双引号包裹,确保Jinja2能解析内部的变量引用。

常见排查点

  • 确认传入的trust/untrust变量格式符合预期,无多余空格或特殊字符干扰拆分逻辑。
  • 检查Ansible 2.10.17对Jinja2split过滤器的支持(该版本原生支持,无需额外配置)。
  • 避免变量引用时的语法错误:不要在命令字符串中遗漏变量的{{ }}包裹,也不要误将变量名写成纯文本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 12:35:01