我的 Ansible 清单中的组没有按预期工作

Groups in my Ansible inventory does not work as expected

我的yaml inventory(开发环境)是这样的:

$> more inventory/dev/hosts.yml 

all:
  children:
    dmz1:
      children:
        ch:
          children:
            amq:
              hosts:
                myamqdev01.company.net: nodeId=1
                myamqdev02.company.net: nodeId=2
            smx:
              hosts:
                mysmxdev01.company.net: nodeId=1
                mysmxdev02.company.net: nodeId=2
    intranet:
      children:
        ch:
          children:
            amq:
              hosts:
                amqintradev01.company.net: nodeId=1
                amqintradev02.company.net: nodeId=2
            smx:
              hosts:
                smxintradev01.company.net: nodeId=1
                smxintradev02.company.net: nodeId=2  

当我尝试 ping(使用 ansible -i inventory/dev -m ping all 我收到错误:

children: | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname children:: Temporary failure in name resolution", 
    "unreachable": true
}
ch: | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname ch:: Temporary failure in name resolution", 
    "unreachable": true
}
all: | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname all:: Temporary failure in name resolution", 
    "unreachable": true
}
lan: | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname lan:: Temporary failure in name resolution", 
    "unreachable": true
}
amq: | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname amq:: Temporary failure in name resolution", 
    "unreachable": true
}
hosts: | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname hosts:: Temporary failure in name resolution", 
    "unreachable": true
}

etc...

为了排除故障,当我执行 ansible -i inventory/dev --list-hosts all 时,我得到:

hosts (16):
  all:
  children:
  dmz1:
  ch:
  amq:
  hosts:
  myamqdev01.company.net:
  myamqdev02.company.net:
  smx:
  mysmxdev01.company.net:
  mysmxdev02.company.net:
  intranet:
  amqintradev01.company.net:
  amqintradev02.company.net:
  smxintradev01.company.net:
  smxintradev02.company.net:

我认为这个命令应该只给主机,不是吗?

我不确定是什么问题,但我按照 official doc 中的示例进行操作,我认为我的 hosts.yml 文件有问题,但猜不出我遗漏了什么。

更新 当我根据答案更正 nodeId 变量时,列表 all 工作正常。但是,当我尝试通过中间父级进行过滤时,这是行不通的:

ansible -i inventory/dev --list-hosts intranet

不return内网主机而是全部。

当我尝试时:ansible -i inventory/dev --list-hosts amq 只有 amq 服务器正确 returned.

您的清单文件不符合 ansible 的格式,因此 yaml 清单插件无法解析它。由于它按顺序尝试插件,出于某种原因我并没有真正理解它最终使用 ini 格式成功并为文件中的每一行提供一个主机。

此外,您必须了解一个组(例如 smx)包含已在清单中定义的所有主机,无论它们在哪里定义(例如作为 intranet 的子级)或 dmz1).

因此在您的实际清单结构中,dmz1intranet 都包含组 amqsmx 作为子组,它们本身包含定义的所有主机,或者在intranetdmz1 部分。因此组 alldmz1intranet 在这里都是等价的并且包含清单中的所有主机。

这是解决格式问题的清单,其结构略有不同,可以满足您对群体定位的期望:

---
all:
  children:
    dmz1:
      hosts:
        myamqdev01.company.net:
          nodeId: 1
        myamqdev02.company.net:
          nodeId: 2
        mysmxdev01.company.net:
          nodeId: 1
        mysmxdev02.company.net:
          nodeId: 2
    intranet:
      hosts:
        amqintradev01.company.net:
          nodeId: 1
        amqintradev02.company.net:
          nodeId: 2
        smxintradev01.company.net:
          nodeId: 1
        smxintradev02.company.net:
          nodeId: 2
    amq:
      hosts:
        myamqdev01.company.net:
        myamqdev02.company.net:
        amqintradev01.company.net:
        amqintradev02.company.net:
    smx:
      hosts:
        mysmxdev01.company.net:
        mysmxdev02.company.net:
        smxintradev01.company.net:
        smxintradev02.company.net:

下面是一些如何定位所需机器组的示例

$ # All machines
$ ansible -i dev/ --list-hosts all
  hosts (8):
    myamqdev01.company.net
    myamqdev02.company.net
    mysmxdev01.company.net
    mysmxdev02.company.net
    amqintradev01.company.net
    amqintradev02.company.net
    smxintradev01.company.net
    smxintradev02.company.net
$ # Intranet
$ ansible -i dev/ --list-hosts intranet
  hosts (4):
    amqintradev01.company.net
    amqintradev02.company.net
    smxintradev01.company.net
    smxintradev02.company.net
$ # all smx machines
$ ansible -i dev/ --list-hosts smx
  hosts (4):
    mysmxdev01.company.net
    mysmxdev02.company.net
    smxintradev01.company.net
    smxintradev02.company.net
$ # amq machines only on dmz1
$ # 1. Only whith patterns
$ ansible -i dev/ --list-hosts 'amq:&dmz1'
  hosts (2):
    myamqdev01.company.net
    myamqdev02.company.net
$ # 2. Using limit
$ ansible -i dev/ --list-hosts amq -l dmz1
  hosts (2):
    myamqdev01.company.net
    myamqdev02.company.net