设为首页 收藏本站
查看: 976|回复: 0

[经验分享] 【Ansible 文档】【译文】网络支持

[复制链接]

尚未签到

发表于 2018-1-2 19:52:06 | 显示全部楼层 |阅读模式
Networking Support 网络支持

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:


  • 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.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-430923-1-1.html 上篇帖子: ansible安装使用入门 下篇帖子: Ansible专题整理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表