解释 netconf 与 restconf 在 Ansible 中的连接参数差异。

解读

在国内运营商、金融、政企网络自动化面试中,面试官常把“NETCONF vs RESTCONF”作为区分候选人是否真正做过网络设备批量管理的试金石。
核心考点有三层:

  1. 协议本质差异(SSH/830 vs HTTPS/443)
  2. Ansible 连接插件对应参数集
  3. 国产化设备(华为、华三、锐捷)与 Cisco/Juniper 在参数命名上的细微区别
    答出“端口、传输层、认证方式、能力交换”四个维度即可拿到基础分;若能结合国产化场景给出ansible_network_os与ansible_connection的推荐组合,可体现工程落地经验。

知识点

  1. ansible_connection
    • netconf:值为ansible.netcommon.netconf
    • restconf:值为ansible.netcommon.httpapi(或社区插件restconf)
  2. ansible_network_os
    • netconf:华为huawei.vrp.vrp、华三h3c.h3c.h3c、Ciscocisco.iosxr.iosxr
    • restconf:需设备声明支持ietf-restconf,常见cisco.iosxe.iosxe、华为huawei.vrp.vrp(V8R10+)
  3. ansible_port
    • netconf:缺省830,国产设备常改830、2830、22(退化为SSH)
    • restconf:缺省443,也可显式写8443、18080等内部SSL端口
  4. ansible_user / ansible_ssh_pass
    • netconf:走SSH,密钥与密码二选一,支持ansible_ssh_private_key_file
    • restconf:走HTTPS,仅支持账号+密码或Token,密钥无效;需额外参数ansible_httpapi_use_ssl=yes、ansible_httpapi_validate_certs=no(内网自签证书场景)
  5. 能力交换参数
    • netconf:第一次RPC自动拉取<hello>,Ansible 侧无需手动声明,但可通过ansible_netconf_capabilities_extras追加国产私有能力集
    • restconf:无能力交换,需事先知道设备YANG模型版本;可通过ansible_httpapi_restconf_root="/restconf/data"自定义根路径,适配华三/华为私有实现
  6. Proxy 与跳板机
    • netconf:复用ansible_ssh_common_args="-o ProxyCommand=ssh -W %h:%p -p 22 bastion"
    • restconf:需ansible_httpapi_proxy_host与ansible_httpapi_proxy_port,走HTTP CONNECT隧道

答案

NETCONF 与 RESTCONF 在 Ansible 中的连接参数差异可归纳为“三不同一追加”:

  1. 连接插件不同:netconf 用ansible.netcommon.netconf,restconf 用ansible.netcommon.httpapi;
  2. 缺省端口不同:netconf 为TCP 830,restconf 为TCP 443;
  3. 认证方式不同:netconf 复用 SSH 密钥或密码,restconf 仅支持 HTTP Basic/JWT 并需ansible_httpapi_use_ssl=yes;
  4. 追加参数:restconf 必须显式关闭证书校验ansible_httpapi_validate_certs=no(内网场景),并可追加ansible_httpapi_restconf_root适配国产设备私有 URI。

示例片段:
NETCONF 主机清单

[ce_routers]
ce1 ansible_host=10.255.1.11 ansible_network_os=huawei.vrp.vrp ansible_connection=ansible.netcommon.netconf ansible_port=830 ansible_user=netconf ansible_ssh_pass=Vault@123

RESTCONF 主机清单

[ce_routers]
ce2 ansible_host=10.255.1.12 ansible_network_os=huawei.vrp.vrp ansible_connection=ansible.netcommon.httpapi ansible_port=443 ansible_user=restconf ansible_password=Vault@123 ansible_httpapi_use_ssl=yes ansible_httpapi_validate_certs=no ansible_httpapi_restconf_root="/restconf/data"

拓展思考

  1. 国产化替代场景下,华三 Comware 7 仅部分版本支持 RESTCONF,此时若强行使用 httpapi 会回退到 CLI,导致 idempotency 失效;建议先通过ansible -m huawei.vrp.vrp_capabilities或h3c.h3c.h3c_capabilities收集实际能力,再决定用 netconf 还是 restconf。
  2. 在 CI/CD 流水线中,RESTCONF 基于 HTTPS 可直接复用已有的 API 网关、WAF、堡垒机,而 NETCONF 的 SSH 通道需要额外开放 830 端口,容易被防火墙拦截;金融客户常把“零端口开放”作为安全红线,此时 RESTCONF 胜出。
  3. 性能对比:NETCONF 一次<edit-config>可携带 8k 条 VLAN 配置,XML 压缩比高;RESTCONF 走 JSON,单条大表需分页(ietf-yang-patch),在 1000+ 台接入层交换机场景下,并发 50 线程 RESTCONF 耗时约为 NETCONF 的 1.8 倍,但可读性更好,方便审计日志落库。
  4. 未来趋势:IETF 正在推进“RESTCONF over HTTP/2 with Server Push”,华为 VRP 8.3 已实验性支持;Ansible 社区计划将 httpapi 插件升级为 multiplexing 连接,届时 RESTCONF 有望补齐性能短板,成为云网一体化编排的首选。