-1

I have a dictionary of vars in an Ansible role ( in roles/my_role/vars/main.yml ):

my_vars:
  - name: var1
    string: var1_string
  - name: var1
    string: var1_string

I want to include all of these in a single file constructed with a jinja2 templates loop:

{% for v in my_vars %}
    "{{ v.string }}"
{% endfor %}

Will this work? Can the J2 templating engine refer to vars in roles/my_role/vars/main.yml in this way? Or is it limited to globals vars and vars specific to the play the initiates the templating engine?

Garreth McDaid
  • 3,449
  • 1
  • 27
  • 42
  • 3
    Have you tried that? I guess it would have taken less time than write this question. – Konstantin Suvorov Dec 06 '17 at 14:53
  • I asked the question before I started testing, so that if my testing did not go well, I might have an answer sooner that if I asked after testing. If my testing worked, I would have created an answer for someone else, given that I wasn't able to find answer anywhere else. I guess it would have taken you less time to not post a worthless comment than post one. – Garreth McDaid Dec 06 '17 at 15:18

1 Answers1

1

Yes, this will work. The Jinja2 templating engine can access dictionary vars that originate in the role.

There is no need to specify

with_items

in the play.

Garreth McDaid
  • 3,449
  • 1
  • 27
  • 42