group name or inventory file name in tasks or jinja

Hi,

I have two inventories, each for test an production. And I call ansible(-playbook) explicitly with one of these, to not mix things up. :slight_smile:
(Things like IP configuration differ from test and production.)
The inventory has group names ending in "-test" or "-prod" too, on which I could check too.

# test:
[webserver-test]
foo1
server2

# production
[webservers-prod]
foo2
server1

How can I check in the task and in jinja templates, if the host is in test or production?

I asked something related in 2013 and got "when: inventory_hostname in groups.webservers-test" as an answer. But this results in a fatal
"error while evaluating conditional: inventory_hostname in groups.webservers-test"

I could set this as a group:var for all, like:
# test
[all:vars]
env="test"

Is this the best way to do this?

Marc

That works fine for me - [all:vars] in an inventory, but I use the
same group names
across all inventories for consistency.

I think this could bite me in the future with dynamic inventories etc.
that may make it
a little harder to do that, but for now it works great.

​You inventory has webserver-test, not webserver*s*-test :)​

But next thing would be, variable names cannot have dashes, so you would
need to rewrite you conditional using a string:

   when: inventory_hostname in groups["webservers-test"]

Serge,

Serge van Ginderachter schrieb (09.03.2015 22:35 Uhr):

    # test:
    [*webserver-tes*t]
    foo1
    server2

    # production
    [webservers-prod]
    foo2
    server1

    How can I check in the task and in jinja templates, if the host is
    in test or production?

    I asked something related in 2013 and got
    â€‹â€‹
    "when: inventory_hostname in groups.webserve*rs*-test" as an answer.
    But this results in a fatal
    "error while evaluating conditional: inventory_hostname in
    groups.webservers-test"

​You inventory has webserver-test, not webserver*s*-test :)​

this have not been real world names, as you can imagine. :slight_smile:
It war an error in the mail, of course.

But next thing would be, variable names cannot have dashes, so you would need to rewrite you conditional using a string:

   when: inventory_hostname in groups["webservers-test"]

OK, thank you.

I like the [all:vars] way better now, I think.

Marc