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

使用Ansible netconf_config的confirm参数时遇类型错误求助

问题描述

使用Ansible的ansible.netcommon.netconf_config模块通过NETCONF配置Cisco交换机时,添加confirm参数触发报错:Argument must be bytes or unicode, got 'int'。已知交换机支持:candidate和:confirmed-commit等NETCONF能力,已尝试将confirm设为字符串、调整参数值、添加validate参数等操作,问题仍未解决,询问该参数是否存在bug或特定格式要求。

分析与解决

1. 模块参数的官方定义

根据ansible.netcommon.netconf_config模块规范,confirm参数接受整数类型的超时时间(单位:秒),用于触发NETCONF的confirmed-commit机制,超时未确认则自动回滚配置。报错提示与参数类型要求矛盾,大概率是旧版本模块的类型转换bug。

2. 优先升级模块版本

旧版ansible.netcommon(低于2.5.0版本)存在NETCONF参数类型处理缺陷,导致整数类型的confirm值在与设备交互时未正确编码为字符串格式。执行以下命令升级到最新稳定版:

ansible-galaxy collection install ansible.netcommon --upgrade

3. 确认设备的超时范围要求

部分Cisco交换机(如IOS XE)对confirmed-commit的超时值有范围限制(通常为60-1800秒),超出范围会触发隐性错误。确保confirm设置的数值在设备允许的范围内。

4. 绕过模块bug:直接发送原生NETCONF RPC

若升级模块后问题仍存在,可使用ansible.netcommon.netconf_rpc模块发送原生confirmed-commit RPC,绕开netconf_config的参数处理逻辑:

- name: 执行confirmed commit操作
  ansible.netcommon.netconf_rpc:
    rpc: |
      <commit xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
        <confirmed/>
        <confirm-timeout>60</confirm-timeout>
      </commit>

5. 验证完整配置提交流程

确保已正确加载候选配置到设备的candidate datastore,再执行confirmed-commit操作。完整Playbook示例:

- name: 配置Cisco交换机并触发confirmed commit
  hosts: cisco_switches
  gather_facts: false
  tasks:
    - name: 加载候选配置
      ansible.netcommon.netconf_config:
        content: |
          <config>
            <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
              <interface>
                <name>GigabitEthernet1/0/1</name>
                <description>Confirmed commit test</description>
              </interface>
            </interfaces>
          </config>
        target: candidate

    - name: 执行confirmed commit(超时60秒)
      ansible.netcommon.netconf_config:
        commit: confirmed
        confirm: 60

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 18:55:11