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": {}
        }
    }
}
3
  • 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, 2020 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. Apr 15, 2020 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. Apr 15, 2020 at 11:08

1 Answer 1

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!

2
  • This was my issue - the file was valid yaml but created with a .out extension. Changing to .yml worked.
    – Wesley
    Sep 13, 2022 at 19:26
  • Glad it worked ;)
    – iProcessor
    Sep 14, 2022 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .