Ansible:如何从动态清单中的标签获取主机

时间:2019-10-30 15:00:35

标签: ansible ansible-inventory

免责声明:我是ansible的新手,找不到答案hereherehere

我需要针对2位主机运行一个剧本。我知道如何使用yamlINI格式的静态广告资源来做到这一点,但是当我尝试使用动态广告资源(特别是VMWare dynamic inventory - vmware_inventory.py )时却迷失了方向。 运行动态库存的结果如下所示:

{
    "_meta": {
        "hostvars": {
            "foo_420be125-0a38-6dcd-247c-1d1839717804": {
                "ansible_connection": "ssh",
                "ansible_user": "root",
                "config.cpuHotAddEnabled": false,
                "config.cpuHotRemoveEnabled": false,
                "config.hardware.numCPU": 4,
                "config.instanceUuid": "500b86dc-b51e-25fb-165d-e51c62ecd725",
                "config.name": "foo",
                "config.template": false,
                "guest.guestId": null,
                "guest.guestState": "notRunning",
                "guest.hostName": "foo.bar.com",
                "guest.ipAddress": "1.2.3.4",
                "name": "foo",
                "runtime.maxMemoryUsage": null,
                "stage": "dev"
            },
            "bar_fffe-6f29-3e32-0ce9a80d0ad3": {
                "ansible_connection": "ssh",
            ...
        }
    },
    "activedirectory-devops": {
        "hosts": [
            "foo_420be125-0a38-6dcd-247c-1d1839717804",
            "bar_fffe-6f29-3e32-0ce9a80d0ad3"
        ]
    },
    "all": {
        "children": [
            "activedirectory-devops",
            "centos64Guest",
            "centos7_64Guest",
            "com.vmware.vr.HasVrDisks",
            "other3xLinux64Guest",
            "otherGuest",
            ...

注意:activedirectory-devops是VMWare vCenter中的一个“标记”,(显然)作为一个有趣的“主机组”返回。

鉴于此,我如何针对activedirectory-devops中的主机运行剧本?毕竟,DNS无法解析“ foo_420be125-0a38-6dcd-247c-1d1839717804”。

我最好的猜测是:

ansible-playbook -i vmware.yml site.yml

2 个答案:

答案 0 :(得分:1)

不确定要绑定的动态清单文件。

您可以尝试在动态清单.ini文件中使用“ skip_keys”来删除不需要的属性。 (在ini文件中取消注释skip_keys并指定不需要的属性)

,以及使用动态广告资源的剧本:

ANSIBLE_HOSTS variable to always use the VMWare inventory:
export ANSIBLE_HOSTS="/home/blabla/vmware-ansible/query.py"

Or using playbook:
ansible-playbook example.yml -i inventory

库存目录包含...

inventory/
  01-openstack.yml          # configure inventory plugin to get hosts from Openstack cloud
  02-dynamic-inventory.py   # add additional hosts with dynamic inventory script
  03-static-inventory       # add static hosts
  group_vars/
    all.yml                 # assign variables to all hosts

答案 1 :(得分:0)

我刚刚解决了自己的问题,如下所示:

ansible activedirectory-devops -m ping --connection=local -i vmware.yml

结果:

foo_420be125-0a38-6dcd-247c-1d1839717804 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
bar_fffe-6f29-3e32-0ce9a80d0ad3 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}