自動化運維工具Ansible實戰(二)主機清單的管理

        Ansible 經過讀取默認的主機清單配置/etc/ansible/hosts,來同時鏈接到多個遠程主機,來執行遠程操做任務的,可是若是要修改默認路徑能夠經過修改主配置文件 ansible.cfg 的 hostfile 參數指定相應的路徑。具體查看相應的路徑爲:php

[root@Monitor ansible]# vim ansible.cfg 
remote_port    = 22
remote_user = root
private_key_file = ~/.ssh/id_rsa_web
host_key_checking = False
hostfile = /etc/ansible/conf/hosts

接下來會詳細講解主機和組,以及他們的正則表達式的匹配:
html

(一)主機和組(Hosts and Groups)java

經過配置/etc/ansible/hosts這個文件來定義主機和組python

(1)簡單的主機和組nginx

[root@Monitor ansible]# vim /etc/ansible/conf/hosts
[webserver]                             ###定義組名
#192.168.180.4                    ###定義主機
192.168.180.5
192.168.180.6
192.168.180.23
[dbserver]
192.168.180.2
[root@Monitor ansible]# ansible webserver -m command -a 'uptime'
192.168.180.23 | SUCCESS | rc=0 >>
 15:00:30 up 45 days, 23:43,  4 users,  load average: 0.00, 0.01, 0.05

192.168.180.5 | SUCCESS | rc=0 >>
 15:01:10 up 45 days, 23:44,  2 users,  load average: 0.00, 0.00, 0.00

192.168.180.6 | SUCCESS | rc=0 >>
 15:01:27 up 45 days, 23:44,  2 users,  load average: 2.00, 2.00, 2.00

注意的要點有:web

a.中括號中的名字表明組名,能夠根據本身的需求將龐大的主機分紅具備標識的組,如上面分了兩個組webservers和dbservers組;正則表達式

b.主機(hosts)部分可使用域名、主機名、IP地址表示;固然使用前二者時,也須要主機能反解析到相應的IP地址,通常此類配置中多使用IP地址;shell

(2)端口與別名。vim

SSH默認的端口是22(此時的ansible主機配置文件能夠省略),可是若是某些主機的SSH運行在自定義的端口上,ansible使用Paramiko進行ssh鏈接時不會使用你SSH配置文件中列出的端口,可是若是修改ansible使用openssh進行ssh鏈接時將會使用:ruby

######在client192.168.180.5上ssh開啓了2個端口鏈接
[root@localhost ~]# vim /etc/ssh/sshd_config 
#       $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $
# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options change a
# default value.
Port 10022
Port 22
######在ansible服務端的配置:
[root@Monitor ansible]# vim /etc/ansible/conf/hosts            
[webserver]
#192.168.180.4
192.168.180.5:10022
192.168.180.6
192.168.180.23
[root@Monitor ansible]# ansible webserver -m command -a 'uptime'  
192.168.180.23 | SUCCESS | rc=0 >>
 16:12:33 up 46 days, 55 min,  4 users,  load average: 0.00, 0.01, 0.05

192.168.180.5 | SUCCESS | rc=0 >>
 16:14:58 up 22 min,  3 users,  load average: 0.00, 0.00, 0.00

192.168.180.6 | SUCCESS | rc=0 >>
 16:13:43 up 46 days, 56 min,  2 users,  load average: 2.00, 2.00, 2.00

(3)指定主機範圍

hosts官方有個列子是經過指定主機名的範圍來進行多臺主機的定義

[root@Monitor ansible]# vim /etc/ansible/conf/hosts   
   [webservers]
    www[01:50].yanruogu.com
    [databases]
    db-[a:f].yanruogu.com

上面指定了從web1到web50,webservers組共計50臺主機;databases組有db-a到db-f共6臺主機。

(4)使用主機變量

hosts主機常常使用到的變量爲:

ansible_ssh_host     #用於指定被管理的主機的真實IP
ansible_ssh_port     #用於指定鏈接到被管理主機的ssh端口號,默認是22
ansible_ssh_user     #ssh鏈接時默認使用的用戶名
ansible_ssh_pass     #ssh鏈接時的密碼
ansible_sudo_pass     #使用sudo鏈接用戶時的密碼
ansible_sudo_exec     #若是sudo命令不在默認路徑,須要指定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解釋器的路徑,默認爲/usr/bin/python 一樣能夠指定ruby 、perl 的路徑
ansible_*_interpreter     #其餘解釋器路徑,用法與ansible_python_interpreter相似,這裏"*"能夠是ruby或才perl等其餘語言

上邊的實例也能夠以下配置直接使用用戶名和密碼和端口號進行鏈接

[root@Monitor ansible]# vim /etc/ansible/conf/hosts             
[webserver]
#192.168.180.4
192.168.180.5 ansible_ssh_port=10022 ansible_ssh_user=root ansible_ssh_pass='123456'
192.168.180.6 ansible_ssh_user=root ansible_ssh_pass='123456'
192.168.180.23 ansible_ssh_user=appuser ansible_ssh_pass='123456'


[root@Monitor ansible]# ansible webserver -m command -a 'uptime'
192.168.180.5 | SUCCESS | rc=0 >>
 16:54:09 up  1:01,  3 users,  load average: 0.00, 0.00, 0.00

192.168.180.23 | SUCCESS | rc=0 >>
 16:52:07 up 46 days,  1:34,  4 users,  load average: 0.00, 0.01, 0.05

192.168.180.6 | SUCCESS | rc=0 >>
 16:52:08 up 46 days,  1:34,  2 users,  load average: 2.05, 2.01, 2.00

(5)定義組變量

變量也能夠經過組名,而後應用到組內的全部成員。組變量的做用域是覆蓋組全部成員,經過定義一個新塊,塊名由組名+":vars" 組成,定義格式以下:

[web1]
Server6
Server5
[web1:vars]
ntp=192.168.180.23
nginx=192.168.180.5
[root@Monitor ansible]# ansible web1 -m command -a 'uptime'
Server5 | SUCCESS | rc=0 >>
 10:26:01 up 23 min,  2 users,  load average: 0.00, 0.00, 0.00

Server6 | SUCCESS | rc=0 >>
 10:26:06 up 40 min,  2 users,  load average: 0.00, 0.00, 0.00

####說明:上面的web1組中包含了兩臺主機Server5和Server6,經過對web1組指定了vars變量,相應的Server5和Server6主機至關於相應的指定了ntp和nginx變量的參數值。

(6)同時Ansible支持組嵌套組 ,經過定義一個新塊,塊名由組名+":children"組成,格式以下:

[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
southeast

如上面的示例中,我指定了杭州組我有host一、hosts2;嘉興組我有host三、host4主機;我又指定了一個組浙江組,同時包含杭州和嘉興;同時爲該組內的全部主機指定了四個vars變量。

分離主機與組特定數據

爲了更好規範定義的主機與組變量,Ansible支持將/etc/ansible/hosts定義的主機名與組變量單獨剝離出來存放到指定文件夾中.將採用YAML格式存放,存放位置規定: "/etc/ansible/group_vars/+組名" 和 "/etc/ansible/host_vars/+主機名" 分別存放指定組名或者主機名定義的變量。以下:

/etc/ansible/group_vars/dbservers

/etc/ansible/group_vars/webservers

定義的dbserver變量格式爲:

[/etc/ansible/group_vars/dbservers]

---

ntp_server:acme.example.org

database_server:storage.example.org


(二)Patterns(主機與組正則匹配部分)

把Patterns 其實就是ansible中的規則去管理哪些主機,也能夠理解爲,要與哪臺主機進行通訊。

如下ansible的用法:

ansible <pattern_goes_here> -m <module_name> -a <arguments>
ansible  <執行的客戶機列表>   -m <調用的模塊>    -a <執行的參數>

直接上一個示例:

ansible webservers -m service -a "name=httpd state=restarted"

這裏是ansible對webservers 組內的主機來進行遠程重啓httpd服務 ,其中webservers 就是Pattern部分。而之因此上面說Pattern(模式)能夠理解爲正則,主要針對下面常常用到的用法而言的。

  1. 表示全部的主機可使用all 或 *

  2.  通配符和邏輯或

利用通配符還能夠指定一組具備規則特徵的主機或主機名,冒號表示or---邏輯或

 *.baidu.com    ##表示全部的以baidu.com結尾的
 *.com             ###表示全部的以.com結尾的主機
webservers1[0]     #表示匹配 webservers1 組的第 1 個主機    webservers1[0:25]  #表示匹配 webservers1 組的第 1 個到第 25 個主機(官網文檔是":"表示範圍,測試發現應該使用"-",注意不要和匹配多個主機組混淆)
webservers:dbservers  #表示兩個組中全部的主機。是或的關係

3.邏輯非!與邏輯and

!非的表達式,如,目標主機必須在組webservers但不在dbservers組中

webservers:!dbservers     #####目標主機必須在webservers組中但不在dbservers組中

and的邏輯表達式:如,目標主機必須即在組webservers中又在組dbservers中

webservers:&dbservers        ####目標主機必須既在webservers組中又在dbservers組中

更復雜的例子爲:

webserver:dbservers:&nginx:!ntp  ###在webservers或者dbservers組中,必須還存在於nginx組中,可是不在ntp組中。

4.混合高級用法

*.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

以上部分主要按照官方Pattern部分進行翻譯和嘗試。


(三)常見的用法命令:

1,表示通配inventory中的全部主機:all *

[root@Monitor ansible]# ansible all -m ping    ###全部的客戶端執行ping 
Server5 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.180.4 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.180.23 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
Server6 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

2,指定具備規則特徵的主機IP或主機名

[root@Monitor ansible]# ansible web1 -m ping     ###全部web1組的主機執行ping
Server6 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
Server5 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

3,patterns能夠分表表示一個或多個組,多組之間用冒號分開,意味着一個主機能夠屬於多個組

[root@Monitor ansible]# ansible web1:dbserver -m ping 
Server6 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
Server5 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.180.2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.180.10 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

4,排除特定的主機

ansible webservers:!dbservers -m ping
#執行命令客戶機必須屬於webservers組,但不屬於dbservers組。

5,執行交集主機

ansible webservers:&dbservers -m ping
#執行命令客戶機同時隸屬於webservers組和dbservers組。

6,更復雜的條件

ansible webservers:database_server:&java_servers:!php_servers -m ping
#執行命令客戶機在webservers和database_server組中,也在java_servers卒中,可是不在php_servers組中。

7,能夠不嚴格定義groups、當個host names、ips、都支持通配符

ansible *.example.com -m ping
ansible *.com -m ping

8,同時支持通配和groups的混合使

ansible one*.com:dbservers

9,也能夠匹配一個組的特定編號的主機

ansible webservers[0] -m ping    #表示匹配webservers組的第一個主機
ansible webservers[-:25] -m ping #表示匹配webservers組的第一個到25個主機

10,在開頭的地方使用「~」,表明這是一個正則表達式方式表示

~(web|db).*\.example\.com 
ansible ~(web|db).*\.example.com -m ping

11,在ansible和ansible-playbook中,還能夠經過一個參數「--limit」來明確指定排除主機或組

ansible-playbook site.yml --limit database_server

12,從ansible1.2開始,若是想排除一個文件中的主機能夠用「@「

ansible-playbook site.yml --limit @retry_hosts.txt
相關文章
相關標籤/搜索