Ansible用于网络设备管理 part 0 安装和登录网络设备

真实在是累了,但是又怕第二天早上又忘掉,在这先大概写写。

安装:

http://www.tecmint.com/install-and-configure-ansible-automation-tool-in-linux/

http://www.tecmint.com/how-to-enable-epel-repository-for-rhel-centos-6-5/

基本就靠这俩就搞定了

登录网络设备让我比较费劲,因为之前不了解公钥私钥加密(说实话是别人讲了,没理解,这次终于理解了,改天得专门写个博客写公钥私钥和如何反其道而行的设置私钥登陆,见http://superuser.com/questions/556355/is-there-a-difference-if-ssh-key-is-created-on-serverside 这哥们儿和我想的一样哈哈!删掉生成者服务器上的私钥!然后放到本地PC!),公钥私钥是让马哥Linux给讲明白的。如何配置公钥私钥主要靠下面这俩链接

http://wilson18.com/topic/72-how-to-set-up-ssh-keys-to-log-in-on-centos-65-from-windows/ 这个链接很神奇,它给了我反其道而行之的思路,也大概让我知道了公司的账号是怎么建设的,估计就是反着来,用脚本生成给user, user自己负责保管私钥。其中的 chmod也有用!然后也传递给我一个概念,加上上面那个哥们儿心里所想的,让我觉得,key pair 这个东西的生成者是哪个机器根本不重要,重要的就是这个私钥放在哪里(登录者),公钥放在哪里(被登录者),然后还有私钥的主人是谁,也就是那个账号,账号是由ssh-keygen时那个机器上的那个Linux账号决定的(不过我觉得可以改...也许不能)。还有这个链接,http://serverfault.com/questions/591476/ssh-server-refused-our-key-for-all-users-except-root-user,这个是用来解决非root用户如何使用密钥的,当我把私钥从生成者服务器上拿走之后,就要解决这个问题,而这个链接给了我方法解决非root用户登录进来的问题。

当把上面的一切弄明白之后,我给自己在北京的一个Linux上建一个账号,然后把私钥剪切到了我的OS X上,然后解决掉一切的权限问题,免密码从OS X登录到这个机器上去。然后正餐才开始:

1、建立一个叫ansibleserver的账号,为他生成key pair, 把public key传到办公室的vc上去,这样ansibleserver就能直接登录vc switch了。

[ansibleserver@Ansible_Server ~]$ ssh 10-a01-ex-vc1
Enter passphrase for key '/home/ansibleserver/.ssh/id_rsa': 
--- JUNOS 12.3R9-S1 built 2015-03-13 08:00:19 UTC
{master:0}

2、然后比着Kirk老师在https://pynet.twb-tech.com/blog/ansible/ansible-cfg-template.html 这个博客里写的架构ansible账户的方法,为ansibleserver这个用户建立相应的用户家目录,如下图,哈哈,我画了图,清楚多了

就这样,比着这个图取理解Kirk的前两篇博客。也为ansibleserver这个账户建立好这一切。

哦?hosts文件要改一下,为的是让ansibleserver这个账户为我们办公室的vc switch服务,图里这个gituser是完全按照Kirk的博客里说的建立的。

那么下面就是如何让ansibleserver为办公室的vc switch服务,这个我参考了诸多的博客,最终知道了hosts文件应该怎么写,因为这个时候ssh已经能直接登录vc switch了,没有理由ansible命令get不到vc switch,当时报的错如下:

[ansibleserver@Ansible_Server ansible]$ ansible all -m ping -u ansibleserver
localhost | FAILED => SSH Error: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
Enter passphrase for key '/home/ansibleserver/.ssh/id_rsa': 
Enter passphrase for key '/home/ansibleserver/.ssh/id_rsa': 
cn-pek1-10-a01-ex-vc1.cnet.com | FAILED => failed to transfer file to error: unknown command: /bin/sh/ping:

Connecting to cn-pek1-10-a01-ex-vc1.cnet.com...
Permission denied (publickey,password,keyboard-interactive).
Couldn't read packet: Connection reset by peer

[ansibleserver@Ansible_Server ansible]$ 
[ansibleserver@Ansible_Server ansible]$ 

我特么的当时感到十分的奇怪,尼玛我passphrase根本没打错啊,我直接ssh命令到vc switch的时候也没出错啊,那我就去搜错报错信息中的“unknown command: /bin/sh/ping:”这一句去了,结果真就找到了解决的办法,来自这个链接 https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/ansible-project/9zG8bMyv4es/Lw6CpW_pIW0J , 这个链接里就教会了我如何去些hosts文件,来避免这个错误,它的里面写的那个IP,其实就是目标主机的地址,我这里写的我的vc的主机名,下面是我的hosts文件的内容

[root@Ansible_Server ansible]# more hosts
[switch]
cn-pek1-10-a01-ex-vc1.cnet.com   ansible_connection=local    ansible_ssh_host=cn-pek1-10-a01-ex-vc1.cnet.com

#[local] 暂时注释掉了这个localhost, 因为保留着它的话,会报出登陆localhost的permission错误,这个以前改过,应该好改
#localhost ansible_ssh_user=ansibleserver
[root@Ansible_Server ansible]# 

改成这样之后,ansible的那个ping命令就能正常使用了,效果如下:

[hsun@Ansible_Server etc]$ ansible -m ping all
cn-pek1-10-a01-ex-vc1.cnet.com | success >> {
"changed": false,
"ping": "pong"
}

我不得不吐槽一下官方文档之简陋,连个ansible all -m ping应该出来什么output它都不写,官方doc链接在这 http://docs.ansible.com/ansible/intro_getting_started.html,基本没帮上什么忙,唉。

to be re-modified....

=========2015年10月28更新========

[root@localhost RTR-TEMPLATE]# ansible -m ping switch
cn-pek1-10-a01-ex-vc1.cnet.com | success >> {
    "changed": false, 
    "ping": "pong"
}

[root@localhost RTR-TEMPLATE]#

这个地方我们直接写 ansible -m ping switch,不写all了,后面跟着的这个switch这个值,就是来自hosts文件的中括号当中的那个[switch],在这里直接写明相关的执行范围,跳过playbook的繁琐步骤,可以看出ansible太灵活了,再对比一下Jason Edelman的这篇博客

http://jedelman.com/home/network-test-automation-with-ansible/ 可以发现Jason大神跳过了更多步骤,直接集成进playbook和hosts里,它的后端使用了cpal来连接真实的网络设备,这个cpal是Jason大神写的一个Python项目,可以直接import的那种,这个的源码在https://github.com/jedelman8/cpal 可以找到,里面还需要import json,之前完全没有接触过json啊,压力好大。

========to be re-modified==========

未经授权,请勿转载!

 
原文地址:https://www.cnblogs.com/Vooom/p/4770188.html