The name of the current inventory file in use is stored in the
"inventory_file" variable name, so you could check that. Beyond that, there
are two other options:

1. Use --extra-vars to specify the target environment, for example:
--extra-vars="inventory=qa"
2. The group_vars location is based on the location of the inventory file,
so you could do something like this:

qa/group_vars/all:
inventory=qa

prod/group_vars/all:
inventory=production

The qa/hosts and prod/hosts would contain your different inventories, so
you'll get output like this:

$ cat play.yml
- hosts: all
  gather_facts: no
  tasks:
  - name: show the variable
    debug: var=inventory

$ ansible-playbook play.yml -i prod/hosts

PLAY [all]
********************************************************************

TASK: [show the variable]
*****************************************************
ok: [you] => {
    "inventory": "production"
}

PLAY RECAP
********************************************************************
you                        : ok=1    changed=0    unreachable=0    failed=0


$ ansible-playbook play.yml -i qa/hosts

PLAY [all]
********************************************************************

TASK: [show the variable]
*****************************************************
ok: [me] => {
    "inventory": "qa"
}

PLAY RECAP
********************************************************************
me                         : ok=1    changed=0    unreachable=0    failed=0



Hope that helps.



On Mon, May 5, 2014 at 4:26 PM, Drew Gulino <[email protected]> wrote:

> As suggested in the best practices doc, have two inventory files: qa and
> production
>
> I call these using the -i parameter:
>
> ansible-playbook -i qa ...
>
> Is there a way I can determine in my playbook which inventory I am using?
>  The use case is I want to notify one chat room if using qa inventory,
> another if I use production.
>
>
> I've tried just putting a variable in the root of the inventory file qa:
>
> inventory=qa
> [webservers]
> host1
>
> but that doesn't work: is not displayed as a variable with: "ansible host1
> -m setup -i qa"
>
> I can define a variable for each host like:
>
> [webservers]
> host1 inventory=qa
>
> but that is terribly redundant.
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/e5606bef-1a43-40a1-b34a-42c0866cd9ce%40googlegroups.com<https://groups.google.com/d/msgid/ansible-project/e5606bef-1a43-40a1-b34a-42c0866cd9ce%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAMFyvFicOE8hQ%2Bs27cBqG4S5a7c%2BV4if-ZrzbSYPj1u9eBp2Cg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to