|
Ansible 通过读取默认的主机清单配置/etc/ansible/hosts,来同时连接到多个远程主机,来执行远程操作任务的,但是如果要修改默认路径可以通过修改主配置文件 ansible.cfg 的 hostfile 参数指定相应的路径。具体查看相应的路径为:1
2
3
4
5
6
| [iyunv@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
|
接下来会详细讲解主机和组,以及他们的正则表达式的匹配:
(一)主机和组(Hosts and Groups)
通过配置/etc/ansible/hosts这个文件来定义主机和组
(1)简单的主机合组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| [iyunv@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
[iyunv@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
|
注意的要点有:
a.中括号中的名字代表组名,可以根据自己的需求将庞大的主机分成具有标识的组,如上面分了两个组webservers和dbservers组;
b.主机(hosts)部分可以使用域名、主机名、IP地址表示;当然使用前两者时,也需要主机能反解析到相应的IP地址,一般此类配置中多使用IP地址;
(2)端口与别名。
SSH默认的端口是22(此时的ansible主机配置文件可以省略),但是如果某些主机的SSH运行在自定义的端口上,ansible使用Paramiko进行ssh连接时不会使用你SSH配置文件中列出的端口,但是如果修改ansible使用openssh进行ssh连接时将会使用:
1
2
3
4
5
6
7
8
9
10
11
12
| ######在client192.168.180.5上ssh开启了2个端口连接
[iyunv@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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| ######在ansible服务端的配置:
[iyunv@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
[iyunv@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官方有个列子是通过指定主机名的范围来进行多台主机的定义
1
2
3
4
5
| [iyunv@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主机经常使用到的变量为:
1
2
3
4
5
6
7
8
9
10
11
| 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等其他语言
|
上边的实例也可以如下配置直接使用用户名和密码和端口号进行连接
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| [iyunv@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'
[iyunv@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" 组成,定义格式如下:
1
2
3
4
5
6
7
| [atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
|
同时Ansible支持组嵌套组 ,通过定义一个新块,块名由组名+":children"组成,格式如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| [atlanta]
host1
host2
[raleigh]
host2
host3
[southeast:children]
atlanta
raleigh
[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2
[usa:children]
southeast
northeast
southwest
southeast
|
分离主机与组特定数据
为了更好规范定义的主机与组变量,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 webservers -m service -a "name=httpd state=restarted"这里是ansible对webservers 组内的主机来进行远程重启httpd服务 ,其中webservers 就是Pattern部分。而之所以上面说Pattern(模式)可以理解为正则,主要针对下面经常用到的用法而言的。
- 表示所有的主机可以使用all 或 *
- 通配符和逻辑或
利用通配符还可以指定一组具有规则特征的主机或主机名,冒号表示or---逻辑或
1
2
3
| *.baidu.com ##表示所有的以baidu.com结尾的
*.com ###表示所有的以.com结尾的主机
webservers1[0] #表示匹配 webservers1 组的第 1 个主机 webservers1[0:25] #表示匹配 webservers1 组的第 1 个到第 25 个主机(官网文档是":"表示范围,测试发现应该使用"-",注意不要和匹配多个主机组混淆)
|
1
| webservers:dbservers #表示两个组中所有的主机。是或的关系
|
3.逻辑非!与逻辑and
!非的表达式,如,目标主机必须在组webservers但不在dbservers组中
1
| webservers:!dbservers #####目标主机必须在webservers组中但不在dbservers组中
|
and的逻辑表达式:如,目标主机必须即在组webservers中又在组dbservers中
1
| 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
|
|