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

相关推荐
Java狂魔哥27 分钟前
计算机网络软考
网络·计算机网络
明达技术29 分钟前
分布式 IO :开启垃圾分类机械臂自动化新篇章
运维·分布式·自动化
头发尚存的猿小二1 小时前
Linux——基本指令
linux·运维·服务器
诚丞成1 小时前
linux启程指南——体悟虚拟开源天地的漫步翩翩
linux·运维·开源
晴天Y282 小时前
MHA集群
运维·服务器·数据库
闪耀迪迦`4 小时前
linux常见操作命令
linux·运维·服务器
m0_748232924 小时前
自己搭建远程桌面服务器-RustDesk(小白版)
运维·服务器
Channing Lewis4 小时前
服务器和本地电脑之间如何传输文件
运维·服务器
众乐乐_20085 小时前
腾讯云扩容记录
linux·运维·服务器
web135085886356 小时前
在Linux系统上使用nmcli命令配置各种网络(有线、无线、vlan、vxlan、路由、网桥等)
linux·服务器·网络