Q: Obscure standard variables?

31 views
Skip to first unread message

Sieds Pallas

unread,
Sep 5, 2017, 12:59:01 PM9/5/17
to Ansible Project
Hi,
I am working trhough some tutorials, and I have a question; I hope this is the proper forum to post it.
In the solution provided with the tutorial I see some variables used that I do not understand:

    inventory_hostname
and:
    groups["webservers"]

I do not find these in the ansible facts (`ansible servera -m setup`).
Also I cannot evaluate them using JINJA2 template "{{ inventory_hostname }}", apparently they are something else.
"webservers" is a group in my inventory, but where is the hash "groups" defined or coming from?

The documentation appears to be silent about these things.

On a related note, how am I supposed to evaluate variables from the facts?  e.g. "{{ ansible_memory_mb}}" gives an error stating that I am not allowed to use JINJA2 templates here.

Tom

Tom Bartsch

unread,
Sep 6, 2017, 2:02:33 AM9/6/17
to Ansible Project
Hi,

groups["webservers"] means that you have to define a group named webservers within your hosts file.

inventory_hostname is the name of the hostname as configured in Ansible's inventory hosts file.

Regards,
Tom

Sieds Pallas

unread,
Sep 6, 2017, 3:34:48 AM9/6/17
to Ansible Project
Hi Tom,
I suspected that much.  My questions are:
- Where are these coming from?  I do not find documented that if you make a section [webservers] in your inventory, that automagically an array "groups[]" is created that has "webservers" as a member. Nor that there is such a thing as inventory_hostname. In my inventory file I know I can specify ungrouped hosts and named groups of hosts: but I find no references to the  variable inventory_hostname. From the way it is used in the examples I gather it is the current hostname that ansible is working on plucked from the inventory.
- Also these variables appear to be used differently as other variables, i.e. not with "{{ }}", so they are really odd. What syntax do we need to use for which items?


Op woensdag 6 september 2017 08:02:33 UTC+2 schreef Tom Bartsch:

Sieds Pallas

unread,
Sep 8, 2017, 3:39:35 AM9/8/17
to Ansible Project
A colleague of mine explained that these variables are created when the inventory is read.
You can get to see those with:
ansible <host-pattern> -m debug -a "var=vars"
This "vars" appears to be undocumented.
HTH

Op woensdag 6 september 2017 09:34:48 UTC+2 schreef Sieds Pallas:
Reply all
Reply to author
Forward
0 new messages