dynamic load yaml using include_vars and use these vars later

14 views
Skip to first unread message

Eric Chang

unread,
Feb 14, 2020, 2:39:39 AM2/14/20
to Ansible Project
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 !

Stefan Hornburg (Racke)

unread,
Feb 14, 2020, 5:08:30 AM2/14/20
to ansible...@googlegroups.com
Try "{{ vars[item]['name'] }}". Only variables inside the double curly braces are evaluated.

Regards
Racke

>
> --
> 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
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/d9fd2b5f-d144-4136-8f6a-692d37960602%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/d9fd2b5f-d144-4136-8f6a-692d37960602%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Eric Chang

unread,
Feb 14, 2020, 8:08:50 PM2/14/20
to Ansible Project
thanks ! it's working now !

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

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

>Regards
>       Racke

>
> --
> 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
Reply all
Reply to author
Forward
0 new messages