group_vars are applied to individual hosts and is not available globally
without a host context.  To use a variable for hosts it must be global and
provided before parsing the play.  Such as providing it via --extra-vars on
the command line.

On Friday, December 11, 2015, Alexandr Kurilin <a...@frontrowed.com> wrote:

> Hey folks,
>
> Ansible 1.9.2 here. I've been recently working on moving us over from
> static inventories to using the ec2 script. One constraint I have is that I
> separate different environments within the same AWS account by the
> Environment tag. In this case I'm working on Environment=dev, which in my
> file tree has its own folder (see tree at the bottom). The problem I'm
> having is that I can't figure out how to set {{ env }} in my *- hosts:*
> from my all.yml file in group_vars. It looks like group_vars as NOT read at
> the time of the playbook yml file templating, but only afterwards. As an
> example, assuming this all.yml:
>
> ---
>
> env: dev
>
> and this foo.yml:
>
> ---
>
> - hosts: "{{ 'tag_Role_utilities:&tag_Environment_' + env }}"
>   tasks:
>     - debug: var=env
>
> this playbook will not work:
>
> ansible-playbook -i inventories/dev/ec2.py foo.yml
>
> PLAY [{{ 'tag_Role_utilities:&tag_Environment_' + env }}]
> *********************
> skipping: no hosts matched
>
> but if I pass the env variable as --extra-vars, then it will:
>
> ansible-playbook -i inventories/dev/ec2.py foo.yml --extra-vars="env=dev"
>
> PLAY [tag_Role_utilities:&tag_Environment_dev]
> ********************************
>
> GATHERING FACTS
> ***************************************************************
> ok: [172.31.57.124]
>
> TASK: [debug var=env]
> *********************************************************
> ok: [172.31.57.124] => {
>     "var": {
>         "env": "dev"
>     }
> }
>
> PLAY RECAP
> ********************************************************************
> 172.31.57.124              : ok=2    changed=0    unreachable=0    failed=0
>
>
> Note that here the value of "env" in the debug module is actually
> correctly populated from the all.yml group_vars file. e.g. if I rename that
> var name in both the debug module and all.yml to something like
> inventory_env, it will get the value correctly.
>
> Is this by design? Is there no way for me to template the tags in *-
> host:* without using an --extra-vars? Any way I can use group_vars for
> playbook templating? It would save me the need to write my own wrapper
> script around ansible that would set --extra-vars for me for each separate
> environment I have.
>
> My inventories/dev tree structure is as follows:
>
> dev
> ├── ec2.ini
> ├── ec2.py
> ├── group_vars
> │   ├── all
> │   │   ├── all.yml
> │   │   └── secrets.yml
> │   ├── apps
> │   └── utils
> └── hosts
>
>
> Thank you.
>
> --
> 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-project+unsubscr...@googlegroups.com
> <javascript:_e(%7B%7D,'cvml','ansible-project%2bunsubscr...@googlegroups.com');>
> .
> To post to this group, send email to ansible-project@googlegroups.com
> <javascript:_e(%7B%7D,'cvml','ansible-project@googlegroups.com');>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/b76bb9ce-174f-440e-8ddb-e56773555f4d%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/b76bb9ce-174f-440e-8ddb-e56773555f4d%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>


-- 
Matt Martz
@sivel
sivel.net

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAD8N0v-KWQqH0zHsa7SPjd4Phfqr5zBCAU-xGx3X2t_1kkVjkg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to