Ansible主机清单(Inventory -- /etc/ansible/hosts)的INI写法及yaml写法介绍--todo

本文详细介绍了Ansible中主机清单的两种配置方式:INI语法和YAML语法,并通过多个实例展示了如何定义主机、组及变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考文章:Ansible02--hosts主机清单书写方式_徐金宝的博客-CSDN博客

自动化运维ansible——ansible清单配置(INI语法&YAML语法)_鲸鱼妹子‍的博客-CSDN博客_ansible清单

官方文档:How to build your inventory — Ansible Documentation 

几个默认组

有两个默认组:allungrouped

all包含所有主机

ungrouped包含只属于all组的主机

每个主机至少属于两个组

尽管allungrouped始终存在,但它们可以是隐式的,不会出现在group_names之类的组清单中。

写法比较

示例1: 

INI写法:

mail.example.com

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

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

YAML写法: 

all:
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:

示例二:

[test1]
192.168.10.11
192.168.10.12
[test2]
192.168.10.13

对应:

all:
  children:
    test1:
      hosts:
        192.168.10.11:
    test2:
      hosts:
        192.168.10.12:
        192.168.10.13:

示例三:

INI写法:

# 简单写法
172.25.254.2 ansible_port=22 ansible_user=root ansible_ssh_pass=redhat

# 别名写法:server2为该主机别名:
server2 ansible_host=172.25.254.2 ansible_port=22 ansible_user=root ansible_ssh_pass=redhat


[test1]
bieming1 ansible_host=192.168.10.11
[test2]
bieming2 ansible_host=192.168.10.12
bieming3 ansible_host=192.168.10.13
[A:children]
test1
test2

对应yml写法:

all:
  children:
    test1:
      hosts:
        bieming1:
          # 需要注意的是,ansible_host:和IP之间要有空格,且ip后不要有冒号
          ansible_host: 192.168.10.11
    test2:
      hosts:
        bieming2:
          ansible_host: 192.168.10.12
        bieming3:
          ansible_host: 192.168.10.13

示例四:

Adding ranges of hosts

In INI:

[webservers]
www[01:50].example.com

In YAML:

...
  webservers:
    hosts:
      www[01:50].example.com:

另一种写法:

In INI:

[webservers]
www[01:50:2].example.com

#other
[databases]
db-[a:f].example.com

In YAML:

...
  webservers:
    hosts:
      www[01:50:2].example.com:

示例五:

配置主机变量:

 In INI:

[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909

In YAML:

atlanta:
  hosts:
    host1:
      http_port: 80
      maxRequestsPerChild: 808
    host2:
      http_port: 303
      maxRequestsPerChild: 909

示例六:

组变量: 将变量分给多个组:

In INI:

[atlanta]
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

In YAML:

atlanta:
  hosts:
    host1:
    host2:
  vars:
    ntp_server: ntp.atlanta.example.com
    proxy: proxy.atlanta.example.com

示例七:

继承变量值,组的组变量:

You can make groups of groups using the :children suffix in INI or the children: entry in YAML. You can apply variables to these groups of groups using :vars or vars::

In INI:

[atlanta]
host1
host2

[raleigh]
host2
host3

[southeast:children]
atlanta
raleigh

[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2

[usa:children]
southeast
northeast
southwest
northwest

In YAML:

all:
  children:
    usa:
      children:
        southeast:
          children:
            atlanta:
              hosts:
                host1:
                host2:
            raleigh:
              hosts:
                host2:
                host3:
          vars:
            some_server: foo.southeast.example.com
            halon_system_timeout: 30
            self_destruct_countdown: 60
            escape_pods: 2
        northeast:
        northwest:
        southwest:

示例八:

组写法示例:

INI:

[proA]
172.25.254.3
[proB]
172.25.254.4

[pro:children]
proA
proB

YAML:

all:
  children:
    pro:
      children:
        proA:
          hosts:
            172.25.254.3:
        proB:
          hosts:
            172.25.254.4:

示例九:

使用别名的方式配置受管主机:
INI:

172.25.254.2
serverc ansible_host=172.25.254.3
172.25.254.4

YAML:

all:
  hosts:
    172.25.254.2:
    serverc:
      # 注意:ansible_host:后面要有空格,host后面不能有空格
      ansible_host: 172.25.254.3
    172.25.254.4:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风情客家__

原创不易,觉得好的话给个打赏哈

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值