Junos OS 平台选项

Juniper Junos OS 支持多种连接。此页面提供了有关每个连接如何在 Ansible 中工作以及如何使用它的详细信息。

可用连接

CLI

仅限 junos_netconf & junos_command 模块

NETCONF

junos_netconf 之外的所有模块,它用于启用 NETCONF

协议

SSH

通过 SSH 的 XML

凭据

如果存在,则使用 SSH 密钥/SSH 代理

如果使用密码,则接受 -u myuser -k

如果存在,则使用 SSH 密钥/SSH 代理

如果使用密码,则接受 -u myuser -k

间接访问

通过堡垒机(跳转主机)

通过堡垒机(跳转主机)

连接设置

ansible_connection: ``ansible.netcommon.network_cli

ansible_connection: ``ansible.netcommon.netconf

启用模式
(特权提升)

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.network_cliansible_connection: ansible.netcommon.netconf

在 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 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 对其进行加密。

另请参阅

设置超时选项