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