ansible安裝以及配置認證python
特性
(1)、no agents:不須要在被管控主機上安裝任何客戶端;
(2)、no server:無服務器端,使用時直接運行命令便可;
(3)、modules in any languages:基於模塊工做,可以使用任意語言開發模塊;
(4)、yaml,not code:使用yaml語言定製劇本playbook;
(5)、ssh by default:基於SSH工做;
(6)、strong multi-tier solution:可實現多級指揮。linux
優勢
(1)、輕量級,無需在客戶端安裝agent,更新時,只需在操做機上進行一次更新便可;
(2)、批量任務執行能夠寫成腳本,並且不用分發到遠程就能夠執行;
(3)、使用python編寫,維護更簡單,ruby語法過於複雜;
(4)、支持sudo。web
兩臺機器192.168.0.188和192.168.0.189
shell
只須要在192.168.0.188上安裝ansible便可vim
yum install -y epel-releasecentos
yum install -y ansibleruby
188上生成密鑰對bash
ssh-keygen -t rsa 直接回車便可,不用設置密鑰密碼
這樣會在root家目錄下生成.ssh目錄,這裏面也會生成兩個文件 id_rsa 和 id_rsa.pub
而後把公鑰(id_rsa.pub)內容放到對方機器(189上)的/root/.ssh/authorized_keys裏面服務器
scp .ssh/id_rsa.pub 192.168.0.189://root/.ssh/authorized_keysssh
本機也要操做
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
對方機器上要配置好 authorized_keys文件的權限
chmod 600 /root/.ssh/authorized_keys
還須要關閉selinux
setenforce 0
ssh web10.xuan.com若是沒有ssh就要yum install -y openssh-clients這個包
(2) ansible 配置
vi /etc/ansible/hosts //增長
[testhost]
127.0.0.1
172.7.15.111
說明: testhost爲主機組名字,自定義的。 下面兩個ip爲組內的機器ip。
ansible遠程執行命令
188上:
vi /etc/ansible/hosts //增長
[testhost] (組名,下面能夠寫具體的IP,也能夠寫主機名,要麼有DNS去解析它,要麼去定義一個)
127.0.0.1
192.168.0.189
說明: testhost爲主機組名字,自定義的。 下面兩個ip爲組內的機器ip。
ansible testhost -m command -a 'w' (-m後面跟模塊的名字,-a後面跟模塊具體的值,這裏是具體的命令)
這樣就能夠批量執行命令了。這裏的testhost 爲主機組名,固然咱們也能夠直接寫一個ip,針對某一臺機器來執行命令。
ansible 127.0.0.1 -m command -a 'hostname'
錯誤: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
解決: yum install -y libselinux-python
還有一個模塊就是shell一樣也能夠實現
ansible testhost -m shell -a 'w'
ansible testhost -m shell -a 'cat /etc/passwd|grep root'
ansible testhost -m command -a 'cat /etc/passwd|grep root'
shell能實現的功能command不必定實現,而command實現的功能shell必定能實現,如shell支持管道輸出,而command不支持管道。
ansible拷貝目錄或者文件
188上:
ansible testhost -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0644"
注意:源目錄會放到目標目錄下面去,若是目標指定的目錄不存在,它會自動建立。若是拷貝的是文件,dest指定的名字和源若是不一樣,而且不是已經存在的目錄,至關於拷貝過去後又重命名。但相反,若是desc是目標機器上已經存在的目錄,則會直接把文件拷貝到該目錄下面。
ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/123"
這裏的/tmp/123和源機器上的/etc/passwd是一致的,但若是目標機器上已經有/tmp/123目錄,則會再/tmp/123目錄下面創建passwd文件
ansible遠程執行腳本
188上:
首先建立一個shell腳本
vim /tmp/test.sh //加入內容
#!/bin/bash
echo `date` > /tmp/ansible_test.txt
而後把該腳本分發到各個機器上
ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mod=0755"
最後是批量執行該shell腳本
ansible testhost -m shell -a "/tmp/test.sh"
shell模塊,還支持遠程執行命令而且帶管道
ansible testhost -m shell -a "cat /etc/passwd|wc -l "
ansible實現任務計劃
188上:
ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt' weekday=6"
若要刪除該cron 只須要加一個字段 state=absent
ansible testhost -m cron -a "name='test cron' state=absent"
其餘的時間表示:分鐘minute 小時hour 日期day 月份month
ansible安裝rpm包管理服務
188上:
ansible testhost -m yum -a "name=httpd" (在name後面還能夠加上state=installed,若是httpd已經安裝過了,就安裝name=vim-enhanced)ansible testhost -m service -a "name=httpd state=started enabled=yes" (這裏的name是centos系統裏的服務名,能夠經過chkconfig --list查到。)文檔使用:ansible-doc -l 列出全部的模塊ansible-doc cron 查看指定模塊的文檔