標籤:管理系統 程式 動態 資訊 手工
這篇總結一下Inventory,變數,目標匹配,及/usr/bin/ansible 命令列常用的模組
上一篇也說過Inventory的預設路徑是在/etc/ansible/hosts,當然這個東西可以說是靜態,為啥是靜態呢,你要手工的把你的要管理的主機寫進去。
當然還有一種動態Inventory,這個其實也簡單,就是如果你已經有了一個資源管理系統,裡面有你們的所有主機資訊。然後咱們寫個指令碼程式,把你們資源管理系統裡面的這些資訊給拉過來,以json格式呈現就OK了。
其實ansible還有一種管理主機的方法,我們隨便把主機定義到一個檔案裡,然後
用ansible或ansible-playbook的 -i參數去調用就OK了。不過這個要建立個目錄結構,後面寫playbook的時候再總結一下。
樓主這裡沒許可權,還看不到資源管理系統,就不搞動態Inventory
OK,先看看靜態Inventory
[email protected]:~$ cat -n /etc/ansible/hosts 1[alltest:children] 2salt 3leihuo 4 5[salt] 6salt-master ansible_ssh_user=lixc ansible_ssh_pass=123456 710.240.162.112 ansible_connection=paramiko 8 9[leihuo] 10lixc ansible_ssh_host=192.168.131.203 ansible_ssh_port=21100 1110.240.162.11[1:9]:22
樓主這個簡單的小例子,第1行,alltest這個組包含倆子組分別是下面的salt,和leihuo
第6行可以設定主機的預設串連使用者,及密碼
第7行可以設定ssh的串連方式,預設是openssh,我這裡用paramiko,不用官網推薦用openssh,因為paramiko查詢key的時候,很耗時,效率不高。
第10行,可以給主機隨便取個別名,這裡的“lixc”就是一個別名,如果ssh預設連接埠不是22,這裡可以
指定特定的連接埠
指定ssh連接埠也可以像第11行,這麼指定。
不過以上兩種指定ssh連接埠方法,只針對我們有少部分的主機是特殊連接埠,如果我們所以主機都是指定的連接埠,不可能每個主機都這麼弄一下吧。
設定檔裡有個選項,改成我們需要的連接埠就OK了,修改後對全域有效
[email protected]:~$ grep "remote_port" /etc/ansible/ansible.cfg remote_port = 22
第7行和11行,是倆相同的主機,說明同一主機可以在不同的組中。在現實當中就像我一台伺服器即可以裝mysql也可以裝apache是一個道理。
OK,靜態Inventory搞完了,再搞搞吧,變數吧。
ansible的變數主要是給後面的playbook使用的。
分為主機變數和組變數兩種類型。
看個例子
[email protected]:~$ cat -n /etc/ansible/hosts 1[alltest:children] 2salt 3leihuo 4 5[salt] 6salt-master salt-port=4505 mysql-port=3306 710.240.162.112 salt-path=/usr/bin/salt-call 8 9[leihuo] 10lixc ansible_ssh_host=192.168.131.203 ansible_ssh_port=21100 1110.240.162.11[1:9]:22 12 [alltest:vars] 13 ls-path=/bin/ls 14 liss=lisisi
6,7行設定主機變數
12-14行,設定的為alltest這個組的變數。組變數就是,我這個組的成員都可以用
當然我們也可以不在/etc/ansible/hosts裡面定義變數,也可以把變數寫進單獨的檔案裡,不過變數定義的形式就不是誰=誰,這麼個形式了。而是遵循yaml文法的key: value的形式。
我們把上面定義的變數寫進檔案。
[email protected]:~$ for dir in {host_vars,group_vars};do ls /etc/ansible/${dir};done10.240.162.112 salt-masteralltest
看一下檔案定義的格式
[email protected]:~$ cat /etc/ansible/host_vars/salt-master ---salt-port: 4505mysql-port: 3306
下面總結下,ansible的patterns,ansible目標的匹配相對來說還是比較簡單,單一的,不像salt很強大,grains,pillar,正則等等都可以用來匹配目標。
基本格式:
ansible <pattern_goes_here> -m <module_name> -a <arguments>
匹配所有主機
*或者all
匹配多個組
salt:leihuo
在salt這個組裡,但不能在leihuo這個組裡的主機
salt:!leihuo
取兩個組的交集
salt:&leihuo
排除某一主機
ansible-playbook site.yaml --limit salt-msater
當然也可以用正則,在/etc/ansible/hosts裡面去定義。如
~salt(master|minion)\.li*\.com
OK,搞得好像流水帳一樣,好吧,下面搞搞ansible的命令列模組吧
先說說啥時候用命令列吧,用這種東西的時候,一般是咱們臨時的去做一些個小事情,可能這次做了,下次就再也不弄了。就用這種方式。。。如果要去幹大型或需要經常重複使用的活兒,就用play-book吧
多了也不說了,直接抄官網的例子了。
好,先來聊聊命令列三劍客,command,shell,raw
ansible命令列,預設使用的模組就是command了。
[email protected]:~$ grep -n "module_name" /etc/ansible/ansible.cfg 59:#module_name = command[email protected]:~$ ansible salt -a ‘ls /tmp‘10.240.162.112 | success | rc=0 >>aptitude-root.6058:d5Ogydtest.logsalt-master | success | rc=0 >>[email protected]:~$
看到了吧,我們如果不加-m 參數的話,預設使用的就是command模組了。
OK,其實command模組,我們平時使用的命令,都可以在-a參數後面加上去執行了。但是,command模組執行命令,和我們平時在真實機器上,/bin/sh環境下執行命令還是有所區別的。
啥區別呢?區別是,command不能用shell的一些特性,看下面這個例子,大夥就知道了。
[email protected]:~$ ansible salt-master -a ‘ls /tmp/*‘salt-master | FAILED | rc=2 >>ls: cannot access /tmp/*: No such file or directory
看到了吧,command弄不了shell中的*,那有誰可以弄?shell這個模組可以,看個例子
[email protected]:~$ ansible salt -m shell -a ‘ls /tmp/*‘ -ssalt-master | success | rc=0 >>/tmp/test
看到了吧。哈哈,其實command能乾的活,shell都可以幹,那要這個command模組還是啥用呢?
官方上說,command用起來更安全,更有可預知性。建議俺們,不到萬不得已,還是不要用shell模組滴好。
在這裡樓主還有一個疑問。官網上說,command使用不了nodes上的環境變數,但是樓主測了下,居然可以用,難不成是官方文檔太老了,沒來得及更新?
[email protected]:~$ ansible salt-master -m shell -a ‘echo $HOME‘ salt-master | success | rc=0 >>/home/lixc[email protected]:~$ ansible salt-master -a ‘echo $HOME‘ salt-master | success | rc=0 >>/home/lixc
OK,兩位劍客都介紹完了,怎麼少的了第三位大俠呢?
raw這東西,咋用的呢,其實前面說的command,shell能乾的活,raw都能幹。
大夥都知道ansible啟動並執行環境是管理機上裝ansible,但是客戶機上是要裝python的。
raw最拿手的好活,就是,如果你有的機器是沒有裝python,或者說你的是python2.4以下,木有裝python-simplejson的,用它可以裝python,裝python-simplejson。
raw還有一個拿手好戲,就是你的有些機器壓根就裝不了python,OK,不用裝了,直接用raw吧,也可以玩ansible
看個例子。
[email protected]:~$ ansible 10.240.162.250 -a ‘ls /tmp/‘ -u root -kSSH password: 10.240.162.250 | FAILED >> { "failed": true, "msg": "/bin/sh: /usr/bin/python: not found\r\n", "parsed": false}[email protected]:~$ ansible 10.240.162.250 -m shell -a ‘ls /tmp/*‘ -u root -kSSH password: 10.240.162.250 | FAILED >> { "failed": true, "msg": "/bin/sh: /usr/bin/python: not found\r\n", "parsed": false}[email protected]:~$ ansible 10.240.162.250 -m raw -a ‘ls /tmp/*‘ -u root -kSSH password: 10.240.162.250 | success | rc=0 >>/tmp/test
看到了吧,我新裝了一台debian的機器,裡面沒裝python,command和shell模組都不能用了,raw照樣OK。現在咱們把python給裝上。
[email protected]:~$ ansible 10.240.162.250 -m raw -a ‘aptitude -y install python‘ -u root -k >/dev/null SSH password: [email protected]:~$
python裝好了,下面咱們看看還能玩command和shell嗎?
[email protected]:~$ ansible 10.240.162.112 -a ‘ls /tmp/‘ -u root -kSSH password: 10.240.162.112 | success | rc=0 >>test[email protected]:~$ ansible 10.240.162.112 -m shell -a ‘ls /tmp/‘ -u root -kSSH password: 10.240.162.112 | success | rc=0 >>test
OK,可以完了。現在問題又出來了,既然raw這麼猛,那還有command和shell有個毛用?
答案還是官網上的一句話,
人家command比較安全有可預知性,所以我們平時用的時候,最好用command。
需要用到shell特性的時候,再去去shell。
如果客戶機上沒裝python,或者python-simplejson,那咱們就用raw把python或python-simplejson給裝上,然後就別用了,回過頭用command和shell
如果,你的機器實在裝不了python就用raw吧,不過裝不了python大多數指的就是路由器交換器了。
OK,三劍客聊完了。
看看檔案管理吧
拷貝個檔案,拷貝之前要備份,並修改檔案到指定的屬性。
[email protected]:~$ ansible salt-master -m copy -a ‘src=/etc/sudoers dest=/etc/sudoers owner=root group=root mode=440 backup=yes‘ -ssalt-master | success >> { "changed": false, "dest": "/etc/sudoers", "gid": 0, "group": "root", "md5sum": "5f82d8684e43943bec2f07c0d1823352", "mode": "0440", "owner": "root", "path": "/etc/sudoers", "size": 1742, "state": "file", "uid": 0}
OK,查看一下是不是按照要求來的
[email protected]:~$ ansible salt-master -m shell -a ‘ls -l /etc/sudoers*‘salt-master | success | rc=0 >>-r--r----- 1 root root 1742 Jun 27 15:04 /etc/sudoers-r--r----- 1 root root 700 Jun 26 16:09 /etc/[email protected]:04~
是對的,下面看看咋樣修改檔案屬性,建立檔案吧
先弄個串連檔案
[email protected]:~$ ansible salt-master -m file -a ‘src=/etc/sudoers dest=/tmp/sudoers mode=440 owner=lixc group=lixc state=link‘ salt-master | success >> { "changed": false, "dest": "/tmp/sudoers", "gid": 1000, "group": "lixc", "mode": "0777", "owner": "lixc", "size": 12, "src": "/etc/sudoers", "state": "link", "uid": 1000}[email protected]:~$ ansible salt-master -m shell -a ‘ls -l /tmp/sudoers‘salt-master | success | rc=0 >>lrwxrwxrwx 1 lixc lixc 12 Jun 27 15:15 /tmp/sudoers -> /etc/sudoers
建立個檔案
[email protected]:~$ ansible salt-master -m file -a ‘dest=/tmp/lixc.log owner=lixc group=lixc mode=644 state=touch‘salt-master | success >> { "changed": true, "dest": "/tmp/lixc.log", "gid": 1000, "group": "lixc", "mode": "0644", "owner": "lixc", "size": 0, "state": "file", "uid": 1000}[email protected]:~$ ansible salt-master -m shell -a ‘ls -l /tmp/lixc.log‘salt-master | success | rc=0 >>-rw-r--r-- 1 lixc lixc 0 Jun 27 15:19 /tmp/lixc.log
遞迴建立個檔案夾
[email protected]:~$ ansible salt-master -m file -a ‘dest=/tmp/a/b/c owner=lixc group=lixc mode=755 state=directory‘salt-master | success >> { "changed": true, "gid": 1000, "group": "lixc", "mode": "0755", "owner": "lixc", "path": "/tmp/a/b/c", "size": 4096, "state": "directory", "uid": 1000}
查看一下結果,
[email protected]:~$ ansible salt-master -a ‘tree /tmp/a‘salt-master | success | rc=0 >>/tmp/a`-- b `-- c
刪除剛才那個檔案夾
[email protected]:~$ ansible salt-master -m file -a ‘dest=/tmp/a/ state=absent‘salt-master | success >> { "changed": true, "path": "/tmp/a/", "state": "absent"}
查看一下結果
[email protected]:~$ ansible salt-master -a ‘tree /tmp/a‘salt-master | success | rc=0 >>/tmp/a [error opening dir]
接下來裝個mysql吧,順便玩玩搞包管理,使用者管理,服務管理這些東西。
OK,先建立個mysql使用者吧
[email protected]:~$ ansible salt-master -m user -a ‘name=mysql shell=/sbin/nologin createhome=no‘ -ssalt-master | success >> { "changed": true, "comment": "", "createhome": false, "group": 1002, "home": "/home/mysql", "name": "mysql", "shell": "/sbin/nologin", "state": "present", "system": false, "uid": 1002}
好,安裝mysql
[email protected]:~$ ansible salt-master -m apt -a ‘name=mysql-server state=installed‘ -s >/dev/null[email protected]:~$
好,配置/etc/mysql歸mysql使用者使用
[email protected]:~$ ansible salt-master -m file -a ‘dest=/etc/mysql mode=644 owner=mysql group=mysql‘ -ssalt-master | success >> { "changed": true, "gid": 1002, "group": "mysql", "mode": "0644", "owner": "mysql", "path": "/etc/mysql", "size": 4096, "state": "directory", "uid": 1002}
啟動mysql服務
[email protected]:~$ ansible salt-master -m service -a ‘name=mysql state=started‘ -ssalt-master | success >> { "changed": false, "name": "mysql", "state": "started"}
OK,安裝好,再走一遍刪除的流程吧
停止服務
[email protected]:~$ ansible salt-master -m service -a ‘name=mysql state=stopped‘ -ssalt-master | success >> { "changed": true, "name": "mysql", "state": "stopped"}
刪除mysql
[email protected]:~$ ansible salt-master -m apt -a ‘name=mysql-server state=absent‘ -s >/dev/null[email protected]:~$
刪除mysql使用者
[email protected]:~$ ansible salt-master -m user -a ‘name=mysql state=absent‘ -ssalt-master | success >> { "changed": true, "force": false, "name": "mysql", "remove": false, "state": "absent"}
OK,完成了,過程都是瞎掰的,主要是想測測ansible的功能的。
還有一個後台執行的功能。-B 30是設定後台執行時間為30秒,-P2是沒兩秒鐘報告一次狀態,這個當你的任務要執行很長時間的時候可以用。
[email protected]:~$ ansible salt-master -m apt -a ‘name=apache2 state=installed‘ -s -B 30 -P2 >>/dev/null
一葉浮萍歸大海,人生何處不相逢,該聊到ansible的facts了,這東西就和salt裡面的grains一個樣。
直接看看,輸出的東西太多了,就不在這裡顯示了
[email protected]:~$ ansible salt-master -m setup >/dev/null[email protected]:~$
本文出自 “西風” 部落格,請務必保留此出處http://lixcto.blog.51cto.com/4834175/1431659