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

相关推荐
草莓熊Lotso1 小时前
Qt 进阶核心:UI 开发 + 项目解析 + 内存管理实战(从 Hello World 到对象树)
运维·开发语言·c++·人工智能·qt·ui·智能手机
薛定谔的猫19828 小时前
RAG(二)基于 LangChain+FAISS + 通义千问搭建轻量级 RAG 检索增强生成系统
运维·服务器·langchain
米高梅狮子8 小时前
4. Linux 进程调度管理
linux·运维·服务器
再创世纪9 小时前
让USB打印机变网络打印机,秀才USB打印服务器
linux·运维·网络
昨夜见军贴061610 小时前
IACheck AI审核如何实现自动化来料证书报告审核,全面提升生产效率与合规水平
运维·人工智能·自动化
yugi98783811 小时前
异构网络下信道环境建模方法及应用
开发语言·网络
浩子智控11 小时前
电子产品设计企业知识管理
运维·服务器·eclipse·系统安全·硬件工程
Xの哲學12 小时前
深入解析 Linux systemd: 现代初始化系统的设计与实现
linux·服务器·网络·算法·边缘计算
龙月12 小时前
journalctl命令以及参数详解
linux·运维
C_心欲无痕12 小时前
网络相关 - 强缓存与协商缓存讲解
前端·网络·网络协议·缓存