1
votes

Disclaimer: I'm new to ansible and couldn't find an answer here, here or here.

I need to run a playbook against 2 hosts. I understand how to do that with a static inventory in yaml or INI format, but I'm lost when trying it using a dynamic inventory--specifically, VMWare dynamic inventory - vmware_inventory.py . The results of running the dynamic inventory look like this:

{
    "_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",
            ...

NOTE: activedirectory-devops is a "tag" in VMWare vCenter that is (apparently) returned as an ansible "host group".

Given that, how can I run a playbook against the hosts in activedirectory-devops? After all, "foo_420be125-0a38-6dcd-247c-1d1839717804" can't be resolved by DNS.

My best guess is:

ansible-playbook -i vmware.yml site.yml
2

2 Answers

2
votes

I just solved my own question as follows:

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

Results:

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

Not sure which dynamic inventory file you are tying..

You can try "skip_keys" in dynamic inventory .ini file to removed unwanted attributes. (un-comment skip_keys in ini file and specify unwanted attributes)

and for your playbook using dynamic inventory:

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

where inventory directory contains...

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