Ad-Hoc 介紹
一、什么是ad-hoc 命令?
ad-hoc 命令是一種可以快速輸入的命令,而且不需要保存起來的命令。就相當於bash中的一句話shell。這也是一個好的地方,在學習ansible playbooks時可以先了解另外一種ansible基本的快速用法,不一定非要寫一個palybook文件。
一般來說,ansible的強大之處在於它的playbook 劇本。但為什么我們還要使用這種臨時的命令呢?
臨時命令適用於下面類似的場景,如果你想在聖誕節到來之時,關掉實驗室的電腦,只需要ansible 的一行命令即可,而不必編寫一個playbook文件來完成這個工作。
不過,對於配置管理和應用部署這種工作,還是需要使用“/usr/bin/ansible-playbook”命令。
1、並行和Shell 命令
接上文,ansible 服務器已經配置好使用密鑰進行認證,管理主機,如果不想使用密鑰的話,那么可以使用--ask-pass (-k) 來用密碼管理。但是最好還是用密鑰的方式。
如下:使用以下命令來查看webserver 組內主機的端口開放狀況:
[root@docker ~]# ansible webserver -a 'netstat -ulntp' 172.17.0.3 | SUCCESS | rc=0 >> Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp6 0 0 :::22 :::* LISTEN - web1 | SUCCESS | rc=0 >> Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:222 0.0.0.0:* LISTEN - tcp6 0 0 :::222 :::* LISTEN -
命令的最后也可以加 -f number ,表示使用的並發進程數目,默認是5個,如下:
ansible webserver -a 'netstat -ulntp' -f 15
/usr/bin/ansible 默認使用當前ansible 服務器登陸的用戶來進行管理,如果你不喜歡這樣,也可以使用 -u username 的方式來指定用戶,如下:
注:(zhangsan 這個用戶必須是被管理主機上真實存在的)
[root@docker ~]# ansible webserver -a "w" -u zhangsan -k
如果你不想使用當前的用戶來管理運行命令,也可以使用 --become -K 命令提升權限.
以上是關於ansible 的基礎,ansible 有許多的模塊,以上的栗子中,沒有指定模塊,因為 默認的模塊是 command ,如果要想使用其它模塊,可以用-m 模塊名 來指定。
注:command 模塊不支持擴展的shell語法,如使用管道和重定向。當然如果需要特殊的shell 語法,可以使用shell模塊來完成任務。像下面這樣:
[root@docker ~]# ansible webserver -m shell -a 'echo $TERM' web1 | SUCCESS | rc=0 >> xterm-256color 172.17.0.3 | SUCCESS | rc=0 >> xterm-256color
2、文件傳輸管理
這里是/usr/bin/ansible 命令行的另外一個用例,Ansible 可以將多個文件並發的拷貝到多台機器上。使用 copy 模塊,將文件直接傳輸到多個服務器上,如下:
[root@docker ~]# ansible webserver -m copy -a "src=/etc/hosts dest=/tmp/hosts" 172.17.0.3 | SUCCESS => { "changed": true, "checksum": "ba0ed35ca3f16342b883784ec7928491d359b8ab", "dest": "/tmp/hosts", "gid": 0, "group": "root", "md5sum": "9e979f3a6509f8d829209613343f90b9", "mode": "0644", "owner": "root", "size": 117, "src": "/root/.ansible/tmp/ansible-tmp-1487773694.97-103709947729677/source", "state": "file", "uid": 0 } web1 | SUCCESS => { "changed": true, "checksum": "ba0ed35ca3f16342b883784ec7928491d359b8ab", "dest": "/tmp/hosts", "gid": 0, "group": "root", "md5sum": "9e979f3a6509f8d829209613343f90b9", "mode": "0644", "owner": "root", "size": 117, "src": "/root/.ansible/tmp/ansible-tmp-1487773694.94-149872215856203/source", "state": "file", "uid": 0 }
檢查一下:
[root@docker ~]# ansible webserver -a 'stat /tmp/hosts' web1 | SUCCESS | rc=0 >> File: '/tmp/hosts' Size: 117 Blocks: 8 IO Block: 4096 regular file Device: fc03h/64515d Inode: 25186117 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2017-02-22 22:28:58.946882890 +0800 Modify: 2017-02-22 22:28:15.001562188 +0800 Change: 2017-02-22 22:28:15.355564788 +0800 Birth: - 172.17.0.3 | SUCCESS | rc=0 >> File: '/tmp/hosts' Size: 117 Blocks: 8 IO Block: 4096 regular file Device: fc02h/64514d Inode: 41950463 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2017-02-22 22:28:58.949882912 +0800 Modify: 2017-02-22 22:28:15.041562482 +0800 Change: 2017-02-22 22:28:15.349564744 +0800 Birth: -
說下另外一個模塊 file ,它允許更改文件的宿主以及權限,這些相同的選項同樣適用 copy 模塊,如下:
[root@docker ~]# ansible webserver -m file -a "dest=/tmp/hosts mode=600" web1 | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0600", "owner": "root", "path": "/tmp/hosts", "size": 117, "state": "file", "uid": 0 } 172.17.0.3 | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0600", "owner": "root", "path": "/tmp/hosts", "size": 117, "state": "file", "uid": 0 }
更改文件的宿主以及屬組:
[root@docker ~]# ansible webserver -m file -a "dest=/tmp/hosts mode=600 owner=zhangsan group=zhangsan" web1 | SUCCESS => { "changed": true, "gid": 1000, "group": "zhangsan", "mode": "0600", "owner": "zhangsan", "path": "/tmp/hosts", "size": 117, "state": "file", "uid": 1000 } 172.17.0.3 | SUCCESS => { "changed": true, "gid": 1000, "group": "zhangsan", "mode": "0600", "owner": "zhangsan", "path": "/tmp/hosts", "size": 117, "state": "file", "uid": 1000 }
本文屬於作者原創,轉載請注明出處:飛走不可 :http://www.cnblogs.com/hanyifeng/p/6431450.html
使用file 模塊來創建目錄,類似於 mkdir -p,如下:
[root@docker ~]# ansible webserver -m file -a "dest=/tmp/zhangsan/pp/1 mode=755 owner=zhangsan group=zhangsan state=directory" web1 | SUCCESS => { "changed": true, "gid": 1000, "group": "zhangsan", "mode": "0755", "owner": "zhangsan", "path": "/tmp/zhangsan/pp/1", "size": 6, "state": "directory", "uid": 1000 } 172.17.0.3 | SUCCESS => { "changed": true, "gid": 1000, "group": "zhangsan", "mode": "0755", "owner": "zhangsan", "path": "/tmp/zhangsan/pp/1", "size": 6, "state": "directory", "uid": 1000 }
以及刪除目錄(遞歸)和刪除文件,如下:
[root@docker ~]# ansible webserver -m file -a "dest=/tmp/zhangsan/pp/1 state=absent" 172.17.0.3 | SUCCESS => { "changed": true, "path": "/tmp/zhangsan/pp/1", "state": "absent" } web1 | SUCCESS => { "changed": true, "path": "/tmp/zhangsan/pp/1", "state": "absent" }
3.軟件包管理
包括yum 和 apt,以下是一些yum 的示例。
確保該軟件包已經安裝,但不要更新它,相當於檢查改軟件是否安裝:
[root@docker ~]# ansible webserver -m yum -a "name=vim state=present" 172.17.0.3 | SUCCESS => { "changed": false, "msg": "", "rc": 0, "results": [ "vim-enhanced-2:7.4.160-1.el7_3.1.x86_64 providing vim is already installed" ] } web1 | SUCCESS => { "changed": false, "msg": "", "rc": 0, "results": [ "vim-enhanced-2:7.4.160-1.el7_3.1.x86_64 providing vim is already installed" ] }
確保軟件安裝的是最新的版本,如下:
[root@docker ~]# ansible webserver -m yum -a "name=vim state=latest" 172.17.0.3 | SUCCESS => { "changed": false, "msg": "", "rc": 0, "results": [ "All packages providing vim are up to date", "" ] } web1 | SUCCESS => { "changed": false, "msg": "", "rc": 0, "results": [ "All packages providing vim are up to date", "" ] }
確保軟件沒有被安裝:
[root@docker ~]# ansible webserver -m yum -a "name=vim state=absent"
4.用戶和組管理
"user" 模塊允許輕松的創建和管理現有的用戶賬號,以及刪除可能存在的用戶賬號,如下:
創建一個用戶,並設置密碼(這里的密碼必須是加密后的。這里有坑,如果你寫成了明文的密碼如如:123456,那么系統的root密碼就是未知(/etc/shadow文件中,該用戶的密碼位置那就變成123456了,即誤搞成加密后的密碼是123456了!))
[root@docker ~]# ansible webserver -m user -a "name=xiaoming password=securitytext" web1 | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 1001, "home": "/home/xiaoming", "name": "xiaoming", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 } 172.17.0.3 | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 1001, "home": "/home/xiaoming", "name": "xiaoming", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 }
本文屬於作者原創,轉載請注明出處:飛走不可 :http://www.cnblogs.com/hanyifeng/p/6431450.html
創建用戶時使用加密后的密碼來設置,其它方法可參考這里
先用python 的 crypt模塊來對密碼 進行加密,如:
[root@docker ~]# python -c 'import crypt; print crypt.crypt("123456", "hello")' heepn6ZumUmSE
使用上述密碼,創建用戶:
[root@docker ~]# ansible webserver -m user -a "name=huahua shell=/bin/bash password=heepn6ZumUmSE update_password=always" 172.17.0.3 | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 1003, "home": "/home/huahua", "name": "huahua", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1003 } web1 | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 1003, "home": "/home/huahua", "name": "huahua", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1003 }
刪除用戶並移除用戶家目錄(remove 要和 state 參數一起使用,相當於userdel -r):
[root@docker ~]# ansible webserver -m user -a "name=xiaoming state=absent remove=yes" 172.17.0.3 | SUCCESS => { "changed": true, "force": false, "name": "xiaoming", "remove": true, "state": "absent" } web1 | SUCCESS => { "changed": true, "force": false, "name": "xiaoming", "remove": true, "state": "absent" }
5.從版本控制中部署程序
直接從git 上部署web 應用
使用 git模塊,要先保證遠程主機上有git軟件,如下所示,檢查git 已被安裝:
[root@docker ~]# ansible webserver -m yum -a "name=git state=present" 172.17.0.3 | SUCCESS => { "changed": false, "msg": "", "rc": 0, "results": [ "git-1.8.3.1-6.el7_2.1.x86_64 providing git is already installed" ] } web1 | SUCCESS => { "changed": false, "msg": "", "rc": 0, "results": [ "git-1.8.3.1-6.el7_2.1.x86_64 providing git is already installed" ] }
確保已經安裝之后,再來從git上拉取源碼,如下:
[root@docker ~]# ansible webserver -m git -a "repo=git://github.com/aliasmee/hello.git dest=/usr/myapp version=HEAD" web1 | SUCCESS => { "after": "f102d1927c4d42cfcca42aaa8e961be4c0b06e00", "before": null, "changed": true, "warnings": [] } 172.17.0.3 | SUCCESS => { "after": "f102d1927c4d42cfcca42aaa8e961be4c0b06e00", "before": null, "changed": true, "warnings": [] }
驗證一下:
[root@docker ~]# ansible webserver -a "ls /usr/myapp" 172.17.0.3 | SUCCESS | rc=0 >> README.md cpu_load.sh diyHttpServer.py look_IP.sh one.py two.txt web1 | SUCCESS | rc=0 >> README.md cpu_load.sh diyHttpServer.py look_IP.sh one.py two.txt
ansible webserver -m service -a "name=httpd state=started"
重啟webserver組內的 web服務器:
ansible webserver -m service -a "name=httpd state=restarted"
很遺憾,我的測試環境中,因為被管理機器都是docker 容器,而且 ansible 的 service 模塊,官方發文說現在還不支持容器的服務支持。詳見此頁面:https://github.com/ansible/ansible-modules-core/issues/4024
7.收集信息
Facts就是主機上已經發現的變量,在playbooks中有描述。可以用於實現指定的任務的條件或者獲取特定的信息,可以通過下面來獲得所有 facts:
[root@docker ~]# ansible all -m setup
8.腳本模塊
scripts 腳本模塊采用腳本名稱,后面跟空格分隔的參數列表組成,如下所示:
[root@docker ~]# ansible webserver -m script -a "/tmp/myapp/cpu_load.sh"
上面栗子中,位於本地路徑的腳本將被傳輸到遠程主機上並執行,適合本地寫好的安裝程序腳本,或其它自定義腳本。
好吧,模塊還有很多很多,具體的只有等用到時仔細研究了,下一篇開始進入playbooks 的學習了。新手上路,文中如果有錯誤的地方,還請大牛們多多指教。
本文屬於作者原創,轉載請注明出處:飛走不可 :http://www.cnblogs.com/hanyifeng/p/6431450.html
參考資料鏈接:http://docs.ansible.com/ansible/intro_adhoc.html