0

I am trying to create dynamic inventory for my setup. everywhere i see examples of IPs and hostname as list. but when i provide hosts value as list it error's out and says it has to be dict.

Below inventory i am providing to playbook,

{
    "all": {
        "hosts": ["192.168.1.2", "192.168.1.3", "192.168.1.4"],
        "vars": {
             "ansible_ssh_user": "root"
        },
        "_meta": {
            "hostvars": {}
        }
    },
    "webserver": {
        "hosts": ["192.168.1.3", "192.168.1.4"],
        "vars": {
             "ansible_ssh_user": "root"
        },
        "_meta": {
            "hostvars": {}
        }
    },
    "dbserver": {
        "hosts": ["192.168.1.2"],
        "vars": {
             "ansible_ssh_user": "root"
        },
        "_meta": {
            "hostvars": {}
        }
    }
}

it gives this warnning and error

[WARNING]:  * Failed to parse /Users/rahulbhatu/playbooks/waf-
playbooks/inventory with yaml plugin: Invalid "hosts" entry for "all" group,
requires a dictionary, found "<type 'list'>" instead.

[WARNING]:  * Failed to parse /Users/rahulbhatu/playbooks/waf-
playbooks/inventory with ini plugin: Invalid host pattern 'all:' supplied,
ending in ':' is not allowed, this character is reserved to provide a port.

[WARNING]: Unable to parse /Users/rahulbhatu/playbooks/waf-playbooks/inventory
as an inventory source

[WARNING]: No inventory was parsed, only implicit localhost is available```
fatal: [{]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname {: nodename nor servname provided, or not known", "unreachable": true}

Howeever when i pass it this way as a dict and value as null it works, can someone help me what i am missing as.

    "all": {
        "hosts": {
             "192.168.1.2": null,
             "192.168.1.3" : null,
             "192.168.1.4" : null
        },
        "vars": {
             "ansible_ssh_user": "root"
        },
        "_meta": {
            "hostvars": {}
        }
    },
    "webserver": {
        "hosts": {
            "192.168.1.3": null,
            "192.168.1.4": null
        },
        "vars": {
             "ansible_ssh_user": "root"
        },
        "_meta": {
            "hostvars": {}
        }
    },
    "dbserver": {
        "hosts": {
            "192.168.1.2": null
        },
        "vars": {
             "ansible_ssh_user": "root"
        },
        "_meta": {
            "hostvars": {}
        }
    }
}
  • Are you sure that second example works? I tried and it does not. Error message that you get is quite clear, it expects dictionary `{}` but it gets list ``[]. So change `[]` to `{}` in `"hosts"`. And `"_meta"` wont work. – dexter Apr 14 '20 at 10:07
  • @dexter The error is clear but i need to pass list of hosts not as a dictionary. Yes the second one works. however i figured out that the problem was that i was passing this json file as an inventory and it errors but when inventory.py produces the same json output with list ``[]`` it works. – Rahul Bhatu Apr 15 '20 at 11:07
  • Now when my inventory script output as the first one which contains list of hosts and it works. but when i pass it as a file it doesn't. – Rahul Bhatu Apr 15 '20 at 11:08

1 Answers1

1

Apart from the above error, you can get the same error if you are using a .ini hosts file. Change the extension to a .yml file to match the actual file format. Mine was that simple and it was resolved like that!

Rowan Hawkins
  • 590
  • 2
  • 18
iProcessor
  • 11
  • 2