Networking Support 网络支持

Working with Networking Devices 使用网络设备

自从Ansible 2.1开始,你现在可以使用成熟模型 - 编写 playbook 和 开发 module 来管理异构的网络设备 。Ansible使用 SSH之上的CLI、API(可用时)来支持越来越多的网络设备。

Network Automation Installation 网络自动化安装

Install the latest Ansible release

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:

  • host - 定义了远端主机的hostname或者ip地址
  • port - 定义了要连接的远端主机的端口
  • username - 定义用来认证连接的用户名
  • password - 定义用来认证连接的密码
  • transport - 定义连接传输构建的类型
  • authorize - 启用特权上升,当设备需要时
  • auth_pass - 为特权上升定义一个密码,如需要时

单个模块可以为这些参数设置默认值为普通的值,匹配设备的默认设置。例如,transport的默认值通常是 cli 。一些模块也支持如 EOS(eapi) 和NXOS(nxapi),而有些仅支持“cli”。所有的参数在每个模块的文档中已经描述。

通过允许单个任务来独立的设置transport参数,使用不同transport机制和认证证书的模块可以按照需求结合

该方法的缺点是每一个任务需要包含一个必须参数。这就是provider参数的作用。provider参数接收关键字参数并且通过任务来传递赋值连接和认证参数

下面俩个配置模块本质上是相同的(使用nxos_config)作为一个例子,但是它可以应用所有的core 网络模块:

---
nxos_config:
src: config.j2
host: "{{ inventory_hostname }}"
username: "{{ ansible_ssh_user }}"
password: "{{ ansible_ssh_pass }}"
transport: cli ---
vars:
cli:
host: "{{ inventory_hostname }}"
username: "{{ ansible_ssh_user }}"
password: "{{ ansible_ssh_pass }} "
transport: cli nxos_config:
src: config.j2
provider: "{{ cli }}"

上面俩个例子是等价的,这些参数还可以被用来确定优先级和默认值。如下示例:

---
vars:
cli:
host: "{{ inventory_hostname }}"
username: operator
password: secret
transport: cli tasks:
- nxos_config:
src: config.j2
provider: "{{ cli }}"
username: admin
password: admin

在上面这个例子中,admin用户名和admin密码会覆写provider中 “cli” 的相应值。

对于provider所有的变量来说这都是对的,所以,你可以有一个任务支持CLI或者NXAPI:

---
vars:
cli:
host: "{{ inventory_hostname }}"
username: operator
password: secret
transport: cli tasks:
- nxos_config:
src: config.j2
provider: "{{ cli }}"
transport: nxapi

如果所有的变量都是通过provider参数来提供,必要参数规则仍然由模块控制。例如:

---
vars:
conn:
password: cisco_pass
transport: cli tasks:
- nxos_config:
src: config.j2
provider: "{{ conn }}"

运行上面的例子,会导致一个错误,产生一个消息:必要参数缺失。

"msg": "missing required arguments: username,host"

整体上来说,这提供了一个非常细粒度的级别,用于控制凭据如何与模块一起使用。它给playbook设计者提供上下文变化的的最大控制力,当在运行playbook 需要的时候。

Networking Environment Variables 网络环境变量

下面的环境变量是对于Ansible网络模块是可用的。

username ANSIBLE_NET_USERNAME

password ANSIBLE_NET_PASSWORD

ssh_keyfile ANSIBLE_NET_SSH_KEYFILE

authorize ANSIBLE_NET_AUTHORIZE

auth_pass ANSIBLE_NET_AUTH_PASS

变量都是以下面的顺序计算,从低到高:

  • Default
  • Environment
  • Provider
  • Task arguments

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.

【Ansible 文档】【译文】网络支持的更多相关文章

  1. 【Ansible 文档】【译文】配置文件

    这里说明一下配置文件的内容,原文地址:http://docs.ansible.com/ansible/latest/intro_configuration.html 这个与[Ansible 文档]配置 ...

  2. 调用webapi 错误:使用 HTTP 谓词 POST 向虚拟目录发送了一个请求,而默认文档是不支持 GET 或 HEAD 以外的 HTTP 谓词的静态文件。的解决方案

    第一次调用webapi出错如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:// ...

  3. Mitmproxy首页、文档和下载 - 支持SSL的HTTP代理 - 开源中国社区

    Mitmproxy首页.文档和下载 - 支持SSL的HTTP代理 - 开源中国社区 undefined 利用Dnspod api批量更新添加DNS解析[python脚本] - 推酷 undefined

  4. 【Ansible 文档】【译文】Windows 支持

    see also:List of Windows Modules Windows Support Windows 支持 Windows: How Does It Work Windows:如何工作 正 ...

  5. 【Ansible 文档】【译文】Playbooks 变量

    Variables 变量 自动化的存在使得重复的做事情变得很容易,但是我们的系统不可能完全一样. 在某些系统中,你可能想要设置一些与其他系统不一样的行为和配置. 同样地,远程系统的行为和状态也可以影响 ...

  6. 【Ansible 文档】【译文】Ad-Hoc 命令介绍

    Introduction To Ad-Hoc Commands Ad-Hoc命令介绍 下面的例子展示了如何使用 /usr/bin/ansible 来运行ad hoc任务. 什么是ad hoc命令? 一 ...

  7. 【Ansible 文档】【译文】入门教程

    http://docs.ansible.com/ansible/latest/intro_getting_started.html Foreword 前言 到这里,你应该已经安装了Ansible,是时 ...

  8. 【Ansible 文档】【译文】常见问题

    http://docs.ansible.com/ansible/latest/faq.html 如何为一个task或者整个Playbook设置PATH或者任意其他环境变量? 通过environment ...

  9. 【C#附源码】数据库文档生成工具支持(Excel+Html)

    [2015] 很多时候,我们在生成数据库文档时,使用某些工具,可效果总不理想,不是内容不详细,就是表现效果一般般.很多还是word.html的.看着真是别扭.本人习惯用Excel,所以闲暇时,就简单的 ...

随机推荐

  1. python安装过程的一些问题解决方案

    1. pip升级后出现提示信息 DEPRECATION: The default format will switch to columns in the future. You can use –f ...

  2. windows 搭建 angular2 开发环境--白纸新手可以参考一下

    初次接触angular,感觉接触一项新的东西真的是很艰难,自从听我朋友说起angular,就对这个东西产生了一些兴趣,就开始研究,经过艰辛的各种查资料各种头痛,终于是把这环境给搭上了·最起码是可以运行 ...

  3. 快速导出云服务器mysql的表数据

    1.许多互联网应用的数据库都布署在远程的Linux云服务器上,我们经常要编辑表数据,导出表数据. 通常的做法是ssh连接到服务器,然后命令登录,命令查询导出数据,费时费力,效率低下. 安装TreeSo ...

  4. Java - "JUC"之Condition源码解析

    Java多线程系列--“JUC锁”06之 Condition条件 概要 前面对JUC包中的锁的原理进行了介绍,本章会JUC中对与锁经常配合使用的Condition进行介绍,内容包括:Condition ...

  5. -C++11可变模版参数(转载)

    泛化之美--C++11可变模版参数的妙用 1概述 C++11的新特性--可变模版参数(variadic templates)是C++11新增的最强大的特性之一,它对参数进行了高度泛化,它能表示0到任意 ...

  6. CPU单核多核区别【转载】

    CPU个数.CPU核心数.CPU线程数 我们在选购电脑的时候,CPU是一个需要考虑到核心因素,因为它决定了电脑的性能等级.CPU从早期的单核,发展到现在的双核,多核.CPU除了核心数之外,还有线程数之 ...

  7. 记一次MySQL安装出现的坑爹问题。。。

    关键词:mysql安装 msvcr100.dll缺失  vc++2010 : win10系统首次安装mysql,图方便下载了图形界面的安装包(5.6.4),本以为小事一桩:装一半失败.卸载清注册表.重 ...

  8. h5新增加的存储方法

    h4中使用的cookie把用户信息保存在客户端浏览器,但是它受到很多限制. 大小:最多能存储4k 带宽:它是随着http请求一起发送到服务器的,因此浪费一部分的带宽. 复杂度:操作复杂. h5新增加了 ...

  9. 【代码笔记】iOS-自定义loading(IanAlert)

    一,效果图. 二,工程图. 三,代码. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIVie ...

  10. 去除img默认的边框

    //当img属性src没有值时,会有难看的边框和难看的一个小图 有什么办法去掉呢? <img  src=" " /> //不要这样写 <img   />  ...