ansible / ansible-documentation

Ansible community documentation
https://docs.ansible.com/
GNU General Public License v3.0
84 stars 505 forks source link

JSON example in script inventory development guide is wrong #811

Open juresaht2 opened 1 year ago

juresaht2 commented 1 year ago

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.

samccann commented 1 year ago

@juresaht2 - the snippet from docs in your description doesn't match the actual documentation at that location:

{

    # results of inventory script as above go here
    # ...

    "_meta": {
        "hostvars": {
            "host001": {
                "var001" : "value"
            },
            "host002": {
                "var002": "value"
            }
        }
    }
}

Have you tried this ^^ to see if it works?

juresaht2 commented 1 year ago

I guess... you didn't read the entire document on the link? Or something?

The example you provided only shows how to use _meta. What is required is a skeleton of the entire structure so that the inventory can be provided. Simply providing _meta without defining at least one group, such as all does not work.

This is what the document actually says, and the skeleton provided for it is the example I provided, which does not work.