group name or inventory file name in tasks or jinja

1,415 views
Skip to first unread message

Marc Patermann

unread,
Mar 9, 2015, 1:18:37 PM3/9/15
to ansible...@googlegroups.com
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. :)
(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

Dick Davies

unread,
Mar 9, 2015, 4:48:22 PM3/9/15
to ansible list
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.

On 9 March 2015 at 17:18, Marc Patermann
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/54FDD5E2.20907%40ofd-z.niedersachsen.de.
> For more options, visit https://groups.google.com/d/optout.

Serge van Ginderachter

unread,
Mar 9, 2015, 5:35:31 PM3/9/15
to ansible...@googlegroups.com

On 9 March 2015 at 18:18, Marc Patermann <hans....@ofd-z.niedersachsen.de> wrote:
# 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"

​You inventory has webserver-test, not webservers-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"]

Marc Patermann

unread,
Mar 10, 2015, 1:18:04 PM3/10/15
to ansible...@googlegroups.com
Serge,

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

> On 9 March 2015 at 18:18, Marc Patermann
> <hans....@ofd-z.niedersachsen.de
> <mailto:hans....@ofd-z.niedersachsen.de>> wrote:
>
> # 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. :)
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
Reply all
Reply to author
Forward
0 new messages