0

I'm working on a small ansible project for myself.

I have a main.yml in the defaults folder of my role.

Structure:

master-folder
 `- roles
     `- some_role
         |- tasks
         |   `- main.yml
         `- defaults
             `- main.yml

Now... In my some_variables.yml I have a dictionary with 2 vars.

parent_var:
  child_var: bob
  child_two_var: bobby

in my main.yml task I want to get the key and value so (child_var: bob) and write it to another file.

What I need help with, is with getting the key and value from the main.yml in the defaults folder.

zigarn
  • 10,892
  • 2
  • 31
  • 45
Shai Shvili
  • 99
  • 1
  • 1
  • 6

1 Answers1

0

Any variable defined in the defaults/*.yml is accessible from any task of the role with {{tree.to.variable}}.

So in your case, you can simply access the {{parent_var}}.

If you want to loop over keys and values, you can use the with_dict loop.

- lineinfile:
    line: "Value for '{{ item.key }}' is '{{ item.value }}'"
    dest: /tmp/test/file1
  with_dict: "{{ parent_var }}"

If you need to loop in a template, there is the {% for %} in jinja2.

- copy:
    dest: /tmp/test/file
    content: |
      {% for k,v in parent_var.iteritems() %}
      Value for '{{ k }}' is '{{ v }}'
      {% endfor %}
zigarn
  • 10,892
  • 2
  • 31
  • 45