Inventory详解

Inventory详解

Ansible 可以根据Inventory文件实现同时配置多个系统。这个文件位于/etc/ansible/hosts。实际使用的时候可以编写多个Inventory文件,使用的时候用-i参数临时指定就行。

这个文件不仅可以自己编写、编写多个。配置的时候不光可以使用本地的资源,也可以使用云端的资源。

这个文件可以编写成多种格式,常用的有:INIYAML

Inventory基础:多台主机分组

表示同一个意思的inventory文件可以用INI写也可以用YAML写,不过需要安装对应的插件。

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:

将多台主机配置到多个组

可以给多个组同时装系统,于是可以做到这种效果:一台主机既是一个web服务器,又是一个数据库服务器

创建组的时候可以用这样一种思路:

  • 用来干啥

    应用,栈,微服务。比如: database serversweb servers

  • 在哪里

    在哪个数据中心或哪个区域的本地的DNS服务器,storage存储服务器。比如:eastwest

  • 什么阶段

    开发阶段,需要避免测试线上代码。比如:prodtest

包含以上的三种思想的YAML格式的Inventory文件可以写成这样:

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:
    east:
      hosts:
        foo.example.com:
        one.example.com:
        two.example.com:
    west:
      hosts:
        bar.example.com:
        three.example.com:
    prod:
      hosts:
        foo.example.com:
        one.example.com:
        two.example.com:
    test:
      hosts:
        bar.example.com:
        three.example.com:

可以看到one.example.comdbservers组中,也在east组中,也在prod组中。

也可以使用嵌套的方式简化这个文件:

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:
    east:
      hosts:
        foo.example.com:
        one.example.com:
        two.example.com:
    west:
      hosts:
        bar.example.com:
        three.example.com:
# 以上相同        
    prod:
      children:
        east:
    test:
      children:
        west:

如果用了这种简写的方法,要注意变量代表它包含的所有主机。

多台主机使用不同端口

如果有的主机没有用默认ssh端口,那么在主机名后加上端口就行。

如果要配置的主机的端口号确实不是默认端口22,那最好还是配一下端口。

就像这样:

badwolf.example.com:5309

如果主机的IP是静态的,不会变更,而且连接的时候需要通过隧道连接,那可以在hosts文件中配置一下别名。

INI文件这样配:

jumper ansible_port=5555 ansible_host=192.0.2.50
别名    端口号             主机名

YAML文件这样配:

...
  hosts:
    jumper:
      ansible_port: 5555
      ansible_host: 192.0.2.50

如果写入的主机名称有统一的模式,可以这样简写:

INI格式:

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

YAML格式:

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

对于数字的模式匹配,前导0可以根据需要加上或者去掉,前后的范围是闭区间的,两个边界值都可以取到。

也可以定义字母范围:

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

也可以选择连接类型每个主机的用户:

[targets]

localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_user=mpdehaan
other2.example.com     ansible_connection=ssh        ansible_user=mdehaan
主机                    连接类型                       连接用户

如上所述,在inventory文件中设置这些只是一种简化方法,稍后我们将讨论如何将它们存储在host_vars目录中的各个文件中。

用变量指代一台主机:主机变量

主机也可以用变量指代,而且现在指定的变量在后面的playbook中会用到。

INI格式:

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

YAML格式:

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格式 YAML格式
子组 :children children:
子组变量 :vars vars:

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

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:

几个默认组

有两个默认组:allungrouped

all包含所有主机

ungrouped包含只属于all组的主机

每个主机至少属于两个组

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

原文地址:https://www.cnblogs.com/lulujunsir/p/inventory.html