ansible-documentation icon indicating copy to clipboard operation
ansible-documentation copied to clipboard

JSON example in script inventory development guide is wrong

Open juresaht2 opened this issue 1 year ago • 2 comments

The skeleton JSON object documented in: https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#tuning-the-external-inventory-script

{
    "_meta": {
      "hostvars": {}
    },
    "all": {
      "children": [
        "ungrouped"
      ]
    },
    "ungrouped": {
      "children": [
      ]
    }
}

...doesn't seem to work when a inventory script outputs it.

It's hard to figure out what exactly is happening from just interpreting the debugging output while learning how to do this, but it looks like it's trying to load hosts as groups and does not parse the hostvars.

I was able to get things working using the structure documented in: https://github.com/geerlingguy/ansible-for-devops/blob/master/dynamic-inventory/custom/inventory.php

{
  "_meta": {
    "hostvars": {
      "localhost": {
        "var1": "example",
      },
      "www.example.com": {
        "ansible_user": "root"
      }
    }
  },
  "all": {
    "hosts": [
      "localhost",
      "www.example.com"
    ]
  }
}

I saw some old bugs opened on this topic, but since those were fixed a long time ago, I assume that in the recent versions of Ansible something about this has changed and the structure provided in the documentation no longer really works and the documentation hasn't been updated.

juresaht2 avatar Nov 10 '23 17:11 juresaht2