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 个单位。

相关推荐
Logan Lie14 分钟前
Web服务监听地址的取舍:0.0.0.0 vs 127.0.0.1
运维·后端
Y淑滢潇潇38 分钟前
RHCE 防火墙实验
linux·运维·rhce
wadesir1 小时前
当前位置:首页 > 服务器技术 > 正文Linux网络HSRP协议(实现路由器热备份与高可用性的实用指南)
linux·服务器·网络
稻谷君W1 小时前
Ubuntu 远程访问 Win11 WSL2 并固定访问教程
linux·运维·ubuntu
泡沫·1 小时前
4.iSCSI 服务器
运维·服务器·数据库
带土12 小时前
4. 两台win11 笔记本局域网内文件传输
网络
悠悠121382 小时前
告别Zabbix?我用Netdata只花10分钟就搞定了50台服务器的秒级监控(保姆级实战)
运维·服务器·zabbix
天庭鸡腿哥2 小时前
大小只有4K的软件,可让系统瞬间丝滑!
运维·服务器·windows·microsoft·everything
虚伪的空想家2 小时前
华为昇腾Atlas 800 A2物理服务器开启VT-d模式
运维·服务器·ubuntu·kvm·vt-d·直通
xixixi777773 小时前
“C2隐藏”——命令与控制服务器的隐藏技术
网络·学习·安全·代理·隐藏·合法服务·c2隐藏