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

相关推荐
JuiceFS1 天前
从 MLPerf Storage v2.0 看 AI 训练中的存储性能与扩展能力
运维·后端
chen9452 天前
mysql 3节点mgr集群部署
运维·后端
LH_R2 天前
OneTerm开源堡垒机实战(三):功能扩展与效率提升
运维·后端·安全
dessler2 天前
Hadoop HDFS-高可用集群部署
linux·运维·hdfs
少妇的美梦2 天前
logstash教程
运维
chen9452 天前
k8s集群部署vector日志采集器
运维
chen9452 天前
aws ec2部署harbor,使用s3存储
运维
白帽黑客沐瑶3 天前
【网络安全就业】信息安全专业的就业前景(非常详细)零基础入门到精通,收藏这篇就够了
网络·安全·web安全·计算机·程序员·编程·网络安全就业
東雪蓮☆3 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
qq_264220893 天前
LVS负载均衡群集和LVS+Keepalived群集
运维·负载均衡·lvs