# dynamic load yaml using include\_vars and use these vars later

**URL:** <https://forum.ansible.com/t/dynamic-load-yaml-using-include-vars-and-use-these-vars-later/32231>\
**Category:** Ansible Project\
**Created:** [February 14, 2020, 7:39am UTC](https://forum.ansible.com/t/dynamic-load-yaml-using-include-vars-and-use-these-vars-later/32231 "2020-02-14T07:39:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Chang](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@Eric\_Chang](https://forum.ansible.com/u/Eric_Chang)\
**Post date:** [February 14, 2020, 7:39am UTC](https://forum.ansible.com/t/dynamic-load-yaml-using-include-vars-and-use-these-vars-later/32231/1 "2020-02-14T07:39:39Z")

</div>

I have dozens of yaml files , and I want to use these yaml files as vars in playbook.  
so I try to dynamic load vars like

aaa.yaml , bbb.yaml,ccc.yaml

- name: get all yaml files name  
shell: find /home/yaml -name \*.yamlawk -F “/” ‘{print $6}’|awk -F “.” ‘{print $1}’  
register: yaml\_files

- name: load omdata  
include\_vars:  
name: “{{ item }}”  
file: “/home/yaml/{{ item }}.yml”  
with\_items: “{{ yam\_files.stdout\_lines }}”  
register: “{{ item }}”

due to these 2 tasks , I should able to get many vars like

aaa: { ‘name’: AAA , id=‘111’ }

bbb: { ‘name’: BBB , id=‘222’ }

with debug , I can confirm those vars are been loaded.

- name: debug yaml\_files  
debug:  
var: “{{ item }}”  
with\_items: “{{ yaml\_files.stdout\_lines }}”

and now , how could I get the vaule in those var files ?

I try to do so by

- name: create yaml\_file  
shell: echo “{{ item }}.name”|tee -a /tmp/tmp.txt  
with\_items: “{{ yaml\_files.stdout\_lines }}”

I though “{{ item }}.name” will get aaa.name which is AAA  
but instead , I get “aaa.name” in tmp.txt

any suggestions will be appreciated , thanks !

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 14, 2020, 10:07am UTC](https://forum.ansible.com/t/dynamic-load-yaml-using-include-vars-and-use-these-vars-later/32231/2 "2020-02-14T10:07:59Z")

</div>

> I have dozens of yaml files , and I want to use these yaml files as vars in playbook.  
> so I try to dynamic load vars like
> 
> aaa.yaml , bbb.yaml,ccc.yaml
> 
> - name: get all yaml files name  
> shell: find /home/yaml -name \*.yamlawk -F "/" '{print $6}'|awk -F "." '{print $1}'  
> register: yaml\_files
> 
> - name: load omdata  
> include\_vars:  
> name: "{{ item }}"  
> file: "/home/yaml/{{ item }}.yml"  
> with\_items: "{{ yam\_files.stdout\_lines  
> }}"  
> register: "{{ item }}"
> 
> due to these 2 tasks , I should able to get many vars like
> 
> aaa: { 'name': AAA , id='111' }  
> bbb: { 'name': BBB , id='222' }
> 
> with debug , I can confirm those vars are been loaded.
> 
> - name: debug yaml\_files  
> debug:  
> var: "{{ item }}"  
> with\_items: "{{ yaml\_files.stdout\_lines }}"
> 
> and now , how could I get the vaule in those var files ?
> 
> I try to do so by
> 
> - name: create yaml\_file  
> shell: echo "{{ item }}.name"|tee -a /tmp/tmp.txt  
> with\_items: "{{ yaml\_files.stdout\_lines }}"
> 
> I though "{{ item }}.name" will get aaa.name which is AAA  
> but instead , I get "aaa.name" in tmp.txt
> 
> any suggestions will be appreciated , thanks !

Try "{{ vars[item]['name'] }}". Only variables inside the double curly braces are evaluated.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Eric\_Chang](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@Eric\_Chang](https://forum.ansible.com/u/Eric_Chang)\
**Post date:** [February 15, 2020, 1:08am UTC](https://forum.ansible.com/t/dynamic-load-yaml-using-include-vars-and-use-these-vars-later/32231/3 "2020-02-15T01:08:50Z")

</div>

thanks ! it’s working now !

Stefan Hornburg (Racke)於 2020年2月14日星期五 UTC+8下午6時08分30秒寫道：
