inventory name as variable

Hi,

I created two inventories: "test" and "production".
test includes groups like "webserver-test", as production are "webserver-prod".

A configuration file differs from test to production.

I created files like files/test-conf.file and files/prod-conf.file and want to use the test-* files for inventory test and prod-* for production.
Can I check for the inventory name a host is in or is there a better way to achieve what I want?

Marc

The best thing to do is to use groups and put your hosts in different groups.

when: inventory_hostname in groups.webservers

Or even better, target with the play:

hosts: production:&webservers

This says target webservers and machines that are in production.

This is also equivalent to

hosts: webservers

with --limit production

on the CLI

In 1.3, there is also a variable “inventory_file” that will tell you the path to the file

however you don’t really want to do that unless you need to know the path, paths shouldn’t be significant.

Michael,

Michael DeHaan schrieb (14.08.2013 13:14 Uhr):

The best thing to do is to use groups and put your hosts in different groups.

I like this one:

when: inventory_hostname in groups.webservers

This playbook fails with
"TASK: [copy linux-init.ldif test]

Sounds like you don’t have a group named webservers and should use another name.

Michael,

Michael DeHaan schrieb (14.08.2013 19:52 Uhr):

Sounds like you don't have a group named webservers and should use another name.

my group is "lsm-test" which works in hosts:

    "---
    - hosts:
      - lsm-test
      - lsm-prod
      gather_facts: no

      tasks:
      - name: stop slapd
        service: name=ldap state=stopped
        register: slapd_is_stopped

      - name: copy linux-init.ldif test
        copy:
              src=files/test-linux-init.ldif
              dest=/etc/openldap/linux-init.__ldif
        when: inventory_hostname in groups.lsm-test
    ..."

Marc

And you referenced “webservers” above, which is the source of the error then :slight_smile:

Michael DeHaan schrieb (15.08.2013 14:30 Uhr):

And you referenced "webservers" above, which is the source of the error then :slight_smile:

this was only a cite from your mail, an example. That may caused the misunderstanding.
My playbook is below the error message in the same mail. There is no "webservers" in there.

Marc

Ok, please make sure there is a bug filed in github and we’ll take a look!