Working with Networking Devices 使用网络设备
自从Ansible 2.1开始,你现在可以使用成熟模型 - 编写 playbook 和 开发 module 来管理异构的网络设备 。Ansible使用 SSH之上的CLI、API(可用时)来支持越来越多的网络设备。
Network Automation Installation 网络自动化安装
Install the latest Ansible> Available Networking Modules 可用的网络模块
大部分标准的Ansible模块被设计为在linux/unix或windows上工作,且不会使用网络设备。一些模块(包括 “slurp”, “raw”, and “setup”)是平台无关的,且会使用网络设备。
查看哪些可用的使用网络设备的模块,浏览 “networking” section of the Ansible module index
Connecting to Networking Devices 连接网络设备
所有的core网络设备实现了一个provider参数,其是一个参数集合,用来定义如何连接设备的特征。本小部分描述provider参数如何使用。
每一个core 网络模块支持底层的操作系统和传输协议。操作系统和模块是一对一匹配的,同时传输协议和操作系统是一对多的关系。有些操作系统可能只有一个传输选项。
每一个core网络模块支持一些基础的参数来配置transport:
Conditionals in Networking Modules 网络模块中的条件
Ansible allows you to use conditionals to control the flow of your playbooks. Ansible networking command modules use the following unique conditional statements.
eq - Equal
neq - Not equal
gt - Greater than
ge - Greater than or equal
lt - Less than
le - Less than or equal
contains - Object contains specified item
Conditional statements evaluate the results from the commands that are executed remotely on the device. Once the task executes the command set, the waitfor argument can be used to evaluate the results before returning control to the Ansible playbook.
For example:
---
- name: wait for interface to be admin enabled
eos_command:
commands:
- show interface Ethernet4 | json
waitfor:
- "result[0].interfaces.Ethernet4.interfaceStatus eq connected"
In the above example task, the command show interface Ethernet4 | json is executed on the remote device and the results are evaluated. If the path(result[0].interfaces.Ethernet4.interfaceStatus) is not equal to “connected”, then the command is retried. This process continues until either the condition is satisfied or the number of retries has expired (by default, this is 10 retries at 1 second intervals).
The commands module can also evaluate more than one set of command results in an interface. For instance:
---
- name: wait for interfaces to be admin enabled
eos_command:
commands:
- show interface Ethernet4 | json
- show interface Ethernet5 | json
waitfor:
- "result[0].interfaces.Ethernet4.interfaceStatus eq connected"
- "result[1].interfaces.Ethernet4.interfaceStatus eq connected"
In the above example, two commands are executed on the remote device, and the results are evaluated. By specifying the result index value (0 or 1), the correct result output is checked against the conditional.
The waitfor argument must always start with result and then the command index in [], where 0 is the first command in the commands list, 1 is the second command, 2 is the third and so on.