ansible fact availability

Hi all,

I have in my hosts file

COUNTRY_SUFFIX=“{{ansible_hostname}}”

It works fine when I use ansible-playbook and play the book with some roles. But if I run as adhoc command like this

ansible -i hosts hostname -u user -k -mdebug -a’msg={{COUNTRY_SUFFIX}}’

then asible says ansible_hostname is not defined.
anybody knows why?

thanks

ad hoc commands do not gather facts, which is what sets
ansible_hostname, this is an expected result

Sounds like he’s discussing a variable in his inventory file and not a fact, though.

However, it’s more apt to say that template expressions are not evaluated in ad-hoc commands.

Nevermind, this is indeed ansible_hostname, which is not available because it’s a fact – sorry!

micheal's other point still stands, ad-hoc does not do any templating,
so even if you had the var it would not work.

It is not true.
If I run adhoc command and using variable defined in inventory it works fine.

So ansible does not get facts only, but these variable defiend in hosts and host_vars and magic var like inventory_hostname works.

Yeah I think I remember the template commit.

(But it’s not going to gather facts first, because facts are a playbook concept)

The v2/ tree effort is essentially going to make “in memory” playbooks for ad hoc commands so might make this easy with a --gather-facts switch in the future or something like that.

Thank you all for helping …

Yeah it would be great if it gather facts too, but currently I switch to use magic vars which works as well.

cheers