一、常見的自動化運維工具:
OS Provisioning:PXE, Cobbler
OS Config:puppet, saltstack, chef, func
Task Exec:fabric, saltstack, func
Program Deployment:fabric
管理主機控制被管理節點的方式:
agent:被管理節點上需要安裝代理程序以接受管理主機的操作;如puppet, func
agentless:被管理節點不需安裝代理程序,管理主機藉助ssh傳遞操作指令,如ansible, fabric;
二、ansible介紹
ansible是一款基於python開發,揉合了衆多自動化運維工具功能的輕量級自動化運維工具,目前實現了除系統安裝以外的批量系統配置、批量任務執行及批量程序部署等功能。
1、ansibler的架構
⑴ansible核心程序
⑵connection plugins:連接插件,負責和被控制端通信;
⑶host inventory:主機庫,定義可控制的主機;
⑷modules:core modules、custom modules
⑸playbook:劇本,使用YAML編寫的聲明性的配置文件
⑹plugins:各種插件,完成日誌記錄、郵件等功能;
ansible從host inventory中獲取被控制的主機信息,通過connection plugins連接主機,調用指指定模塊向被控制端發送操作指令。
2、ansible的特性
⑴高度模塊化,藉助模塊完成各種任務
⑵agentless,即無需在被控制端安裝agent
⑶默認基於ssh協議向被控制端發送操作指令
①基於密鑰認證
②在inventory文件中指定賬號和密碼
⑷一系列任務執行可寫成劇本(playbook)
⑸具有冪等性:不會重複執行相同操作,比如不會重複安裝軟件
三、ansible安裝配置
以下node1爲ansible主機,node2、node3、node4爲被控主機
1、rpm安裝(epel源)
yum -y install ansible #只需要在控制端安裝
主配置文件:/etc/ansible/ansible.cfg
inventory:/etc/ansible/hosts
[root@node1 ~]# yum -y install ansible Installed: ansible.noarch 0:1.9.4-1.el6 Dependency Installed: PyYAML.x86_64 0:3.10-3.1.el6 libyaml.x86_64 0:0.1.3-4.el6_6 python-babel.noarch 0:0.9.4-5.1.el6 python-crypto2.6.x86_64 0:2.6.1-2.el6 python-httplib2.noarch 0:0.7.7-1.el6 python-jinja2.x86_64 0:2.2.1-2.el6_5 python-keyczar.noarch 0:0.71c-1.el6 python-pyasn1.noarch 0:0.0.12a-1.el6 python-setuptools.noarch 0:0.6.10-3.el6 python-simplejson.x86_64 0:2.0.9-3.1.el6 sshpass.x86_64 0:1.05-1.el6 Complete! [root@node1 ~]# rpm -ql ansible | less /etc/ansible /etc/ansible/ansible.cfg #主配置文件 /etc/ansible/hosts #host inventory /etc/ansible/roles /usr/bin/ansible #主程序 /usr/bin/ansible-doc #獲取幫助信息 /usr/bin/ansible-galaxy /usr/bin/ansible-playbook #運行“劇本”的命令 /usr/bin/ansible-pull /usr/bin/ansible-vault #可把playbook加密存放 /usr/lib/python2.6/site-packages/ansible ... [root@node1 ~]# vim /etc/ansible/ansible.cfg ... [defaults] # some basic default values... inventory = /etc/ansible/hosts #inventory文件路徑 #library = /usr/share/my_modules/ remote_tmp = $HOME/.ansible/tmp pattern = * forks = 5 #執行任務時啓動的併發線程數 poll_interval = 15 sudo_user = root #ask_sudo_pass = True #ask_pass = True transport = smart #remote_port = 22 #遠程被控節點的ssh端口 module_lang = C ... ...
2、添加被控主機
vim /etc/ansible/hosts
ntp.magedu.com #不屬於任何組的主機直接定義在文件中最上端
[websrvs] #可將一批主機歸於一個組
www1.magedu.com:2222 #若被控節點的ssh使用了非默認端口,可以在被控節點後標明
www2.magedu.com
www[01:50].example.com #可使用通配
172.16.100.7
[dbsrvs]
db-[a:f].example.com
◆默認以root用戶身份執行,如果是口令認證,則需要在任務執行時輸入密碼或在inventory中的被控制主機旁以參數的形式指明,例如:
192.168.30.20 ansible_connection=ssh ansible_user=fedora ansible_port=2222 ansible_pass=magedu ansible_become=true ansible_become_user=root ansible_become_method=sudo ansible_become_pass=magedu
ansible_user、ansible_port、ansible_pass分別取代了2.0以前版本中的ansible_ssh_user、ansible_ssh_port、ansible_ssh_pass
基於口令認證當然比較麻煩,故通常配置ssh基於密鑰認證
inventory中還有一些可配置的參數,具體見官方文檔
◆主機變量
可以在inventory中定義主機時爲其添加主機變量。例如:
[websrvs]
www1.magedu.com http_port=80 maxRequestsPerChild=808
www2.magedu.com http_port=8080 maxRequestsPerChild=909
◆組變量
組變量是指賦予給指定組內所有主機上的變量。例如:
[websrvs]
www1.magedu.com
www2.magedu.com
[websrvs:vars]
ntp_server=ntp.magedu.com
nfs_server=nfs.magedu.com
◆組嵌套
inventory中,組還可以包含其它的組,並且也可以向組中的主機指定變量。例如:
[apache]
httpd1.magedu.com
httpd2.magedu.com
[nginx]
ngx1.magedu.com
ngx2.magedu.com
[websrvs:children]
apache
nginx
[websrvs:vars]
ntp_server=ntp.magedu.com
注意:在inventory文件中定義的變量只能在playbook中使用,而ansible命令不支持
[root@node1 ~]# cd /etc/ansible/ [root@node1 ansible]# vim hosts #inventory文件中有些配置示例,可參考 .... # Ex 1: Ungrouped hosts, specify before any group headers. ## green.example.com #可以是主機名 ## blue.example.com ## 192.168.100.1 #也可是ip地址 ## 192.168.100.10 # Ex 2: A collection of hosts belonging to the 'webservers' group ## [webservers] ## alpha.example.org ## beta.example.org ## 192.168.1.100 ## 192.168.1.110 # If you have multiple hosts following a pattern you can specify # them like this: ## www[001:006].example.com # Ex 3: A collection of database servers in the 'dbservers' group ## [dbservers] ## ## db01.intranet.mydomain.net ## db02.intranet.mydomain.net ## 10.25.1.56 ## 10.25.1.57 ... [root@node1 ansible]# cp hosts hosts.bac [root@node1 ansible]# vim hosts [websrvs] 192.168.30.20 192.168.30.13 [dbsrvs] 192.168.30.14
3、配置ssh基於密鑰認證
ssh-keygen -t rsa
ssh-copy-id -i .ssh/id_rsa.pub [email protected]
[root@node1 ~]# ssh-keygen -t rsa ... Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 49:87:45:68:24:2d:a1:23:6f:e3:8d:ec:88:a8:43:f9 root@node1 The key's randomart image is: +--[ RSA 2048]----+ | o+.oo | | ...+o | | . o oo . | | o . . o | | . + S | | o + + | |. . + . | |o. E | |=.. . | +-----------------+ [root@node1 ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected] ... [root@node1 ~]# ssh [email protected] 'hostname' node2 #以同樣的方式將公鑰文件複製到其它被控節點
4、ansible的幾個命令:
⑴ansible-doc
ansible-doc -l:列出所有模塊
ansible-doc [-s] MODULE_NAME:查看指定模塊的用法
-s:生成可以複製到playbook中以作修改的摘要;簡而言之就是以簡要形式顯示模塊用法
⑵ansible:執行任務
ansible <host-pattern> [-f forks][-m module_name] [-a args] [options]
-m module_name:指定調用的模塊
-a args:指定向模塊傳遞的參數
-f #:指定併發數
-k:默認基於密鑰認證,使用該選項指定基於口令認證
◆host-pattern:
all #所有主機
192.168.30.20, 192.168.30.*
www.example.com, www.example.com:ftp.test.com
websrvs #組中的所有主機
websrvs:dbsrvs #兩個組中的所有主機
websrvs:!dbsrvs #在websrvs不在dbsrvs的主機
websrvs:&dbsrvs #同時在websrvs和dbsrvs中的主機
5、ansible常用模塊及其使用示例
①command:命令模塊,默認模塊,用於在遠程執行命令;command模塊並不支持shell變量和管道等,若想使用shell來執行,應調用shell模塊
例:ansible [-m command] all -a 'date'
[root@node1 ~]# ansible all -a 'date' 192.168.30.14 | SUCCESS | rc=0 >> Sat Apr 9 01:21:18 CST 2016 192.168.30.13 | SUCCESS | rc=0 >> Tue May 3 16:18:06 CST 2016 192.168.30.20 | SUCCESS | rc=0 >> Tue May 3 16:18:06 CST 2016 [root@node1 ~]# ansible 192.168.30.20:192.168.30.14 -a 'hostname' 192.168.30.14 | SUCCESS | rc=0 >> node4 192.168.30.20 | SUCCESS | rc=0 >> node2
②cron:週期性任務計劃模塊
state:
present:生成
absent:移除
例:ansible websrvs -m cron -a 'name="sync time" minute="*/3" job="/usr/sbin/ntpdate 172.16.100.1 &> /dev/null"'
[root@node1 ~]# ansible all -m cron -a 'name="sync time" minute="*/10" job="/usr/sbin/ntpdate 0.centos.pool.ntp.org &> /dev/null"' 192.168.30.14 | SUCCESS => { "changed": true, "jobs": [ "sync time" ] } 192.168.30.20 | SUCCESS => { "changed": true, "jobs": [ "sync time" ] } 192.168.30.13 | SUCCESS => { "changed": true, "jobs": [ "sync time" ] }
[root@node2 ~]# crontab -l #Ansible: sync time */10 * * * * /usr/sbin/ntpdate 0.centos.pool.ntp.org &> /dev/null
③user:管理用戶
例:ansible websrvs -m user -a 'name=fedora password=加密串'
[root@node1 ~]# openssl passwd -1 -salt `openssl rand -hex 4` Password: $1$56ff09e3$xedL2L61OEqnX0I7uVCiV. [root@node1 ~]# ansible dbsrvs -m user -a 'name=federa password=$1$56ff09e3$xedL2L61OEqnX0I7uVCiV' 192.168.30.14 | SUCCESS => { "changed": true, "comment": "", "createhome": true, "group": 503, "home": "/home/federa", "name": "federa", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 503 } [root@node1 ~]# ansible dbsrvs -m user -a 'name=federa state=absent' 192.168.30.14 | SUCCESS => { "changed": true, "force": false, "name": "federa", "remove": false, "state": "absent" }
④copy:複製文件
src=:指定本地源文件路徑
content=:取代src=,表示直接用此處指定的內容生成爲目標文件內容
dest:指定遠程目標文件路徑
force: 當設置爲yes時,如果目標主機存在該文件,但內容不同,會強制覆蓋。默認爲yes
backup: 在覆蓋之前備份源文件,yes/no
例:ansible websrvs -m copy -a 'src=/mine/ntp.conf dest=/etc/ntp.conf[ owner=root group=root mode=644 backup=yes]'
ansible websrvs -m copy -a 'content="hello" dest=/tmp/test.ansible'
[root@node1 ~]# ansible all -m copy -a 'src=/root/test.ansible dest=/tmp/' 192.168.30.13 | SUCCESS => { "changed": true, "checksum": "5c074e046394da951a17e8baa97fb67833f20733", "dest": "/tmp/test.ansible", "gid": 0, "group": "root", "md5sum": "c4c6374c8a58aadfda80777e13f17e9f", "mode": "0644", "owner": "root", "size": 14, "src": "/root/.ansible/tmp/ansible-tmp-1462272045.11-211394086056695/source", "state": "file", "uid": 0 } ...
⑤file:設定文件屬性
path=:指明目標文件路徑,等同 dest 或 name
src=:被鏈接的源文件路徑,只應用於 state=link 的情況
force=:強制創建軟鏈接
state=:後面接文件的各種狀態,如directory, link, hard, file及absent(刪除)
directory: 如果目錄不存在,則創建目錄
file: 即使文件不存在,也不會被創建
absent: 刪除目錄、文件或鏈接文件
touch: 如果文件不存在,則會創建一個新文件,如果存在,則更新其時間戳
link: 創建軟鏈接
hard
其它選項:owner, group, mode, recurse
例:ansible websrvs -m file -a 'src=/tmp/test.ansible path=/tmp/test.link state=link'
ansible websrvs -m file -a 'owner=fedra group=fedra mode=644 path=/tmp/test.ansible'
⑥service:控制服務的運行狀態
enabled=:是否開機自動啓動,取值爲true或false
state=:狀態,取值有started, stopped, restarted
例:ansible websrvs -m service -a 'name=httpd state=started enabled=true'
[root@node3 ~]# service httpd status httpd is stopped
[root@node1 ~]# ansible 192.168.30.13 -m service -a 'name=httpd state=started' 192.168.30.13 | SUCCESS => { "changed": true, "name": "httpd", "state": "started" }
[root@node3 ~]# service httpd status httpd (pid 30552) is running...
⑦shell:
ansible websrvs -m shell -a 'echo $TERM'
[root@node1 ~]# ansible websrvs -m shell -a 'echo $TERM' 192.168.30.20 | SUCCESS | rc=0 >> xterm 192.168.30.13 | SUCCESS | rc=0 >> xterm
⑧script:將本地腳本複製到遠程主機並執行
例:ansible websrvs -m script -a '/root/adduser.sh'
⑨ping:測試指定主機是否能連接
[root@node1 ~]# ansible all -m ping 192.168.30.14 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.30.13 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.30.20 | SUCCESS => { "changed": false, "ping": "pong" }
⑩yum:管理程序包
name=:指明要安裝或卸載的程序包,可帶上版本號
state:present,latest表示安裝;absent表示卸載
例:ansible all -m yum -a 'name=zsh'
ansible all -m yum -a 'name=zsh state=absent'
[root@node1 ~]# ansible dbsrvs -m yum -a 'name=zsh state=latest' 192.168.30.14 | SUCCESS => { "changed": true, "msg": "", "rc": 0, "results": [ ...
⑾setup:收集遠程主機的facs
playbook運行時,會自動調用setup模塊收集遠程主機的相關信息(稱爲facts,如操作系統版本、ip地址、cpu數量等),這些信息保存於變量中,可在playbook中引用。
我們也可直接使用ansible命令直接獲取這些變量信息:
ansible all -m setup [-a 'filter=ansible_eth[0-2]']
filter:過濾器,表示只返回與指定shell風格通配符匹配的變量信息
ansible all -m setup --tree /tmp/facts
--tree:表示將收集的facs以樹狀的結構輸入到指定文件中
[root@node1 ~]# ansible websrvs -m setup -a 'filter=ansible_fqdn' 192.168.30.20 | SUCCESS => { "ansible_facts": { "ansible_fqdn": "node2" }, "changed": false } 192.168.30.13 | SUCCESS => { "ansible_facts": { "ansible_fqdn": "node3" }, "changed": false } [root@node1 ~]# ansible dbsrvs -m setup -a 'filter=ansible_processor*' 192.168.30.14 | SUCCESS => { "ansible_facts": { "ansible_processor": [ "GenuineIntel", "Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz", "GenuineIntel", "Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz" ], "ansible_processor_cores": 2, "ansible_processor_count": 1, "ansible_processor_threads_per_core": 1, "ansible_processor_vcpus": 2 }, "changed": false }
⑿template
template是使用了Jinjia2格式作爲文件模版,進行文檔內變量的替換的模塊。它的每次使用都會被ansible標記爲”changed”狀態。
舉個例子:
vim /root/httpd.conf
...
ServerName ` ansible_fqdn `
ansible websrvs -m template -a 'src=/root/httpd.conf desc=/etc/httpd/conf/httpd.conf
當/root/httpd.conf文件被複制到第一個主機時,ServerName的值被替換成第一個主機的ansible_fqdn的值node2,而被複制到第二個主機時,ServerName的值會被替換成node3
⒀synchronize
該模塊會調用rsync命令,用於將ansible機器的指定目錄推送到客戶機器 的指定目錄下
示例:ansible 192.168.30.20 -m synchronize -a 'src=/usr/local/src/ dest=/usr/local/src/ delete=yes compress=yes'
⒁get_url
常用模塊,可以實現在遠程主機上下載url到本地
示例:ansible 192.168.30.20 -m get_url -a 'url=http://ftp.linux.ncsu.edu/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm dest=/tmp'
⒂其它模塊:filesystem, group, hostname等