dynamic load yaml using include_vars and use these vars later

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 !

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
        Racke

thanks ! it’s working now !

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