从Ansible Playbook访问元组子级中的主机

问题描述

我正在尝试整理清单.yaml文件以按组排列主机,然后在Ansible剧本中调用它们。

inventory.yaml文件如下:

---
all:
  hosts:
    children:
      MY_Lab:
        Ubuntu_18:
          hosts:
            BWT_BKP_A:
              ansible_host: "10.2.8.19"

            BWT_BKP_B:
              ansible_host: "10.2.8.22"

            BWT_BKP_C:
              ansible_host: "10.2.9.12"
        vars:
          ansible_connection: ssh
          ansible_user: administrator
          ansible_ssh_pass: P@ssword
          ansible_sudo_pass: P@ssword

从我的Ansible剧本中,我想访问Ubuntu_18部分中的主机,但是我不能:

name: Test connectivity
  hosts: BWT_BKP_A
  vars:
   ansible_python_interpreter: /usr/bin/python3
  tasks:
    - name: Ping test
      ping:

我收到以下错误

 ansible-playbook ping-test.yaml -i inventory.yaml
[WARNING]: Could not match supplied host pattern,ignoring: BWT_BKP_A

PLAY [Test connectivity] *****************************************************************************************************
skipping: no hosts matched

如果我尝试整个小组,例如:

hosts: Ubuntu_18

我遇到同样的错误

不确定我缺少什么。我正在使用Ansible 2.9.12

解决方法

库存文件中有错误。 children键应该是hosts的同级物,而不是孩子,并且每次尝试创建“子组”时,都需要另一个children:键。

使用它作为我的测试清单:

---
all:
  children:
    MY_Lab:
      children:
        Ubuntu_18:
          hosts:
            BWT_BKP_A:
              ansible_host: "127.0.0.1"

            BWT_BKP_B:
              ansible_host: "127.0.0.1"

            BWT_BKP_C:
              ansible_host: "127.0.0.1"

我可以跑步:

$ ansible -i hosts.yml -m ping MY_Lab
BWT_BKP_B | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },"changed": false,"ping": "pong"
}
BWT_BKP_A | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },"ping": "pong"
}
BWT_BKP_C | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },"ping": "pong"
}

或者:

$ ansible -i hosts.yml -m ping BWT_BKP_A
BWT_BKP_A | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },"ping": "pong"
}

等等。