1、Ansible簡介node
Ansible 是一個簡單的自動化運維管理工具,能夠用來自動化部署應用、配置、編排 task(持續 交付、無宕機更新等),採用 paramiko 協議庫),經過SSH或者ZeroMQ等鏈接主機。python
Ansible 在管理節點將 Ansible 模塊經過 SSH 協議(或者 Kerberos、LDAP)推送到被管理端執 行,執行完以後自動刪除,可使用 SVN 等來管理自定義模塊及編排mysql
Ansible由5 個部分組成:web
Ansible:核心
sql
Modules:包括 Ansible 自帶的核心模塊及自定義模塊 shell
Plugins:完成模塊功能的補充,包括鏈接插件、郵件插件等 vim
Playbooks:劇本,定義 Ansible 多任務配置文件,由Ansible自動執行centos
Inventory:定義 Ansible 管理主機的清單
安全
Ansible的特性:ruby
Ansible 無需安裝服務端和客戶端,只要 SSH 便可。這意味着,任何一臺裝有 Ansible 的機器均可以成爲強大的管理端
模塊化,調用特定的模塊,完成特定的任務
基於Python語言實現,由Paramiko、PyYAML和Jinja2三個關鍵模塊;
部署簡單,輕量級,無需在客戶端安裝agent,更新時,只需在操做機上進行一次更新便可
主從模式,支持自定義模塊
支持Playbook,冪等性,一個playbook屢次執行結果相同
2、Ansible的安裝和配置前準備
經過yum安裝Ansible,須要配置epel源。ansible依賴於Python 2.6或更高的版本、paramiko、PyYAML及Jinja2。
[root@c7node1 ~]# yum install ansible # RHEL/CentOS,須要配置 EPEL
ansible經過ssh實現配置管理、應用部署、任務執行等功能,所以,須要事先配置ansible端能基於密鑰認證的方式聯繫各被管理節點。
試驗使用的是一臺centos 7,兩臺cento 6,分別配置主機名爲下列:
c7node1.wlw.com IP:172.16.88.11
C6node1.wlw.com IP:172.16.88.22
C6node2.wlw.com IP:172.16.88.33
修改方法爲:
[root@c7node1 ~]# hostnamectl set-hostname C7node1.wlw.com [root@C6node1 ~]# vim /etc/sysconfig/network HOSTNAME=C6node1.wlw.com [root@C6node2 ~]# vim /etc/sysconfig/network HOSTNAME=C6node2.wlw.com
分別爲全部主機密鑰受權:
[root@c7node1 ~]# ssh-keygen -t rsa [root@c7node1 .ssh]# pwd /root/.ssh [root@c7node1 .ssh]# ls id_rsa id_rsa.pub
#生成密鑰和公鑰
[root@c7node1 .ssh]# cd ~/.ssh [root@c7node1 .ssh]# ssh-copy-id -i id_rsa.pub root@172.16.88.11 [root@c7node1 .ssh]# ssh-copy-id -i id_rsa.pub root@172.16.88.22 [root@c7node1 .ssh]# ssh-copy-id -i id_rsa.pub root@172.16.88.33 #把服務器中的公鑰分配複製到對應主機~/.ssh/authorized_keys文件中,這樣就能實現密鑰受權,無需密碼既能夠登錄
註釋:
1.在首次鏈接或者重裝系統以後會出現檢查 keys 的提示
The authenticity of host '192.168.0.5 (192.168.0.5)' can't be established. ECDSA key fingerprint is 05:51:e5:c4:d4:66:9b:af:5b:c9:ba:e9:e6:a4:2b:fe. Are you sure you want to continue connecting (yes/no)?
解決辦法:
vim /etc/ansible/ansible.cfg 或者 ~/.ansible.cfg
[defaults]
host_key_checking = False 也能夠經過設置系統環境變量來禁止這樣的提示
export ANSIBLE_HOST_KEY_CHECKING=False
2.在使用 paramiko 模式時,主機 keys 的檢查會很慢
3.默認狀況下 Ansible 會記錄一些模塊的參數等信息到每一個被控端的 syslog 日誌文件裏,除非
在任務或者劇本里設置了 no_log: True 會不記錄日誌
3、Ansible命令參數介紹
ansible <host-pattern(all,*:表明全部主機)> [-f forks] [-m module_name(默認:command) ] [-a arg(key=value )]
-m :要執行的模塊,默認爲command
-a :模塊的參數,通常爲key=value
-C :只是測試一下會改變什麼內容,不會真正去執行
-f :fork多少個進程併發處理,默認5
-s :sudo運行
-U :udo到哪一個用戶,默認爲root
--list-host:只打印有哪些主機會執行這個 playbook 文件,不是實際執行該 playbook
--limit:指定運行時排除的主機
-v :verbose mode (-vvv for more, -vvvv to enable connection debu gging)
ansible-doc: Show Ansible module documentation
-l, --list 列出全部可用模塊
-s, --snippet 查看模塊的詳細信息
4、Ansible主機清單Inventory
Ansible 經過讀取默認的主機清單配置/etc/ansible/hosts,能夠同時鏈接到多個遠程主機上執行任務, 默認路徑能夠經過修改 ansible.cfg 的 hostfile 參數指定路徑。[ ]表示主機的分組名,能夠按照功能、系統等進行分類,便於對某些主機或者某一組功能相同的主機進行操做
[root@c7node1 ansible]# vim /etc/ansible/hosts [targets] localhost ansible_connection=local wlw.com ansible_connection=ssh ansible_ssh_user=root ansible_ssh_prot=234 #指定本地主機經過local本地鏈接,指定wlw.com主機經過ssh且用戶爲root鏈接,且端口ssh端口爲234 wlw ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50 #設置主機別名爲wlw,這裏的設置在執行返回結果的時候不會顯示ip地址,會顯示定義的別名,別名也能夠給其餘組引用 www[01:50].wlw.com db-[a:g].wlw.com #支持通配符匹配,會替換程www01 ~ www50 , db-a ~ db-g [atlanta] host1 http_port=80 maxRequestsPerChild=800 host1 http_port=80 maxRequestsPerChild=800 #爲每一個主機單獨指定一些變量,這些變量隨後能夠在 playbooks 中使用 [atlanta:vars] server=http #爲一個組指定變量,組內每一個主機均可以使用該變量 [group1] host1 [group2] host2 [group:children] group1 group2 #這裏是group1組包含了定義爲別名的主機host1,而後group包含了group1和group2兩個組的全部主機 [web] 172.16.88.11 172.16.88.22 [db] 172.16.88.22 172.16.88.33
ansible基於ssh鏈接inventory中指定的遠程主機時,還能夠經過參數指定其交互方式參數:
ansible_ssh_host:指定主機別名對應的真實 IP,如:251 ansible_ssh_host=183.60.41.251,
隨後鏈接該主機無須指定完整 IP,只需指定 251 就行
ansible_ssh_port:指定鏈接到這個主機的 ssh 端口,默認 22
ansible_ssh_user:鏈接到該主機的 ssh 用戶
ansible_ssh_pass:鏈接到該主機的 ssh 密碼(連-k 選項都省了),安全考慮仍是建議使用私鑰
或在命令行指定-k 選項輸入
ansible_sudo_pass:sudo 密碼
ansible_sudo_exe(v1.8+的新特性):sudo 命令路徑
ansible_connection:鏈接類型,能夠是 local、ssh 或 paramiko,ansible1.2 以前默認爲 param
iko
ansible_ssh_private_key_file:私鑰文件路徑
ansible_shell_type:目標系統的 shell 類型,默認爲 sh,若是設置 csh/fish,那麼命令須要遵循
它們語法
ansible_python_interpreter:python 解釋器路徑,默認是/usr/bin/python,可是如要要連*BSD
系統的話,就須要該指令修改 python 路徑
ansible_*_interpreter:這裏的"*"能夠是 ruby 或 perl 或其餘語言的解釋器,做用和 ansible_p
ython_interpreter 相似
5、Ansible經常使用模塊及其經常使用選項
ping:測試模塊,能夠測試主機的狀態,不用帶參數
[root@c7node1 ansible]# ansible all -m ping 172.16.88.22 | success >> { "changed": false, "ping": "pong" } 172.16.88.11 | success >> { "changed": false, "ping": "pong" } 172.16.88.33 | success >> { "changed": false, "ping": "pong" }
command:執行命令,這個模塊並不支持 shell 變量和管道等,若想使用 shell 來執行模塊,請使用-m 參數指定 shell 模塊
[root@c7node1 ansible]# ansible all -m command -a 'hostname' 172.16.88.11 | success | rc=0 >> c7node1.wlw.com 172.16.88.33 | success | rc=0 >> C6node2.wlw.com 172.16.88.22 | success | rc=0 >> C6node1.wlw.com # command模塊要執行命令無須爲key=value格式,而是直接給出要執行的命令便可,上面命令等同於nsible all -a 'hostname'
shell:切換到某個shell執行指定的指令,參數與command相同
[root@c7node1 tmp]# ansible web -m shell -a "echo 'shell moudel' | tee /tmp/shell" 172.16.88.33 | success | rc=0 >> shell moudel 172.16.88.11 | success | rc=0 >> shell moudel [root@c7node1 tmp]# cat /tmp/shell shell moudel
user:系統用戶管理
-a "name='用戶名' state={present|absent}(present建立,absent刪除) system={yes|no}'建立爲系統用戶' uid='指定uid' shell='指定shell' home='指定家目錄'"
[root@c7node1 ansible]# ansible web -m user -a 'name=test uid=299 home=/home/testhome shell=/bin/csh system=yes state=present' 172.16.88.11 | success >> { "changed": true, "comment": "", "createhome": true, "group": 299, "home": "/home/testhome", "name": "test", "shell": "/bin/csh", "state": "present", "system": true, "uid": 299 } [root@c7node1 ansible]# tail -1 /etc/passwd test:x:299:299::/home/testhome:/bin/csh
group:系統組管理
-a "name='組名' state={present|absent}(present建立,absent刪除) system={yes|no}(建立爲系統用戶) gid='指定gid'
[root@c7node1 home]# ansible web -m group a 'name=testgroup state=present system=yes' 172.16.88.11 | success >> { "changed": true, "gid": 997, "name": "testgroup", "state": "present", "system": true } [root@c7node1 home]# tail -1 /etc/group testgroup:x:997:
cron:制定計劃任務
-a "name='計劃任務名' minute='指定分鐘' job='執行的任務' user='執行的用戶' state={present|absent}(present建立,absent刪除)"
[root@c7node1 ~]# ansible web -m cron -a "name='web server back' minute=*/2 job='date >/tmp/data.ts' user=root state=present" 172.16.88.11 | success >> { "changed": true, "jobs": [ "web server back" ] } [root@c7node1 ~]# crontab -l #Ansible: web server back */2 * * * * date >/tmp/data.ts [root@c7node1 tmp]# cat data.ts Tue Oct 20 04:28:01 EDT 2015
copy:複製文件到遠程主機
-a "backup={yes|no}(覆蓋前備份) dest='目標位置' src='源位置' mode='指定權限' owner='指定屬主' group='指定屬組'"
[root@c7node1 tmp]# touch /tmp/Centos7file [root@c7node1 tmp]# ansible all -m copy -a 'src=/tmp/Centos7file dest=/tmp/ mode=777 owner=mail group=mail' 172.16.88.11 | success >> { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/tmp/Centos7file", "gid": 12, "group": "mail", "mode": "0777", "owner": "mail", "path": "/tmp/Centos7file", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 8 } [root@C6node2 ~]# ll /tmp/Centos7file -rwxrwxrwx. 1 mail mail 0 10月 20 16:37 /tmp/Centos7file
file:設置文件屬性,若是對應文件或目錄不存在則建立
-a "path='指定文件,絕對路徑' mode='指定權限' owner='指定屬主' group='指定屬組' state={directory|link|hard|present|absent}"
state:
directory:若是目錄不存在,就建立目錄
file:即便文件不存在,也不會被建立
link:建立軟連接,須要額外指定,src:被連接的源文件路徑,dest:被連接到的路徑
hard:建立硬連接
touch:若是文件不存在,則會建立一個新的文件,若是文件或目錄已存在,則更新其最後修改時間
absent:刪除目錄、文件或者取消連接文件
[root@c7node1 ~]# ansible web -m file -a "dest=/tmp/test mode=600 owner=root group=root state=directory" 192.168.0.11 | success >> { "changed": true, "gid": 0, "group": "root", "mode": "0600", "owner": "root", "path": "/tmp/test", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 6, "state": "directory", "uid": 0 } [root@c7node1 ~]# ll -d /tmp/test drw-------. 2 root root 6 Oct 20 08:10 /tmp/test
yum
-a "name='服務' state={present|latest|absent}(present:安裝服務,但不更新,latest:安裝最新服務,absent:卸載服務)"
[root@c7node1 tmp]# ansible db -m yum -a "name='mysql-server' state=latest" 172.16.88.33 | success >> { "changed": true, "msg": "", "rc": 0, "results": [ 安裝詳細信息 ] } [root@C6node2 mnt]# rpm -q mysql-server mysql-server-5.1.73-3.el6_5.x86_64
service
-a "name='服務' state={started|stopped|restarted}"
[root@c7node1 tmp]# systemctl status httpd httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: inactive (dead) [root@C6node2 mnt]# /etc/init.d/httpd status httpd 已停 root@c7node1 tmp]# ansible web -m service -a 'name=httpd state=started' 172.16.88.33 | success >> { "changed": true, "name": "httpd", "state": "started" } 172.16.88.11 | success >> { "changed": true, "name": "httpd", "state": "started" [root@c7node1 tmp]# systemctl status httpd httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: active (running) since 二 2015-10-20 05:03:32 EDT; 2min 12s ago Main PID: 4193 (httpd) [root@C6node2 mnt]# /etc/init.d/httpd status httpd (pid 2770) 正在運行... #這裏能夠看出管理服務能夠同時對Centos6和Centos7執行
script:運行腳本,腳本能夠沒有執行權限
-a '/path/to/script'
setup:收集主機信息
[root@c7node1 tmp]# ansible web -m setup #蒐集主機的全部系統信息 [root@c7node1 tmp]# ansible all -m setup --tree /tmp/facts #蒐集系統信息並以主機名爲文件名分別保存在/tmp/facts 目錄 [root@c7node1 tmp]# ansible all -m setup -a 'filter=ansible_*_mb' #蒐集和內存相關的信息 [root@c7node1 tmp]# ansible all -m setup -a 'filter=ansible_eth[0-2]' #蒐集網卡信息