Junos OS 平台选项

瞻博网络 Juniper Junos OS 支持多种连接。本页提供了有关在 Ansible 中每种连接工作原理及使用方法的详细介绍。

可用连接

CLI,仅限 junos_netconfjunos_commandjunos_ping 模组NETCONF,除 junos_netconf 外的那些启用了 NETCONF 的模组
协议SSH透过 SSH 的 XML
凭据若存在 SSH 密钥/ ssh-agent,则使用 SSH 密钥/ ssh-agent,若使用密码,则接受 -u my_user -k若存在 SSH 密钥/ ssh-agent,则使用 SSH 密钥/ ssh-agent,若使用密码,则接受 -u my_user -k
间接访问使用堡垒机(跳转主机)使用堡垒机(跳转主机)
连接设置ansible_connection: ansible.netcommon.network_cliansible_connection: ansible.netcommon.netconf
enable 模式(权限提升)不受 Junos OS 支持不受 Junos OS 支持
返回数据格式stdout[0].
  • json: result[0]['software-information'][0]['host-name'][0]['data'] foo lo0
  • text: result[1].interface-information[0].physical-interface[0].name[0].data foo lo0
  • xml: result[1].rpc-reply.interface-information[0].physical-interface[0].name[0].data foo lo0
  • ansible_connection: local 已被弃用。请使用 ansible_connection: ansible.netcommon.netconfansible_connection=ansible.netcommon.network_cli 代替。

    在 Ansible 中使用 CLI

    示例 CLI 仓库变量 [junos:vars]

    [junos:vars]
    ansible_connection=ansible.netcommon.network_cli
    ansible_network_os=junipernetworks.junos.junos
    ansible_user=myuser
    ansible_password=!vault...
    ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q bastion01"'
    
    • 若咱们使用的是 SSH 密钥(包括 ssh-agent),则可以移除 ansible_password 配置项;
    • 若咱们直接访问的主机(不通过堡垒机/跳转主机),则可移除 ansible_ssh_common_args 配置项;
    • 若咱们通过堡垒机/跳转主机访问咱们的主机,则咱们不能在 ProxyCommand 指令中包含咱们的 SSH 密码。为防止秘密外泄(例如在 ps 的输出中),SSH 不支持使用环境变量提供密码。

    示例 CLI 任务

    - name: Retrieve Junos OS version
      junipernetworks.junos.junos_command:
        commands: show version
      when: ansible_network_os == 'junipernetworks.junos.junos'
    

    在 Ansible 中使用 NETCONF

    启用 NETCONF

    在咱们可以使用 NETCONF 连接交换机前,咱们必须:

    • 使用 pip install ncclientpython -m pip install ncclient)命令,在控制节点上安装 ncclient 这个 python 软件包;
    • 在 Junos OS 设备上启用 NETCONF。

    要经由 Ansible 在新交换机上启用 NETCONF,就要通过 CLI 连接使用 junipernetworks.junos.junos_netconf 这个模组。像上面的 CLI 示例中一样,设置咱们平台级变量,然后运行一个如下的 playbook 任务:

    - name: Enable NETCONF
      connection: ansible.netcommon.network_cli
      junipernetworks.junos.junos_netconf:
      when: ansible_network_os == 'junipernetworks.junos.junos'
    

    启用 NETCONF 后,就要修改咱们的变量,以使用 NETCONF 连接。

    示例 NETCONF 仓库的 [junos:vars]

    [junos:vars]
    ansible_connection=ansible.netcommon.netconf
    ansible_network_os=junipernetworks.junos.junos
    ansible_user=myuser
    ansible_password=!vault |
    ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q bastion01"'
    

    示例 NETCONF 任务

    - name: Backup current switch config (junos)
      junipernetworks.junos.junos_config:
        backup: yes
      register: backup_junos_location
      when: ansible_network_os == 'junipernetworks.junos.junos'
    

    警告

    切勿以明文方式存储密码。我们建议使用 SSH 密钥验证 SSH 连接。Ansible 支持 ssh-agent 来管理 SSH 密钥。如果必须使用密码来验证 SSH 连接,建议使用 Ansible Vault 对密码进行加密。

    (End)

    Last change: 2025-03-24, commit: ac09bcd

    小额打赏,赞助 xfoss.com 长存......

    微信 | 支付宝

    若这里内容有帮助到你,请选择上述方式向 xfoss.com 捐赠。