.ansible是新出現的自動化運維工具,基於Python開發,集合了衆多運維工具(puppet、cfengine、chef、func、fabric)的優勢,實現了批量系統配置、批量程序部署、批量運行命令等功能。ansible是基於模塊工做的,自己沒有批量部署的能力。真正具備批量部署的是ansible所運行的模塊,ansible只是提供一種框架。主要包括:
(1)、鏈接插件connection plugins:負責和被監控端實現通訊;
(2)、host inventory:指定操做的主機,是一個配置文件裏面定義監控的主機;
(3)、各類模塊核心模塊、command模塊、自定義模塊;
(4)、藉助於插件完成記錄日誌郵件等功能;
(5)、playbook:劇本執行多個任務時,非必需可讓節點一次性運行多個任務。python
1.部署簡單,只需在主控端部署Ansible環境,被控端無需作任何操做; 2.默認使用SSH協議對設備進行管理; 3.有大量常規運維操做模塊,可實現平常絕大部分操做; 4.配置簡單、功能強大、擴展性強; 5.支持API及自定義模塊,可經過Python輕鬆擴展; 6.經過Playbooks來定製強大的配置、狀態管理; 7.輕量級,無需在客戶端安裝agent,更新時,只需在操做機上進行一次更新便可; 8.冪等性,一個任務之行1遍或n遍效果同樣,不因重複執行出現狀況
Ansible:Ansible核心程序。 HostInventory:記錄由Ansible管理的主機信息,包括端口、密碼、ip等。 Playbooks:「劇本」YAML格式文件,多個任務定義在一個文件中,定義主機須要調用哪些模塊來完成的功能。 CoreModules:核心模塊,主要操做是經過調用核心模塊來完成管理任務。 CustomModules:自定義模塊,完成核心模塊沒法完成的功能,支持多種語言。 ConnectionPlugins:鏈接插件,Ansible和Host通訊使用
Ansible 系統由控制主機對被管節點的操做方式可分爲兩類,即adhoc和playbook:mysql
ad-hoc模式(點對點模式) 使用單個模塊,支持批量執行單條命令。ad-hoc 命令是一種能夠快速輸入的命令,並且不須要保存起來的命令。就至關於bash中的一句話shell。 playbook模式(劇本模式) 是Ansible主要管理方式,也是Ansible功能強大的關鍵所在。playbook經過多個task集合完成一類功能,如Web服務的安裝部署、數據庫服務器的批量備份等。能夠簡單地把playbook理解爲經過組合多條ad-hoc操做的配置文件。
簡單理解就是Ansible在運行時, 首先讀取ansible.cfg中的配置, 根據規則獲取Inventory中的管理主機列表, 並行的在這些主機中執行配置的任務, 最後等待執行返回的結果。web
1.加載本身的配置文件,默認/etc/ansible/ansible.cfg;
2.查找對應的主機配置文件,找到要執行的主機或者組;
3.加載本身對應的模塊文件,如 command;
4.經過ansible將模塊或命令生成對應的臨時py文件(python腳本), 並將該文件傳輸至遠程服務器;
5.對應執行用戶的家目錄的.ansible/tmp/XXX/XXX.PY文件;
6.給文件 +x 執行權限;
7.執行並返回結果;
8.刪除臨時py文件,sleep 0退出;sql
Ansible:基於ssh協議不須要代理,適合中小型應用場景
Saltstack:須要agent代理軟件(執行效率更高)
Puppet:ruby,功能強大,配置複雜,適合超大型環境shell
主控端端:192.168.136.167
被控端01:192.168.136.168
被控端02:192.168.136.185數據庫
#三臺主機都關閉防火牆 [root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0 #主控端安裝ansible yum install -y epel-release //安裝epel源 yum install ansible -y ansible --version //查看ansible版本 ansible 2.9.3 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] yum install tree -y tree /etc/ansible/ //樹狀結構展現文件夾 /etc/ansible/ ├── ansible.cfg #ansible的配置文件 ├── hosts #ansible的主倉庫,用於存儲須要管理的遠程主機的相關信息 └── roles # cd /etc/ansible vim hosts //配置主機清單 [webserver] 192.168.136.168 [mysql] 192.168.136.185 #推送公鑰 ssh-keygen -t rsa [root@localhost ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/idrsa): #回車 Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): #輸入密碼 Enter same passphrase again: Your identification has been saved in /root/.ssh/idrsa. Your public key has been saved in /root/.ssh/idrsa.pub. The key fingerprint is: SHA256:QnRuJjR10Jy6HuyQxQz3ccWML8iHCdQ1HZx5ba57Ak0 root@localhost.localdomain The key's randomart image is: +---[RSA 2048]----+ | +o==.ooBo+.| | o.+o. o.B +| | o=+= . + | | . += o E .| | .+S. . + . | | o.+ . o | | + . . . | | o o .| | o | +----[SHA256]-----+ #公鑰推給對方主機 ssh-copy-id root@192.168.136.168 ssh-copy-id root@192.168.136.185 //配置密鑰對驗證 #查看被控端兩臺主機的時間 [root@localhost ~]# ansible 192.168.136.168 -m command -a 'date' Enter passphrase for key '/root/.ssh/idrsa': 192.168.136.168 | CHANGED | rc=0 >> Sun Feb 9 09:02:44 CST 2020 [root@localhost ~]# ansible mysql -m command -a 'date' Enter passphrase for key '/root/.ssh/idrsa': 192.168.136.185 | CHANGED | rc=0 >> Sun Feb 9 09:03:11 CST 2020 #免交戶 [root@localhost ~]# ssh-agent bash #ssh代理 [root@localhost ~]# ssh-add #添加密碼 [root@localhost ~]# ansible webserver -m command -a 'date' 192.168.136.168 | CHANGED | rc=0 >> Sun Feb 9 09:05:08 CST 2020
命令格式:ansible [主機] [-m 模塊] [-a args] ansible-doc -l //列出全部已安裝的模塊 注:按q退出 ansible-doc -s yum //-s列出yum模塊描述信息和操做動做 #ansible默認模塊,all:表明全部主機(只要主機在線),-a+''號指定參數 [root@localhost ~]# ansible all -a 'date' 192.168.136.185 | CHANGED | rc=0 >> Sun Feb 9 09:16:22 CST 2020 192.168.136.168 | CHANGED | rc=0 >> Sun Feb 9 09:16:22 CST 2020 ansible 192.168.80.182 -m command -a 'date' //指定ip執行date ansible webserver -m command -a 'date' //指定分類執行date ansible mysql -m command -a 'date' ansible all -m command -a 'date' //全部hosts主機執行date命令 ansible all -a 'ls /' 若是不加-m模塊,則默認運行command模塊
兩種狀態(state):present表示添加(能夠省略),absent表示移除。 ansible-doc -s cron //查看cron模塊信息 #每分鐘執行一次,job:操做,echo輸出heihei,name:名稱 ansible webserver -m cron -a 'minute="/1" job="/bin/echo heihei" name="test cron job"' 192.168.136.168 | CHANGED => { "ansiblefacts": { "discoveredinterpreterpython": "/usr/bin/python" }, "changed": true, "envs": [], "jobs": [ "test cron job" ] } #查看週期性計劃性任務 [root@localhost ~]# ansible webserver -a 'crontab -l' 192.168.136.168 | CHANGED | rc=0 >> #Ansible: test cron job /1 /usr/bin/echo heihei ansible webserver -a 'crontab -l' ansible webserver -m cron -a 'name="test cron job" state=absent' //移除計劃任務,假如該計劃任務沒有取名字,name=None便可
user模塊是請求的是useradd, userdel, usermod三個指令 ansible-doc -s user ansible all -m user -a 'name="test01"' //建立用戶test01 192.168.136.185 | CHANGED => { "ansiblefacts": { "discoveredinterpreterpython": "/usr/bin/python" }, "changed": true, "comment": "", "createhome": true, "group": 1001, "home": "/home/test01", "name": "test01", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 } 192.168.136.168 | CHANGED => { "ansiblefacts": { "discoveredinterpreterpython": "/usr/bin/python" }, "changed": true, "comment": "", "createhome": true, "group": 1001, "home": "/home/test01", "name": "test01", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 } ansible mysql -m command -a 'tail /etc/passwd' ansible webserver -m user -a 'name="test01" state=absent' //刪除用戶test01
group模塊請求的是groupadd, groupdel, groupmod 三個指令。 ansible-doc -s group ansible mysql -m group -a 'name=mysql gid=306 system=yes' ansible mysql -a 'tail /etc/group' [root@localhost ~]# ansible mysql -a 'tail /etc/group' 192.168.136.185 | CHANGED | rc=0 >> slocate:x:21: postdrop:x:90: postfix:x:89: stapusr:x:156: stapsys:x:157: stapdev:x:158: tcpdump:x:72: chen:x:1000: mysql:x:306: test01:x:1001: ansible mysql -m user -a 'name=test02 uid=306 system=yes group=mysql' ansible mysql -a 'tail /etc/passwd' ansible mysql -a 'id test02' 192.168.136.185 | CHANGED | rc=0 >> uid=306(test02) gid=306(mysql) groups=306(mysql)
ansible-doc -s copy #src原,dest目標,owner:指定文件權限 ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640' ansible mysql -a 'ls -l /opt' 192.168.136.185 | CHANGED | rc=0 >> total 4 -rw-r-----. 1 root root 541 Feb 9 09:44 fstab.back drwxr-xr-x. 2 root root 6 Mar 26 2015 rh ansible mysql -a 'cat /opt/fstab.back' #contest:指定內容,生成一個新文件 ansible mysql -m copy -a 'content="hello heihei!" dest=/opt/fstab.back' //將hello heihei!寫入/opt/fstab.back ansible mysql -a 'cat /opt/fstab.back' 192.168.136.185 | CHANGED | rc=0 >> hello heihei!
ansible-doc -s file ansible mysql -m user -a 'name=mysql system=yes' ansible mysql -m group -a 'name=mysql system=yes' #path:指定文件路徑 ansible mysql -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back' //修改文件的屬主屬組權限等 ansible mysql -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link' //設置/opt/fstab.link爲/opt/fstab.back的連接文件 ansible mysql -m file -a "path=/opt/fstab.back state=absent" //刪除一個文件 ansible mysql -m file -a "path=/opt/test state=touch" 建立一個文件 -----ping模塊------- ansible all -m ping 192.168.136.185 | SUCCESS => { "ansiblefacts": { "discoveredinterpreterpython": "/usr/bin/python" }, "changed": false, "ping": "pong" } 192.168.136.168 | SUCCESS => { "ansiblefacts": { "discoveredinterpreterpython": "/usr/bin/python" }, "changed": false, "ping": "pong" }
ansible-doc -s service [root@ab ~]# yum install -y httpd [root@aa ~]# ansible webserver -a 'systemctl status httpd' //查看web服務器httpd運行狀態 ansible webserver -m service -a 'enabled=true name=httpd state=started' #關閉用stop 192.168.136.185 | CHANGED => { "ansiblefacts": { "discoveredinterpreterpython": "/usr/bin/python" }, "changed": true, "enabled": true, "name": "httpd", "state": "started", "status": { "ActiveEnterTimestampMonotonic": "0", "ActiveExitTimestampMonotonic": "0", "ActiveState": "inactive", //啓動httpd服務 [root@ab ~]# systemctl status httpd //查看是否開啓 ------shell模塊----- ansible-doc -s shell [root@localhost ~]# ansible webserver -m shell -a 'echo abc123|passwd --stdin chen' 192.168.136.168 | CHANGED | rc=0 >> Changing password for user chen. passwd: all authentication tokens updated successfully. //建立用戶使用無交互模式給用戶設置密碼
#本地建立腳本讓其餘全部被控端主機一塊兒執行這個腳本 ansible-doc -s script vi test.sh #!/bin/bash echo "hello ansible from script"> /opt/script.txt chmod +x test.sh ansible mysql -m script -a 'test.sh' [root@localhost ~]# ansible mysql -a 'cat /opt/script.txt' 192.168.136.185 | CHANGED | rc=0 >> hello ansible from script
ansible-doc -s yum ansible mysql -m yum -a 'name=httpd' //yum安裝httpd 192.168.136.185 | CHANGED => { "ansiblefacts": { "discoveredinterpreterpython": "/usr/bin/python" }, "changed": true, "changes": { "installed": [ "httpd" ] }, "msg": "", "rc": 0, "results": [ [root@ac ~]# rpm -q httpd ansible mysql -m yum -a 'name=httpd state=absent' //卸載zsh [root@ac ~]# rpm -q httpd
ansible-doc -s setup ansible mysql -m setup //獲取mysql組主機的facts信息 ***