On Tue, 17 Mar 2020 12:21:25 -0700 (PDT)
Jason Bullen <[email protected]> wrote:

> file dev.yml
> dev:
>   accesspolicies:
>     - policy1
>     - policy2
> 
> file prod.yml
> prod:
>   accesspolicies:
>     - policy1
>
> [...]
> vars_prompt:
>   - name: env
>     prompt: "Environment"
> tasks:
>   - include_vars:
>     file: "{{ env }}.yml"
>   - debug:
>     msg: "{{ env }}.accesspolicies"
> 
> Is there a way to do this better?

"lookup" plugin "vars" helps to reference the variable. For example

  shell> cat play.yml
  - hosts: localhost
    vars_prompt:
      - name: env
        private: false
        prompt: "Environment"
    tasks:
      - include_vars: "{{ env }}.yml"
      - set_fact:
          my_env: "{{ lookup('vars', env ) }}"
      - debug:
          var: my_env.accesspolicies

gives

  shell> ansible-playbook play.yml
  Environment: dev
  ...
  TASK [debug] ***
  ok: [localhost] => {
      "my_env.accesspolicies": [
          "policy1", 
          "policy2"
      ]
  }

HTH,
        -vlado

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20200317215646.2051bd6a%40gmail.com.

Attachment: pgpRG4Fse0wu_.pgp
Description: OpenPGP digital signature

Reply via email to