【网工Ansible】第 4 节: Ansible Quick Start: Ad hoc commands

目录

一、官方论坛/文档参考网址

二、预配置

1.模块raw介绍

2.课外知识——思科设备配置重置

3.问题与处理

1)遇到问题

2)探索尝试

3)最终解决

三、ad hoc commands

1.基本命令形式

2.用以信息统一检索收集

3.多个检索条件

4.备份命令及其它

5.备份文件检索


一、官方论坛/文档参考网址

发现了Ansible论坛:https://forum.ansible.com/ 

想起了GNS3论坛:https://gns3.com/community/featured

在我们继续讨论 Ansible Playbook 之前,让我们先看一下 ansible adhoc 命令。

ansible文档官方网站:https://docs.ansible.com/ansible/latest/ 官网中的示例中大多是面向webserver(服务器)而非网络设备的

ansible官方文档“网络自动化”部分:Network Getting Started — Ansible Community Documentation

ad hoc命令介绍:https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html

二、预配置

直接导入教程所给项目后的额外配置:

DNS配置以保证可使用域名ping与SSH登录

1.模块raw介绍

官方文档:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/raw_module.html

摘:执行低级且肮脏的 SSH 命令,而不通过模块子系统;该模块不需要远程系统上的 python

这意味着一些网上的ansible命令无法顺利执行的原因,可能与设备上未安装Python有关。

2.课外知识——思科设备配置重置

(当下发的配置存在问题的情况下,清空路由器原配置用以重新下发)

//进入特权模式

switch> enable

//清空系统配置

switch# erase startup-configure

//保存清空设置

switch# write erase

//进行热重启

switch# reload

3.问题与处理

1)遇到问题

【错误】

S1 | FAILED | rc=127 >>
/bin/sh: 1: show: not found
non-zero return code
解决方案:删除ansible主机文件中的ansible_connection=local

之后又弹出【错误】

S1 | FAILED | rc=-1 >>
to use the 'ssh' connection type with passwords, you must install the sshpass program

但又遇到新的【问题】gns3上networkautomation不能apt-get update,

2)探索尝试

尝试更新软件源配置文件(依旧不能成功)

ubuntu更改镜像源(软件源):https://blog.csdn.net/weixin_41762173/article/details/79480832

教程内所用镜像网站:https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

3)最终解决

但似乎是由于docker本身的原因,让更新命令不能实现。采用带GUI的ubuntu实体机作为替代

sudo passwd root,以设置root密码,是“su -”命令得以生效

我吃惊又惊喜到不知说什么好,重建了一个项目,重新打开网络自动化设备,这会,他又能更新了“apt-get update”。尽管输出信息中仍显示部分存在问题,不过已经能够使使用apt-get install命令下载sshpass了

三、ad hoc commands

1.基本命令形式

ansible S1 -m raw -a "show version" -u david -k,替换引号中的显示命令即可在本机上获得输出信息。(“请注意,这是实时完成的,所以我没有加快视频速度。”)

root@NetworkAutomation-1:~# ansible gns3-core -i ./gns3hosts -m raw -a "show version" -u david -k | grep flash0

2.用以信息统一检索收集

3.多个检索条件

多个检索使用单引号括起,并用“\|”作为分割(\为转义,放在正则表达式符号之前,如“.”例见下

4.备份命令及其它

Ansible Ad Hoc commands:
========================

root@NetworkAutomation-1:~# ansible gns3-core -i ./gns3hosts -m raw -a "show run" -u david -k | grep 'username' > usernames.txt

root@NetworkAutomation-1:~# cat usernames.txt

#此命令可用于获取设备的完整配置信息,作为备份
root@NetworkAutomation-1:~# ansible gns3-core -i ./gns3hosts -m raw -a "show run" -u david -k > shrun.txt

root@NetworkAutomation-1:~# cat shrun.txt

root@NetworkAutomation-1:~# more shrun.txt

root@NetworkAutomation-1:~# ansible gns3-core -i ./gns3hosts -m raw -a "show ver" -u david -k > shver.txt

root@NetworkAutomation-1:~# more shver.txt | grep Version

root@NetworkAutomation-1:~# more shver.txt | grep 'SUCCESS\|Version'

https://docs.ansible.com/ansible/2.9/user_guide/modules.html
https://docs.ansible.com/ansible/2.9/modules/modules_by_category.html
https://docs.ansible.com/ansible/2.9/modules/raw_module.html#raw-module

5.备份文件检索

实施备份,然后再使用检索,节省实时输出消耗的等待时间

  • 13
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值