真實在是累了,但是又怕第二天早上又忘掉,在這先大概寫寫。
安裝:
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==========
未經授權,請勿轉載!