Ansible動的インベントリJSONでホスト名に基づいてホスト変数を「レンダリング」できますか?

Ansible動的インベントリJSONでホスト名に基づいてホスト変数を「レンダリング」できますか?

Ansible ドキュメントは例を提供します。ここJSON形式で在庫を返す方法:

{
    "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" ]
}

以下に追加するには、以下を使用して単一ホストのホスト変数を設定できます。

{

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

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

}

今私はAnsible 1.9.1を使用しており、ホスト変数または単一のホストを使用したいと思います。ただし、一部のホスト変数は特定のパターンに従います。最も顕著なパターンは、ワイルドカードが短いホスト名に置き換えられるansible_ssh_hostパターンに従います。*.mydomain.tld

Jinja2テンプレートでレンダリングされるスキーマを提供してJSONを短縮する方法はありますか?上記の例の一部を調整します。

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

何もない良いAnsibleが期待するフォーマットは可能ですか?これを言及する文書が見つかりませんでした。

ベストアンサー1

あなたはそれを使用することができますinventory_hostname マジック変数この場合。

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

おすすめ記事