Ansible程序员程序猿阵线联盟-汇总各类技术干货

Ansible Inventory - 管理主机的清单

2018-02-28  本文已影响347人  Anoyi

默认存于 /etc/ansible/hosts 文件中,也可以通过 -i <PATH> 指定到文件。

主机和组

示例:INI 格式

all.example.com

other ansible_port=5555 ansible_host=192.0.2.50

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com
db2.example.com
db3.example.com

[otherservers]
other.example.com:8080

示例:YAML 格式

all:
  hosts:
    all.example.com
    other:
      ansible_port: 5555
      ansible_host: 192.0.2.50
  children:
    webservers:
      hosts:
        web1.example.com:
        web2.example.com:
    dbservers:
      hosts:
        db1.example.com:
        db2.example.com:
        db3.example.com:

主机变量

可供 playbooks 使用,示例:

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

组变量

示例:INI 格式

[atlanta]
host1
host2

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

示例:YAML 格式

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

群组和组变量

示例:INI 格式

[atlanta]
host1
host2

[raleigh]
host2
host3

[southeast:children]
atlanta
raleigh

[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30

[usa:children]
southeast
northeast

示例: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
       northeast:

如果需要存储列表或散列数据,建议将主机和组特定变量与 Inventory 文件分开。

关于群组的补充说明:

默认组

有两个默认组:allungrouped

即每个主机将永远属于至少2个组。

参考资料

Ansible - Inventory

上一篇下一篇

猜你喜欢

热点阅读