ansible -m debug -a "var=hostvars[inventory_hostname]"
動作するようです。有効な変数のソース(host_vars
、group_vars
、_meta
動的在庫、等)は、すべての考慮されます。
動的インベントリスクリプトの場合hosts.sh
:
#!/bin/sh
if test "$1" = "--host"; then
echo {}
else
cat <<EOF
{
"ungrouped": [ "x.example.com", "y.example.com" ],
"group1": [ "a.example.com" ],
"group2": [ "b.example.com" ],
"groups": {
"children": [ "group1", "group2" ],
"vars": { "ansible_ssh_user": "user" }
},
"_meta": {
"hostvars": {
"a.example.com": { "ansible_ssh_host": "10.0.0.1" },
"b.example.com": { "ansible_ssh_host": "10.0.0.2" }
}
}
}
EOF
fi
得られる:
$ chmod +x hosts.sh
$ ansible -i hosts.sh a.example.com -m debug -a "var=hostvars[inventory_hostname]"
a.example.com | success >> {
"var": {
"hostvars": {
"ansible_ssh_host": "10.0.0.1",
"ansible_ssh_user": "user",
"group_names": [
"group1",
"groups"
],
"groups": {
"all": [
"x.example.com",
"y.example.com",
"a.example.com",
"b.example.com"
],
"group1": [
"a.example.com"
],
"group2": [
"b.example.com"
],
"groups": [
"a.example.com",
"b.example.com"
],
"ungrouped": [
"x.example.com",
"y.example.com"
]
},
"inventory_hostname": "a.example.com",
"inventory_hostname_short": "a"
}
}
}
2.0.2
では、これはもう機能しないようです。出力はlocalhost | SUCCESS => { "hostvars": "<ansible.vars.hostvars.HostVars object at 0x7f320943da10>" }