【问题标题】:Ansible inventory group array pointer to parent指向父级的 Ansible 库存组数组指针
【发布时间】:2017-09-13 18:52:29
【问题描述】:

我想检查是否有人知道我在 Ansible 中遇到的这个问题是否有解决方案。

我有一个如下所示的库存文件:-

[clusterA]
10.0.0.1
10.0.0.2
10.0.0.3
....

[clusterB]
10.1.0.1
10.1.0.2
10.1.0.3
....

[web-lb]
10.0.0.1
10.1.0.1

我不想在web-lb组中重复IP地址,而是想做这样的事情:-

[web-lb:children]
clusterA[0]
clusterB[0]

如果我们可以如上所述编写组,我不需要复制 IP 地址,我可以将组中的不同项目混合到另一个组中,例如

[webA-lb:children]
clusterA[1]
clusterA[5]
clusterB[3]

更新

下面的配置也不行

[webA-lb]
clusterA[1]

错误:

bbed5901ea74:~$ ansible -i hosts all --list-hosts
hosts (6):
10.1.0.1
10.1.0.2
10.1.0.3
10.0.0.1
10.0.0.2
10.0.0.3
bbed5901ea74:~$ vi hosts
bbed5901ea74:~$ ansible -i hosts all --list-hosts
ERROR! Attempted to read "hosts" as YAML: Syntax Error while loading YAML.


The error appears to have been in '/home/jenkins/hosts': line 2, column 1, 
but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

[clusterA]
10.0.0.1
^ here

Attempted to read "hosts" as ini file: host range must be begin:end or 
begin:end:step

【问题讨论】:

    标签: ansible ansible-inventory


    【解决方案1】:

    我认为您不能像这样将:children 与单个选择器[] 结合使用。 :children 后缀表示一组组。

    所以你可以这样做:

    [web-lb]
    clusterA[0]
    clusterB[0]
    

    但不是这个:

    [web-lb:children]
    clusterA[0]
    clusterB[0]
    

    有关详细信息,请参阅 Ansible 的 PatternsInventory 文档。 Ansible 非常灵活。您还可以使用! 符号将某些孩子排除在组之外。

    【讨论】:

    • 我确实尝试了您提到的方法,但即使我们在组中省略了 children 标记,ansible 似乎也无法理解。
    • 嗯...你知道为什么它说它在 INI 语法时试图将你的 hosts 文件读取为 YAML 吗?
    • 当我在组中有方括号时,就会发生此错误。然后我将hosts 更改为hosts.ini 并再试一次,我收到此错误29286d512845:~$ ansible -i hosts.ini all --list-hosts ERROR! Attempted to read "hosts.ini" as ini file: host range must be begin:end or begin:end:step
    • 感谢您的快速回复。如果我正确理解了 ansible inventory,则 pattern 的用法可以应用于像 192.168.2.[100-110] 这样的主机,但它不能在组上使用。从playbook 的角度来看,您可以应用hosts: clusterA[1] 之类的组模式。尽管如此,我已经放弃了这种我想按照我之前的帖子中想要的方式布局我的inventory 文件的方法。
    • 我已经从您提供的链接中再次使用 YAML 进行测试。它确实显示了相同的错误host range must be begin:end or begin:end:step。我的结论是,您不能在孩子或其他组本身的组上分配模式。您可以改为在主机上执行模式
    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 2016-07-30
    • 1970-01-01
    相关资源
    最近更新 更多