ansible介绍+基本操作
1ansible介紹
- Ansible基于Python語言實現,由paramiko和PyYAML兩個關鍵模塊構建
- 不需要安裝客戶端,通過sshd去通信
- 基于模塊工作,模塊可以由任何語言開發
- 不僅支持命令行使用模塊,也支持編寫yaml格式的playbook,易于編寫和閱讀
- 有提供UI(瀏覽器圖形化)www.ansible.com/tower,收費的
- 官方文檔 ?http://docs.ansible.com/ansible/latest/index.html
- ansible已經被redhat公司收購,它在github上是一個非常受歡迎的開源軟件,github地址https://github.com/ansible/ansible
- 一本不錯的入門電子書 https://ansible-book.gitbooks.io/ansible-first-book/
- Ansible的基本架構:
1. 核心引擎:即圖中所看到的ansible
2. 核心模塊(core module):在模塊庫(module library)中分為兩塊,一個是核心模塊另外一個就是自定義模塊(custom modules)。核心模塊中都是ansible自帶的模塊,Ansible模塊資源分發到遠程節點使其執行特定任務或匹配一個特定的狀態。這些核心核心模塊都遵循這batteries included哲學。其實這里這個還是很有意思的,batterires included:Python has a large standard library, commonly cited as one of Python’s greatest strengths,providing tools suited to many tasks. 這就意味著Python有這巨大的庫支持你完成你想完成的任務工作。
3. 自定義模塊(custom modules):如果在Ansible中滿足不了你所需求的模塊,那么Ansible也能提供添加自定義化的模塊。
4. 插件(plugin):這里我的理解就是完成較小型的任務。輔助協助模塊來完成某個功能。
5. 劇本(playbook):定義需要給遠程主機執行的一系列任務。例如安裝一個nginx服務,那么我們可以把這拆分為幾個任務放到一個playbook中。例如:第一步需要下載nginx的安裝包。第二步我可能考慮需要做的就是將我事先寫好的nginx.conf的配置文件下發的目標服務器上。第三步,我們需要把服務啟動起來。第四步,我們可能需要檢查端口是否正常開啟。那么這些步驟可以通過playbook來進行整合,然后通過inventory來下發到想要執行劇本的主機上。并且playbook也支持交互式執行playbook里面的步驟,而且如果有那一個步驟執行返回了一個錯誤報告,那么我們可以僅僅只單獨執行這個步驟。你可以把playbook理解成為一個組策略,控制管理這個OU下所有的主機行為。
6. 連接插件(connectior plugins):Ansible默認是基于SSH連接到目標機器上執行操作的。但是同樣的Ansible支持不同的連接方法,要是這樣的話就需要連接插件來幫助我們完成連接了。
7. 主機清單(host inventory):為Ansible定義了管理主機的策略。一般小型環境下我們只需要在host文件中寫入主機的IP地址即可,但是到了中大型環境我們有可能需要使用靜態inventory或者動態主機清單來生成我們所需要執行的目標主機。
2 ansible安裝
- 環境:準備兩臺機器?
- chy 192.168.212.10 ?//只需要在這臺機器上安裝ansible
- chy01 192.168.212.11
- 安裝如下 chy//192.168.212.10?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | [root@chy?~]#?yum?list?|grep?ansible?//看到有2.4版本的yum包,只需要安裝自帶的源即可 ansible.noarch??????????????????????????2.4.0.0-5.el7??????????????????extras??? ansible-doc.noarch??????????????????????2.4.0.0-5.el7??????????????????extras??? ansible-inventory-grapher.noarch????????2.3.2-1.el7????????????????????epel????? ansible-lint.noarch?????????????????????3.4.15-1.el7???????????????????epel????? ansible-openstack-modules.noarch????????0-20140902git79d751a.el7???????epel????? ansible-review.noarch???????????????????0.13.0-2.el7???????????????????epel????? kubernetes-ansible.noarch???????????????0.6.0-0.1.gitd65ebd5.el7???????epel? python2-ansible-tower-cli.noarch????????3.1.7-1.el7????????????????????epel?? [root@chy?~]#?yum?install?-y?ansible?ansible-doc?//安裝ansible [root@chy?~]#?ls?/root/.ssh/?//查看有沒有密鑰對,如果有就無需在重新生成,如果之前沒有生成過需要用ssh-keygen生成一下密鑰對。 authorized_keys??id_rsa???????????id_rsa.pub???????known_hosts??? 之后將公鑰復制到另一臺機器上 [root@chy01?~]#?cat?.ssh/authorized_keys?//公鑰已經放在了里面 [root@chy?~]#?ssh?chy01 The?authenticity?of?host?'chy01?(192.168.212.11)'?can't?be?established. ECDSA?key?fingerprint?is?de:d2:32:86:e0:89:5c:2c:51:68:92:9b:7e:40:52:5c. Are?you?sure?you?want?to?continue?connecting?(yes/no)??yes Warning:?Permanently?added?'chy01'?(ECDSA)?to?the?list?of?known?hosts. Last?login:?Wed?Nov??8?01:04:24?2017?from?chy #?cat?/etc/motd? [root@chy?~]#?vi?/etc/ansible/hosts?//配置主機組 ##?[webservers] ##?alpha.example.org ##?beta.example.org ##?192.168.1.100 ##?192.168.1.110 [testhost] chy02 chy01 說明:?testhost為主機組名字,自定義的。?下面兩個ip為組內的機器ip。 增加testhost里的內容,testhost下面可以寫主機名也可以寫ip地址,如果寫了主機名切記需要去/etc/hosts里配置,如果做了dns則不需要去/etc/hosts配置(這里需要注意的是組里添加的機器的前提是都要做密鑰認證的) |
3ansible遠程執行命令
-遠程執行命令操作如下
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@chy?~]#?ansible?testhost?-m?command?-a?'w' chy01?|?SUCCESS?|?rc=0?>> ?01:46:28?up??1:00,??2?users,??load?average:?0.16,?0.05,?0.06 USER?????TTY??????FROM?????????????LOGIN@???IDLE???JCPU???PCPU?WHAT root?????pts/0????192.168.212.1????00:54???37:48???0.02s??0.02s?-bash root?????pts/1????chy??????????????01:46????0.00s??0.26s??0.05s?w chy02?|?SUCCESS?|?rc=0?>> ?01:46:28?up??2:24,??2?users,??load?average:?0.00,?0.01,?0.05 USER?????TTY??????FROM?????????????LOGIN@???IDLE???JCPU???PCPU?WHAT root?????pts/0????192.168.212.1????00:27????1:08m??0.02s??0.02s?-bash root?????pts/1????chy??????????????01:46????1.00s??0.30s??0.00s?w 語法介紹:ansible?后跟定義的主機組?-m?跟模塊,-?a跟需要執行的命令 [root@chy?~]#?ansible?chy01?-m?command?-a?'hostname' chy01?|?SUCCESS?|?rc=0?>> chy01 如上也可以只針對一臺機器進行操作 |
如果出現了如下錯誤,請按照如下的方法解決
| 1 2 3 | ansible?127.0.0.1?-m??command?-a?'hostname' ?錯誤:"msg":?"Aborting,?target?uses?selinux?but?python?bindings?(libselinux-python)?aren't?installed!" ?解決:yum?install?-y?libselinux-python |
4 anonymous拷貝文件或目錄
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@chy?~]#?ansible?chy01?-m?copy?-a?"src=/etc/ansible?dest=/tmp/ansible_test?owner=root?group=root?mode=0755" chy01?|?SUCCESS?=>?{ ????"changed":?true,? ????"dest":?"/tmp/ansible_test/",? ????"failed":?false,? ????"src":?"/etc/ansible" } 拷貝目錄注意:源目錄會放到目標目錄下面去,如果目標指定的目錄不存在,它會自動創建。如果拷貝的是文件,dest指定的名字和源如果不同,并且它不是已經存在的目錄,相當于拷貝過去后又重命名。但相反,如果desc是目標機器上已經存在的目錄,則會直接把文件拷貝到該目錄下面。 [root@chy?~]#?ansible?chy01?-m?copy?-a?"src=/etc/passwd?dest=/tmp??owner=root?group=root?mode=0755"?//copy文件 chy01?|?SUCCESS?=>?{ ????"changed":?true,? ????"checksum":?"84c5bb4be970a90c7157c2d57401ca0ac0039eca",? ????"dest":?"/tmp/passwd",? ????"failed":?false,? ????"gid":?0,? ????"group":?"root",? ????"md5sum":?"177c3249629069b366250d27ef7820df",? ????"mode":?"0755",? ????"owner":?"root",? ????"size":?2182,? ????"src":?"/root/.ansible/tmp/ansible-tmp-1510078509.74-153887684649484/source",? ????"state":?"file",? ????"uid":?0 } |
[root@chy ~]# ansible chy01 -m copy -a "src=/etc/passwd dest=/tmp/copy.txt ?owner=root group=root mode=0755" //這里需要注意一下就是如果想要將cp過去的文件改名稱直接可以在cp的過程中改名字,在操作時拷貝到相應的目錄后,后面直接跟文件名,這樣就會直接cp過去的
chy01 | SUCCESS => {
? ? "changed": true,?
? ? "checksum": "84c5bb4be970a90c7157c2d57401ca0ac0039eca",?
? ? "dest": "/tmp/copy.txt",?
? ? "failed": false,?
? ? "gid": 0,?
? ? "group": "root",?
? ? "md5sum": "177c3249629069b366250d27ef7820df",?
? ? "mode": "0755",?
? ? "owner": "root",?
? ? "size": 2182,?
? ? "src": "/root/.ansible/tmp/ansible-tmp-1510078638.87-216978821762184/source",?
? ? "state": "file",?
? ? "uid": 0
}
[root@chy01 ~]# ls -ls /tmp/copy.txt ?//可以查看到已經cp過來并且已經改名成功
4 -rwxr-xr-x 1 root root 2182 Nov ?8 02:17 /tmp/copy.txt
5 ansible遠程執行腳本
- shell模塊是用來執行腳本的,如下是更詳細的操作
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | [root@chy?~]#?cat?/tmp/1.sh? #!/bin/bash ?echo?`date`?>?/tmp/ansible_test.txt 寫一個輸出時間的腳本 之前的saltstack腳本可以直接搞到遠程并且執行,但是ansible必須先拷貝,之后在執行 [root@chy?~]#?ansible?testhost?-m?copy?-a?"src=/tmp/1.sh?dest=/tmp/test.sh?mode=0755"?//執行的是兩臺機器 chy02?|?SUCCESS?=>?{ ????"changed":?true,? ????"checksum":?"1d452b51a06996a4ead87e91a7a288d3318f3e0c",? ????"dest":?"/tmp/test.sh",? ????"failed":?false,? ????"gid":?0,? ????"group":?"root",? ????"md5sum":?"8d6e5eb9fca38ae7c456a9da182e4426",? ????"mode":?"0755",? ????"owner":?"root",? ????"size":?50,? ????"src":?"/root/.ansible/tmp/ansible-tmp-1510080028.29-281241865001849/source",? ????"state":?"file",? ????"uid":?0 } chy01?|?SUCCESS?=>?{ ????"changed":?true,? ????"checksum":?"1d452b51a06996a4ead87e91a7a288d3318f3e0c",? ????"dest":?"/tmp/test.sh",? ????"failed":?false,? ????"gid":?0,? ????"group":?"root",? ????"md5sum":?"8d6e5eb9fca38ae7c456a9da182e4426",? ????"mode":?"0755",? ????"owner":?"root",? ????"size":?50,? ????"src":?"/root/.ansible/tmp/ansible-tmp-1510080028.3-155921385180503/source",? ????"state":?"file",? ????"uid":?0 } [root@chy?~]#?ansible?testhost?-m?shell?-a?"/tmp/test.sh"?//之后在執行腳本,-a?后跟執行的腳本即可 chy01?|?SUCCESS?|?rc=0?>> chy02?|?SUCCESS?|?rc=0?>> |
- command不能帶管道符,而shell可以用管道符
測試如下
| 1 2 3 4 5 6 7 8 | [root@chy?~]#?ansible?chy01?-m?command?-a?"cat?/etc/passwd|wc?-l" chy01?|?FAILED?|?rc=1?>> cat:無效選項?--?l Try?'cat?--help'?for?more?information.non-zero?return?code [root@chy?~]#?ansible?chy01?-m?shell??-a?"cat?/etc/passwd|wc?-l" chy01?|?SUCCESS?|?rc=0?>> 34 |
6 管理計劃
- 管理計劃用到的模塊是cron
如下操作
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [root@chy?~]#?ansible?chy01?-m?cron?-a?"name='test?cron'?job='/bin/touch/tmp/1212.txt'?weekday=6" -m?后跟模塊的名稱,-a?跟執行的命令"name?跟任務的名稱,job跟命令是什么,在后面就是分時日月周,如果定義就跟具體的,不定義直接跟*就可以 chy01?|?SUCCESS?=>?{ ????"changed":?true,? ????"envs":?[],? ????"failed":?false,? ????"jobs":?[ ????????"test?cron" ????] } [root@chy01?ansible]#?crontab?-l?//查看任務計劃 0?0?*?*?*?/bin/bash?/usr/local/sbin/nginx_logrotate.sh 0?1?*?*?*?/bin/python?/home/jumpserver/manage.py?crontab?run?3718e5baf203ed0f54703b2f0b7e9e16?#?django-cronjobs?for?jumpserver */10?*?*?*?*?/bin/python?/home/jumpserver/manage.py?crontab?run?9956b75140f4453ab1dc4aeb62962a74?#?django-cronjobs?for?jumpserver #?Lines?below?here?are?managed?by?Salt,?do?not?edit #Ansible:?test?cron *?*?*?*?6?/bin/touch/tmp/1212.txt You?have?new?mail?in?/var/spool/mail/root [root@chy?~]#?ansible?testhost?-m?cron?-a?"name='test?cron'?state=absent"?刪除任務計劃 chy01?|?SUCCESS?=>?{ ????"changed":?true,? ????"envs":?[],? ????"failed":?false,? ????"jobs":?[] } 切記在做任務計劃時千萬不要手動修改,任務計劃里的#?#?Lines?below?here?are?managed?by?Salt,?do?not?edit #Ansible:?test?cron 這行內容。 |
希望看過的童鞋多多指教,謝謝!
本文轉自我不是瘦子51CTO博客,原文鏈接:http://blog.51cto.com/chy940405/1979724,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的ansible介绍+基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: solaris10下允许root通过te
- 下一篇: 42.Linux应用调试-初步制作系统调