Ansible是一款爲類Unix系統開發的自由開源的配置和自動化工具。它用Python寫成,相似於Chef和Puppet,可是有一個不一樣和優勢是咱們不須要在節點中安裝任何客戶端。它使用SSH來和節點進行通訊。html
以前也在網上找了一些RHEL6的配置文檔,基本都是編譯安裝有點麻煩,還得注意python版本小於3.x,若是用RHEL7\CENTOS7的話只要先安裝好epel7的源,下面都是自動依賴安裝的超方便,因此強烈推薦用RHEL7\CENTOS7安裝。node
若是硬要編譯安裝可訪問主頁:https://github.com/ansible/ansiblepython
playbook雖然很實用但目前用的很少,故不作詳細記錄,參考:http://www.361way.com/ansible-playbook-example/4441.htmllinux
root用戶沒法登錄,普通用戶登陸後su過去遇到的問題,參考:http://www.361way.com/ansible-su/4882.html git
本篇中咱們將在RHEL 7上安裝並配置Ansible,而且嘗試管理兩個節點github
服務端:192.168.180.99web
客戶端:192.168.180.97\98正則表達式
1、設置EPEL倉庫shell
Ansible倉庫默認不在yum倉庫中,所以咱們須要使用下面的命令啓用epel倉庫。vim
rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm
2、yum安裝ansible
yum install ansible ansible --version #沒錯的話就是最新版的2.2
3、設置ssh鑑權
其實很簡單就是ansible服務端生成rsa的key給到客戶端,這樣服務端就能夠不用輸入密碼直接登陸客戶端了,是否是很方便!
#默認不設置加密密碼,目錄~/.ssh/id_rsa.pub ssh-keygen -i rsa -P '' #該命令會自動給服務端key添加到客戶端的~/.ssh/authorized_keys中 ssh-copy-id -id root@192.168.180.97\97
4、ansible節點清單
一、給服務端ansible定義節點清單
vim /etc/ansible/hosts [test] #這裏用域名、主機名、IP均可以 192.168.180.97 192.168.180.98 [other] #起個別名 jumper ansible_ssh_port = 5555 ansible_ssh_host = 192.168.1.50 #定了從web1到web50,webservers組共計50臺主機 www[01:50].361way.com #databases組有db-a到db-f共6臺主機 db-[a:f].91it.org
二、使用主機變量
如下是Hosts部分中常常用到的變量部分
ansible_ssh_host # 要鏈接的主機名 ansible_ssh_port # 端口號默認是22 ansible_ssh_user # ssh鏈接時默認使用的用戶名 ansible_ssh_pass # ssh鏈接時的密碼 ansible_sudo_pass # 使用sudo鏈接用戶是的密碼 ansible_ssh_private_key_file # 祕鑰文件若是不想使用ssh-agent管理時可使用此選項 ansible_shell_type # shell的類型默認sh ansible_connection # SSH 鏈接的類型: local , ssh , paramiko在 ansible 1.2 以前默認是 paramiko ,後來智能選擇,優先使用基於 ControlPersist 的 ssh (支持的前提) ansible_python _ interpreter #用來指定 python 解釋器的路徑,一樣能夠指定ruby 、perl 的路徑
示例以下:
[test1] 10.212.52.252 ansible_ssh_user=root ansible_ssh_pass='361way.com' 10.212.52.14 ansible_ssh_user=test1 ansible_ssh_pass='91it.org' 10.212.52.16 ansible_ssh_user=test2 ansible_ssh_port=7788 ansible_ssh_pass='123456'
三、組內變量
變量也能夠經過組名,應用到組內的全部成員:
[test] host1 host2 [test:vars] ntp_server=ntp.aaa.com proxy=proxy.bbb.com
上面test組中包含兩臺主機,經過對test組指定vars變動,相應的host1和host2至關於相應的指定了ntp_server和proxy變量參數值 。
[hangzhou] host1 host2 [jiaxing] host2 host3 [zhejiang:children] hangzhou jiaxing [zhejiang:vars] some_server=foo.southeast.example.com halon_system_timeout=30 self_destruct_countdown=60 escape_pods=2 [china:children] zhejiang henan shandong hebei
如上面的示例中,我指定了杭州組我有host一、hosts2;嘉興組我有host三、host4主機;我又指定了一個組浙江組,同時包含杭州和嘉興;同時爲該組內的全部主機指定了四個vars變量。後面我又設定了一個組中國組,包含浙江、河南、山東、河北。
注:因爲vars變量在ansible ad-hoc部分中基本用不到,主要用在ansible-playbook中,後面的章節部分也會提到。
5、ansible經常使用功能
更多更詳細的話參考:http://www.361way.com/ansible-modules/4415.html
一、ping模塊:看到ping字段爲pong即爲通,該功能主要用於批量檢測連通性
ansible -m ping test
二、執行shell命令的command默認模塊,用於遠程執行命令,不支持變量,例子:
#查看當前用戶 ansible -m command -a 'w' test #查看日期 ansible -m command -a 'date' test #給節點增長用戶 ansible -m command -a 'useradd mark' test ansible -m command -a 'grep mark /etc/passwd' test #重定向輸出到文件 ansible -m command -a 'df -Th' test > /root/test.log
三、cron定時任務,例子:
ansible test -m cron -a 'minute="*/1" job="/bin/echo "hell"" name="test job" ' #查看是否成功,另外注意:這裏既然生成了,就當即生效,不用重啓crond進程 ansible test -m command -a 'crontab -l'
四、user管理,例子:
ansible test -m user -a 'name=mark' #查看是否成功,如今不明白和command useradd添加有什麼區別,可是既然有用戶管理仍是用它好了 ansible -m command -a 'grep mark /etc/passwd' test
五、遠程複製模塊copy,例子:
#若是要想遠程複製文件成功,必須給全部客戶端安裝這個。。。不知道爲何,若是不裝複製失敗報錯 ansible -m command -a 'yum install libselinux-python -y' 'test ansible -m copy -a 'src=/root/VMwareTools-9.4.10-2068191.tar.gz dest=/root owner=root mode=640' 'test' #往遠程主機文件寫入內容:使用content代替src ansible -m copy -a 'content="aaabbb\n" dest=/root/test.log' test
六、設置文件屬性模塊file,例子:
#固然group\mode\owner不寫也行 ansible test -m file -a 'owner=mark group=root mode=644 path=/root/test.log' #建立文件符號連接 ansible test -m file -a 'path=/root/test.1 src=/root/test.log state=link'
七、service指定服務狀態模塊管理(須保證遠程主機已安裝好該服務),例子:
#enableed:是否開機自動啓動 true/false #name:服務名稱 #start:狀態,取值爲started,stoped,restarted ansible test -m service -a 'enabled=true name=httpd state=started'
八、shell模塊: 在遠程主機上運行命令,支持管道、變量等在使用複製命令時使用,例子:
#該功能好用,能夠批量更改客戶端用戶密碼 ansible test -m shell -a 'echo password |passwd –stdin mark'
九、script將本地腳步複製到遠程主機上並運行(注意本地主機腳步路徑使用相對路勁),例子:
ansible test -m script -a "test.sh"
十、yum程序安裝,例子
#name指定要安裝的程序包,能夠帶上版本號 #state:preset,laster表示安裝最新,absent卸載 ansible test -m yum -a 'name=lrzsz'
十一、setup收集遠程主機的facts,例子:
#查看全部 ansible test -m setup #查看IP配置 ansible test -m setup -a 'filter=ansible_default_ipv4' #查看內存信息 ansible test -m setup -a 'filter=ansible_memory_mb' #查看主機名 ansible test -m setup -a 'filter=ansible_nodename'
每一個被管理節點在接受並運行管理命令以前,會將本身主機相關信息,操做系統版本、ip地址等會報告給ansible主機用於將狀態報告給ansible主機直接調用其變量。
十二、其餘
#查看模塊幫助命令 ansible-doc -l #查看支持模塊 ansible-doc -s Modules
6、Patterns(主機與組正則匹配部分)
把Patterns 直接理解爲正則實際是不徹底準確的,正常的理解爲patterns意味着在ansible中管理哪些主機,也能夠理解爲,要與哪臺主機進行通訊。在探討這個問題以前咱們先看下ansible的用法:
ansible <pattern_goes_here> -m <module_name> -a <arguments>
直接上一個示例:
ansible webservers -m service -a "name=httpd state=restarted"
這裏是對webservers 組或主機重啓httpd服務 ,其中webservers 就是Pattern部分。而之因此上面我說Pattern(模式)能夠理解爲正則,主要針對下面常常用到的用法而言的。
利用通配符還能夠指定一組具備規則特徵的主機或主機名,冒號表示or---邏輯或
one.361way.com one.361way:two.361way.com 192.168.1.50 192.168.1.*
固然,這裏的*通配符也能夠用在前面,如:
*.361way.com *.com
上面的用法,在多個組之間一樣適用 ,如:
webservers webservers:dbservers #表示兩個組中全部的主機
固然你能夠作出非的表達式,例如,目標主機必須在組webservers但不在phoenix組中
webserver:!phoenix
你還能夠作出交集的表達式,例如,目標主機必須即在組webservers中又在組staging中
webservers:&staging
一個更復雜的示例:
webserver:dbservers:&staging:!phoenix
上面這個複雜的表達式最後表示的目標主機必須知足:在webservers或者dbservers組中,必須還存在於staging組中,可是不在phoenix組中。這些能夠看做是SaltStack中Compound matchers 。
*.361way.com:*.org
還能夠在開頭的地方使用」~」,用來表示這是一個正則表達式:
~(web|db).*\.91it\.org
到這裏估計你應該用能明白爲何前面我會提到Patterns 能夠理解爲正則的緣由了。最後部分給兩個ansible-playbook中具體可能用的用法:
a、在ansible-palybook命令中,你也可使用變量來組成這樣的表達式,可是你必須使用「-e」的選項來指定這個表達式(一般咱們不這樣用):
ansible-palybook -e webservers:!`excluded`:&`required`
b、在ansible和ansible-playbook中,還能夠經過一個參數」–limit」來明確指定排除某些主機或組:
ansible-playbook site.yml --limit datacenter2