了解管理 Junos 设备时的 Ansible 清单文件
清单文件概述
Ansible 清单文件定义了运行操作指南中的命令、模块和任务的主机和主机组。根据您的 Ansible 环境和插件,文件可以采用多种格式之一。常见格式包括 INI 和 YAML。
清单文件的默认位置为 /etc/ansible/hosts。您还可以在其他位置创建特定于项目的清单文件,例如,在项目的 playbook 目录中。Ansible 配置文件可以指定非默认清单文件位置的路径。您还可以在执行 playbook 时指定清单文件的位置。
清单文件可以列出单个主机或用户定义的主机组。因此,您可以将具有类似角色的 Junos 设备组织到组中。然后,您就可以在该组中的设备上执行相同的操作和配置任务。例如,如果您管理一个或多个数据中心,则可以为需要相同操作集(如应用类似配置和升级 Junos OS)的交换机创建组。
为了使用 Ansible 管理 Junos 设备,您必须在每台托管设备上拥有一个具有相应访问权限的 Junos OS 登录帐户。您必须确保清单文件中每个主机都有对应用户名和密码或访问密钥。
以下 INI 格式的示例清单文件定义了一个主机、 host1和两组主机,routers以及 switches。清单还定义了一个父组network,其中包括 和 组routersswitches。
host1.example.com [routers] router1.example.com router2.example.com [switches] switch1.example.com switch2.example.com [network:children] routers switches
有关 Ansible 清单文件的更多信息,请参阅 https://docs.ansible.com/projects/ansible/latest/inventory_guide/intro_inventory.html 上的 Ansible 官方文档。
juniper.device 集合:连接和网络操作系统变量
从 2.0.0 版开始 juniper.device ,我们将瞻博网络 juniper.device 产品系列和红帽 Ansible junipernetworks.junos 产品系列整合为一个 juniper.device 产品系列。尽管您在同一命名空间下执行模块,但每个模块集使用不同的 Ansible 连接和网络操作系统值,因此您不能在同一操作指南中混合使用这些模块。
您可以在操作指南、清单文件、主机或组变量文件或者命令行参数中定义 Ansible 连接。如果在清单文件中定义连接,请设置为 ansible_connection 模块的适当值。 表 1 概述了每个模块集的连接值。对于模块 junipernetworks.junos ,还必须定义 ansible_network_os.
| 模块集 | ansible_connection value |
ansible_network_os value |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
无需在清单文件中定义 ansible_connection ,还可以在剧本的播放中进行定义 connection 。两个参数采用相同的值。
除了所需的连接和网络操作系统变量外,清单文件还可以选择性地定义 Ansible 用于连接到设备的用户名。如果端口与默认端口不同,它还可以指定端口。我们还建议您在 Ansible 配置文件或 Ansible 清单文件中定义 Python 解释器路径。如果 Ansible 无法找到您的环境使用的 Python 安装,指定 Python 解释器路径可以防止不必要的错误。
以下部分为每个模块集提供了一些简化的清单文件示例。
juniper.device 模块的示例清单文件
如果使用原始 juniper.device 模块来管理 Junos 设备,则清单文件应定义 ansible_connection 为 local 或 juniper.device.pyez。
以下清单文件定义了一个名为 junos 的组,该组具有三个主机。主机使用连接 local 连接到受管理设备。
[junos] router1.example.com router2.example.com router3.example.com [junos:vars] ansible_connection=local ansible_user=username ansible_port=port [all:vars] ansible_python_interpreter=python-interpreter-path
junipernetworks.junos 模块的示例清单文件
如果使用junipernetworks.junos模块(juniper.device.junos_*模块)来管理 Junos 设备,则清单文件必须为该模块集定义相应的变量。可以使用全限定名称ansible.netcommon.netconf或 ansible.netcommon.network_cli指定值。ansible_connection或者,您可以只指定 netconf 或 network_cli。查看模块文档,以验证每个模块支持的连接值。
以下清单文件定义了一个名为 junos 的组,该组具有三个主机。Ansible 使用连接 ansible.netcommon.netconf 连接到托管设备。清单文件还包括这些模块的所需 ansible_network_os 值。
[junos] router1.example.com router2.example.com router3.example.com [junos:vars] ansible_connection=ansible.netcommon.netconf ansible_network_os=juniper.device.junos ansible_user=username ansible_port=port [all:vars] ansible_python_interpreter=python-interpreter-path