欢迎来到战五渣的博客

人生三重境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

002.Ansible之Inventory文件

一 简介

在使用Ansible来批量管理主机的时候,通常我们需要先定义要管理哪些主机或者主机组,而这个用于管理主机与主机组的文件就叫做Inventory,也叫主机清单。该文件默认位于/etc/ansible/hosts。当然我们也可以通过修改ansible配置文件的hostfile配置项来修改默认inventory的位置。

二 定义主机和组

有五个主机

node1  192.168.132.131  主控端

node2  192.169.132.132  被控端

node3  192.169.132.133  被控端

node4  192.169.132.134  被控端

node5  192.169.132.135  被控端

2.1 直接写进hosts文件

对于/etc/ansible/hosts最简单的定义格式像下面,直接把IP写在里面:

[root@node1 ansible]# cat /etc/ansible/hosts

localhost
192.168.132.132
192.168.132.133
192.168.132.134
192.168.132.135

2.2 简单实用ping模块检测连通性

[root@node1 ansible]# ansible 192.168.132.132 -m ping

ansible  指定主机或者主机组  -m 后跟使用的模块,ping是ansible测试和被控端的连通性

是因为没有配置密码认证也没有配置ssh秘钥自动连接,主机之间不能交互

方法一:

修改配置,可以输入密码

[root@node1 ansible]# vim /etc/ansible/ansible.cfg

[defaults]

# some basic default values...

#inventory      = /etc/ansible/hosts
#library        = /usr/share/my_modules/
#module_utils   = /usr/share/my_module_utils/
#remote_tmp     = ~/.ansible/tmp
#local_tmp      = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks          = 5
#poll_interval  = 15
#sudo_user      = root
#ask_sudo_pass = True
ask_pass      = True    #打开这个配置
#transport      = smart
#remote_port    = 22
#module_lang    = C
#module_set_locale = False

再次执行

  

使用秘钥自动登录认证

恢复配置,注释掉ack_pass

[root@node1 ansible]# ssh-copy-id root@192.168.132.132

[root@node1 ansible]# ansible 192.168.132.132 -m ping

不用输入密码,也可以使用模块操作

方法三,使用hosts配置密码

修改hosts文件

[root@node1 ansible]# cat /etc/ansible/hosts

localhost
192.168.132.132  
192.168.132.133  ansible_ssh_pass=redhat
192.168.132.134
192.168.132.135

执行

[root@node1 ansible]# ansible 192.168.132.133 -m ping

在这里可以使用这种方式推送key到被控端,然后删掉hosts的密码配置

2.3 实用普通用户控制

配置普通用户

[root@node2 ~]# useradd ansible

[root@node2 ~]# vim /etc/sudoers.d/ansible

ansible       ALL=(ALL)       NOPASSWD: ALL

配置秘钥

[root@node2 ~]# mkdir  /home/ansible/.ssh
[root@node2 ~]# cp /root/.ssh/authorized_keys /home/ansible/.ssh/
[root@node2 ~]# chmod 600 /home/ansible/.ssh/authorized_keys
[root@node2 ~]# chown ansible:ansible /home/ansible/.ssh  -R
[root@node2 ~]# ll /home/ansible/.ssh/ -d
drwxr-xr-x 2 ansible ansible 29 Apr 27 10:52 /home/ansible/.ssh/
[root@node2 ~]# ll /home/ansible/.ssh/authorized_keys
-rw------- 1 ansible ansible 0 Apr 27 10:52 /home/ansible/.ssh/authorized_keys

主控段配置ansible用户

[root@node1 ansible]# vim ansible.cfg

remote_user = ansible

检查连通性

 检查用户,使用shell模块

但是到133就会报错,用户被denied

按照相同方式配置其他几台机器配置ansible用户,并提权设置

2.4 hosts文件管理

使用主机名连接,则需要保证主机名可以被解析

[root@node1 ansible]# cat /etc/ansible/hosts 

localhost
node2  
192.168.132.133 
192.168.132.134
192.168.132.135

不能解析

添加/etc/hosts

[root@node1 ansible]# vim /etc/hosts

192.168.132.132  node2  

[root@node1 ansible]# ansible node2  -m shell -a "whoami"

node2 | CHANGED | rc=0 >>
ansible

也可以配置/etc/ansivle/hosts

[root@node1 ansible]# vim /etc/ansible/hosts

localhost
node2  
node3 ansible_ssh_host=192.168.132.133 
192.168.132.134
192.168.132.135

也可以使用主机名

 

但是不能使用IP

连接自己[root@node1 ansible]# ansible localhost -m ping

也可以直接指定

[root@node1 ansible]# vim /etc/ansible/hosts 

localhost  ansible_connection=local
node2  
node3 ansible_ssh_host=192.168.132.133 
192.168.132.134
192.168.132.135

三 主机分组

配置都使用主机组

3.1 简答配置

[root@node1 ansible]# vim /etc/hosts

192.168.132.131  node1  
192.168.132.132  node2  
192.168.132.133  node3  
192.168.132.134  node4  
192.168.132.135  node5  

[root@node1 ansible]# vim /etc/ansible/hosts

[web]
node1
node2
[mysql]
node3
node4
[redis]
node5

执行

[root@node1 ansible]# ansible web -m ping

一个主机可位于多个组

[web]
node1 
node2
[mysql]  
node3 
node4
[redis]
node5
[php]
node1
node3

执行

[root@node1 ansible]# ansible php  -m  shell -a "hostname"

 

# 中括号中的名字代表组名,可以根据自己的需求将庞大的主机分成具有标识的组,如上面分了两个组webservers和dbservers组;

# 主机(hosts)部分可以使用域名、主机名、IP地址表示;当然使用前两者时,也需要主机能反解析到相应的IP地址,一般此类配置中多使用IP地址;

node4
[web]
node1 
node2
[mysql]  
node3 
node4
[redis]
node5
[php]
node1
node3

hosts的名字可以使用其他的文件名,但是默认找hosts文件,需要用到其他文件

[root@node1 ansible]# cp hosts inventory
[root@node1 ansible]# ansible -i inventory php -m shell -a "hostname"

也可以使用飞hosts命名的文件

3.2  指定主机范围

# 下面指定了从web-node01到web-node50,webservers组共计50台主机;databases组有db-node-a到db-node-f共6台主机
[webservers]
web-node[01:50].test.com
[databases]
db-node[a:f].test.com

3.3 定义主机组嵌套

# 如下示例中,production组包含两个子组,分别为webservers和dbservers,webservers和dbservers组分别包含若干主机
[webservers]
node1
node2
[dbservers]
node3

[php:children]
webservers
dbservers

执行

[root@node1 ansible]# ansible php -m shell -a "echo hostname"

node3 | CHANGED | rc=0 >>
hostname
node2 | CHANGED | rc=0 >>
hostname
node1 | CHANGED | rc=0 >>
hostname

两个主机组都有打印

四 选择主机与组

在前面定义Inventory的时候,我们会把所有被管理主机通过主机组的方式定义到Inventory当中,但是当我们实际使用的时候,可能只需要对某一主机或主机组进行操作,这个时候就需要通过匹配的方式指定某一特定主机或主机组。

4.1 定义主机组

在此之前,先定义一个主机清单示例:

[root@node1 ansible]# cat /etc/ansible/hosts

srv1.example.com
srv2.example.com
s1.lab.example.com
s2.lab.example.com

[web]
jupiter.lab.example.com
saturn.example.com

[db]
db1.example.com
db2.example.com
db3.example.com

[lb]
lb1.lab.example.com
lb2.lab.example.com

[boston]
db1.example.com
jupiter.lab.example.com
lb2.lab.example.com

[london]
db2.example.com
db3.example.com
file1.lab.example.com
lb1.lab.example.com

[dev]
web1.lab.example.com
db3.example.com

[stage]
file2.example.com
db2.example.com

[prod]
lb2.lab.example.com
db1.example.com
jupiter.lab.example.com

[function:children]
web
db
lb
city

[city:children]
boston
london
environments

[environments:children]
dev
stage
prod
new

[new]
172.25.252.23
172.25.252.44

14.2 匹配所有主机

可以通过all或者*来指定匹配所有主机,通过如下指令查看all匹配到的主机:

[root@node1 ansible]# ansible all --list-hosts

或者

4.3 匹配指定的主机或主机组

匹配单个组

[root@node1 ansible]# ansible prod --list-hosts

匹配单个主机

[root@node1 ansible]# ansible db2.example.com --list-hosts

  hosts (1):
    db2.example.com

匹配多个主机

 [root@node1 ansible]# ansible 'lb1.lab.example.com,s1.lab.example.com,db1.example.com' --list-hosts

  hosts (3):
    lb1.lab.example.com
    s1.lab.example.com
    db1.example.com

匹配多个组

[root@node1 ansible]# ansible 'london,boston' --list-hosts

  hosts (7):
    db2.example.com
    db3.example.com
    file1.lab.example.com
    lb1.lab.example.com
    db1.example.com
    jupiter.lab.example.com
    lb2.lab.example.com

匹配不属于任何组的主机

[root@node1 ansible]#  ansible ungrouped --list-hosts

  hosts (4):
    srv1.example.com
    srv2.example.com
    s1.lab.example.com
    s2.lab.example.com

4.4 通配符匹配

匹配'*.example.com':

[root@node1 ansible]# ansible '*.example.com' --list-hosts

  hosts (14):
    s1.lab.example.com
    file1.lab.example.com
    lb1.lab.example.com
    srv2.example.com
    db3.example.com
    srv1.example.com
    web1.lab.example.com
    db2.example.com
    db1.example.com
    jupiter.lab.example.com
    lb2.lab.example.com
    file2.example.com
    s2.lab.example.com
    saturn.example.com

匹配172.25.*的主机:

[root@node1 ansible]# ansible '172.25.*' --list-hosts

[root@node1 ansible]#  ansible '172.25.*' --list-hosts 
  hosts (2):
    172.25.252.23
    172.25.252.44

匹配以s开头的主机及主机组:

[root@node1 ansible]# ansible 's*' --list-hosts

  hosts (7):
    file2.example.com
    db2.example.com
    s1.lab.example.com
    srv2.example.com
    srv1.example.com
    s2.lab.example.com
    saturn.example.com

4.5 通配符组合匹配

匹配包含*.example.com但不包含*.lab.example.com的主机:

 [root@node1 ansible]# ansible '*.example.com,!*.lab.example.com' --list-hosts  

  hosts (7):
    srv2.example.com
    db3.example.com
    srv1.example.com
    db2.example.com
    db1.example.com
    file2.example.com
    saturn.example.com

匹配包含prod以及172开头、包含lab关键字的主机或组

[root@node1 ansible]# ansible 'prod,172*,*lab*' --list-hosts

  hosts (10):
    lb2.lab.example.com
    db1.example.com
    jupiter.lab.example.com
    172.25.252.23
    172.25.252.44
    s1.lab.example.com
    file1.lab.example.com
    lb1.lab.example.com
    web1.lab.example.com
    s2.lab.example.com

匹配属于db组同时还属于london组的主机:

[root@node1 ansible]# ansible 'db,&london' --list-hosts

  hosts (2):
    db2.example.com
    db3.example.com

匹配在london组或者boston组,还必须在prod组中且必须不在lb组中的主机:

[root@node1 ansible]# ansible 'boston,london,&prod,!lb' --list-hosts

  hosts (2):
    db1.example.com
    jupiter.lab.example.com

4.6 正则表达式匹配

在开头的地方使用”~”,用来表示这是一个正则表达式:

[root@node1 ansible]# ansible '~(s|db).*example\.com' --list-hosts

  hosts (8):
    s1.lab.example.com
    srv2.example.com
    db3.example.com
    srv1.example.com
    db2.example.com
    db1.example.com
    s2.lab.example.com
    saturn.example.com

 4.7 通过--limit明确指定主机或组

通过--limit在选定的组中明确指定主机:

[root@node1 ansible]# ansible ungrouped  --limit srv1.example.com --list-hosts

  hosts (1):
    srv1.example.com

通过--limit参数,还可以指定一个文件,该文件中定义明确指定的主机的列表,定义一个retry_hosts.txt如下:

[root@node1 ansible]# vim retry_hosts.txt

srv1.example.com

[root@node1 ansible]# ansible ungrouped  --limit @retry_hosts.txt --list-hosts

  hosts (1):
    srv1.example.com

4.8 通配符和正则表达式配合使用

[root@node1 ansible]# ansible '~(s|db).*,prod,*.lab.example.com' --list-hosts

  hosts (14):
    db1.example.com
    db2.example.com
    db3.example.com
    file2.example.com
    s1.lab.example.com
    srv2.example.com
    srv1.example.com
    s2.lab.example.com
    saturn.example.com
    lb2.lab.example.com
    jupiter.lab.example.com
    file1.lab.example.com
    lb1.lab.example.com
    web1.lab.example.com

主机组示例结束


博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!

posted @ 2020-04-28 11:23  梦中泪  阅读(1677)  评论(0编辑  收藏  举报