List of list and variables

136 views
Skip to first unread message

Guillaume Querso

unread,
Jul 23, 2015, 4:36:10 AM7/23/15
to Ansible Project
i am not sure it is possible to do so, but i would like to have a list of list. it would look like this:

my_files:
  path: /data/ansible/testing1/
    - {name: 'test1.reg', ext: '.reg'}
    - {name: 'test1.lst', ext: '.lst'}
    - {name: 'test1.sql', ext: '.sql'}
  path: /data/ansible/testing2/
    - {name: 'test2.reg', ext: '.reg'}
    - {name: 'test2.lst', ext: '.lst'}
    - {name: 'test2.xsl', ext: '.xsl'}

and most important, how can i access to those vairables in a playbook? 

Note: i absolutely need to keep this kind of hierarchy with the path and then the files in this path.

Thank you!

Brian Coca

unread,
Jul 28, 2015, 7:23:50 PM7/28/15
to Ansible Project
first this is a list of dictionaries in mixed list/dictionaries which
are part of a dictionary (my_files), access to list is via index [0]
for first item or [1] for second, for dictionaries just use keys. But
I'm not sure this is a proper construct at all.


--
Brian Coca

J Hawkesworth

unread,
Jul 29, 2015, 1:30:55 AM7/29/15
to Ansible Project, gquers...@gmail.com
That's a complicated structure, and you seem to be storing some redundant information.
Just wondering if you can simplify the structure and use filters to derive the ext from the name
See http://docs.ansible.com/ansible/playbooks_filters.html#other-useful-filters - there's a splitext filter which looks appropriate.

Hope that helps,

Jon

Guillaume Querso

unread,
Jul 29, 2015, 4:28:15 AM7/29/15
to Ansible Project, j.r.haw...@googlemail.com
thank you for all your replies. i changed the way i am organizing my file. now i do:
my_files:
  - {path: '/data/ansible/testing1', name: 'test1.reg', ext: '.reg'}
  - {path: '/data/ansible/testing1', name: 'test1.lst', ext: '.lst'}

so it is less readable but easier to access to a variable (item.path, item.name, ...). 

J Hawkesworth

unread,
Jul 29, 2015, 7:41:40 AM7/29/15
to Ansible Project, gquers...@gmail.com
Not really what it is you are trying to achieve, but perhaps you can have a simple structure and use filters to fetch the parts of the full path that you need.

Something like this:

---
- name: Demonstrate splitting paths
  hosts
: localhost
  gather_facts
: false
  vars
:
    my_files
:
     
- /data/ansible/testing1/test.reg
     
- /data/ansible/testing1/test.lst
  tasks
:
     
- name: get directory
       debug
: msg={{item | dirname }}
       with_items
: my_files


     
- name: get basename
       debug
: msg={{item | basename }}
       with_items
: my_files


     
- name: get extention before v2
       debug
: msg={{item.split('.')[1] }}
       with_items
: my_files


Hope this helps

Jon
Reply all
Reply to author
Forward
0 new messages