2

The Ansible documentation gives an example here on how to return an inventory in JSON format:

{
    "databases"   : {
        "hosts"   : [ "host1.example.com", "host2.example.com" ],
        "vars"    : {
            "a"   : true
        }
    },
    "webservers"  : [ "host2.example.com", "host3.example.com" ],
    "atlanta"     : {
        "hosts"   : [ "host1.example.com", "host4.example.com", "host5.example.com" ],
        "vars"    : {
            "b"   : false
        },
        "children": [ "marietta", "5points" ]
    },
    "marietta"    : [ "host6.example.com" ],
    "5points"     : [ "host7.example.com" ]
}

and adding below that, that the hostvars for a single host can be set using:

{

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

    "_meta" : {
       "hostvars" : {
          "moocow.example.com"     : { "asdf" : 1234 },
          "llama.example.com"      : { "asdf" : 5678 },
       }
    }

}

Now I'm using Ansible 1.9.1 and would like to use hostvars or individual hosts. However, some of the hostvars follow a pattern. Most prominently ansible_ssh_host will follow a pattern *.mydomain.tld, there the wildcard gets replaced by the short hostname.

Is there a way to shorten the JSON by giving a pattern that will be rendered as a Jinja2 template, for example? Adjusting part of the above example:

{
    "atlanta"     : {
        "hosts"   : [ "host1", "host4", "host5" ],
        "vars"    : {
            "ansible_ssh_host" : "{{hostname}}.example.com",
            "b"   : false
        }
}

Anything like that possible with the format Ansible expects? I didn't find documentation mentioning this.

1 Answer 1

4

You can use inventory_hostname magic variables in this case.

{
    "atlanta"     : {
        "hosts"   : [ "host1", "host4", "host5" ],
        "vars"    : {
            "ansible_ssh_host" : "{{inventory_hostname}}.example.com",
            "b"   : false
        }
}

You must log in to answer this question.

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