2.ansible部署

2.ansible部署,第1张


一、简介

在使用Ansible来批量管理主机的时候,通常我们需要先定义要管理哪些主机或者主机组,而这个用于管理主机与主机

组的文件就叫做Inventory,也叫主机清单。


该文件默认位于/etc/ansible/hosts。


当然我们也可以通过修改ansible配置文

件的hosƞile配置项来修改默认inventory的位置。



二、定义主机和组

对于/etc/ansible/hosts最简单的定义格式像下面:

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

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

mail.yanruogu.com

[webservers]

web1.yanruogu.com

web2.yanruogu.com 

[dbservers]

db1.yanruogu.com

db2.yanruogu.com
2. 指定主机范围
# 下面指定了从web01到web50,webservers组共计50台主机;databases组有db-a到db-f共6台主机

[webservers]

www[01:50].yanruogu.com

[databases]

db-[a:f].yanruogu.com
3.定义主机组嵌套

一个主机组可以包含若干个子主机组:

# 如下示例中,production组包含两个子组,分别为webservers和dbservers,webservers和dbservers组分别
[webservers]
web1.lab.example.com
web2.lab.example.com
[dbservers]
db1.lab.example.com
db2.lab.example.com
[production:children]
webservers
dbservers
4.选择主机与组

在前面定义Inventory的时候,我们会把所有被管理主机通过主机组的方式定义到Inventory当中,但是当我们实际使用

的时候,可能只需要对某一主机或主机组进行 *** 作,这个时候就需要通过匹配的方式指定某一特定主机或主机组。


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

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
5.补充
重要:
两个主机始终存在:
●all主机组含有清单中明确列出的每一个主机。


●unground主机组含有清单中明确列出、但不属于任何其他组的每一个主机。


# 查看主机列表信息 ansible-inventory -i inventory --list ansible-inventory -i inventory --graph

验证清单

若有疑问,可使用ansible命令验证计算机是否存在于清单中;

[user@controlnode ~]$ ansible washington1.example.com --list-hosts

   hosts(1):

     washington1.example.com

[user@controlnode ~]$ ansible washington01.example.com --list-hosts

  [WARNING];provided hosts list is empty,only localhost is available
  
  hosts(0):


二、构建ansible清单









三、管理ANSIBLE配置文件












欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/571818.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存