Ansible 清单描述

系统:CentOS Linux release 7.9.2009 (Core)

安装Ansible :

yum -y install epel-release

yum -y install ansible

可以使用多种格式之一创建库存文件,最常见的格式是 INI 和 YAML。默认的INI文件在 /etc/ansible/hosts

清单的基础知识:格式、主机和组

InI格式,例如:

c 复制代码
# 域名或者IP, 这种就是属于主机类型
mail.example.com		

# [webservers] 这种就是属于组类型,可以控制组内的成员
[webservers]			
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com

YAML类型,例如:

c 复制代码
# 主机类型,默认划分到 ungrouped 组,后面会介绍
ungrouped:		
  hosts:
    mail.example.com:
webservers:
  hosts:
    foo.example.com:
    bar.example.com:
dbservers:
  hosts:
    one.example.com:
    two.example.com:
    three.example.com:

默认组

即使您没有在清单文件中定义任何组,Ansible 也会创建两个默认组: allungrouped。该all组包含每个主机。该ungrouped组包含没有加入到其他组的所有主机。每个主机将始终属于至少 2 个组(all和ungrouped/或all以及其他一些组)。比如上面的基本清单中,主机mail.example.com所属的all组和ungrouped组;主机two.example.com属于all组和dbservers组。尽管all和ungrouped始终存在,但它们可以是隐式的,不会出现在组列表中。上述 就没有出现all 和 ungrouped 组

将主机放到多个组中

INI格式:

c 复制代码
# foo.example.com 属于 [webservers] [prod] 组
# bar.example.com 属于 [webservers] [test] 组
# one.example.com 属于 [dbservers] [prod] 组
# two.example.com 属于 [dbservers] [prod] 组
# three.example.com 属于 [dbservers] [test]组
mail.example.com

[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com

[prod]
foo.example.com
one.example.com
two.example.com

[test]
bar.example.com
three.example.com

YAML格式:

c 复制代码
# foo.example.com 属于 [webservers] [prod] 组
# bar.example.com 属于 [webservers] [test] 组
# one.example.com 属于 [dbservers] [prod] 组
# two.example.com 属于 [dbservers] [prod] 组
# three.example.com 属于 [dbservers] [test]组
ungrouped:
  hosts:
    mail.example.com:
webservers:
  hosts:
    foo.example.com:
    bar.example.com:
dbservers:
  hosts:
    one.example.com:
    two.example.com:
    three.example.com:
prod:
  hosts:
    foo.example.com:
    one.example.com:
    two.example.com:
test:
  hosts:
    bar.example.com:
    three.example.com:

嵌套组(父/子组关系)

INI格式:

c 复制代码
# prod 组为父组,管理 webservers 组
# test 组为父组,管理 dbservers 组
mail.example.com

[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com

[prod:children]
webservers

[test:children]
dbservers

YAML格式:

c 复制代码
# prod 组为父组,管理 webservers 组
# test 组为父组,管理 dbservers 组
ungrouped:
  hosts:
    mail.example.com:
webservers:
  hosts:
    foo.example.com:
    bar.example.com:
dbservers:
  hosts:
    one.example.com:
    two.example.com:
    three.example.com:
prod:
  children:
    webservers:
test:
  children:
    dbservers:

添加主机范围

如果您有很多具有相似模式的主机,您可以将它们添加为一个范围,而不是单独列出每个主机名:

在 INI 中:

c 复制代码
[webservers]
www[01:50].example.com

在 YAML 中:

c 复制代码
# ...
  webservers:
    hosts:
      www[01:50].example.com:

用来表示 www01.example.com www02.example.com www03.example.com ... www50.example.com

您可以在定义主机的数字范围时指定步长(序列号之间的增量):

在 INI 中:

c 复制代码
[webservers]
www[01:50:2].example.com

在 YAML 中:

c 复制代码
# ...
  webservers:
    hosts:
      www[01:50:2].example.com:

上面的示例将使子域 www01、www03、www05、...、www49 匹配,但不会匹配 www00、www02、www50 等,因为步幅(增量)为每步 2 个单位。

相关推荐
云和数据.ChenGuang3 分钟前
Deepseek 持续迭代的模型
运维·运维技术·数据库运维工程师·运维教程
物联网软硬件开发-轨物科技30 分钟前
【轨物方案】聚焦锯床设备智能化升级,打造工业互联网新范式
运维·科技·物联网
kkk_皮蛋36 分钟前
RTCP: 统计、同步与网络自适应
网络
suzhou_speeder42 分钟前
企业数字化网络稳定运行与智能化管理解决方案
运维·服务器·网络·交换机·poe·poe交换机
专业开发者43 分钟前
Wi-Fi 认证:为企业网络筑牢未来根基
网络
RisunJan1 小时前
Linux命令-grpck命令(验证和修复组配置文件(`/etc/group` 和 `/etc/gshadow`)完整性的工具)
linux·运维·服务器
无敌最俊朗@2 小时前
Modbus TCP关键知识点回顾
网络
JY.yuyu2 小时前
TCP/UDP协议
网络·tcp/ip·udp
十月南城2 小时前
任务调度与异步化思路——定时、异步与重试的协作模型与幂等保障
网络
Evan芙2 小时前
nginx日志管理及日志格式定制
运维·nginx