how to deference data from registered variables with loop

16 views
Skip to first unread message

Dale Sykora

unread,
Nov 27, 2018, 9:37:39 AM11/27/18
to Ansible Project
 I am trying to process a list of subfolders, but am having trouble dereferencing the data
If I run the following playbook

---
- name: Get folders under c:\Users
  hosts: ychtrn1d
  gather_facts: false
  tasks:
    - name: Determine which folders exist under Users
      win_find:
        paths: 'c:\Users\'
        file_type: directory
      register: folders
    - debug:
         msg: "{{ folders }}"

I get this (some data removed for brevity)

ok: [ychtrn1d] => {
    "msg": {
        "changed": false,
        "failed": false,
        "files": [
            {
                "creationtime": 1415372291.3851476,
                "filename": "administrator",
                "size": 3009061740
            },
            {
                "creationtime": 1247523608.929812,
                "filename": "Public",
                "size": 120599529
            },
            {
                "creationtime": 1415371442.8504908,
                "filename": "test",
                "size": 70521
            }
        ],
        "matched": 3
    }
}

I want to loop through the 3 filename values, so I change playlist to

---
- name: Get folders under c:\Users
  hosts: ychtrn1d
  gather_facts: false
  tasks:
    - name: Determine which folders exist under Users
      win_find:
        paths: 'c:\Users\'
        file_type: directory
      register: folders
    - debug:
         msg: "{{ item }}"
      loop: "{{ folders.files.filename }}"

This fails

fatal: [ychtrn1d]: FAILED! => {"msg": "'list object' has no attribute 'filename'"}

However, changing to

      loop: "{{ folders.files }}"

does return data, just more than I need

I am familiar with perl arrays/hashes but not in ansible/yaml

How can I return only "filename" data?



        

Kai Stian Olstad

unread,
Nov 27, 2018, 11:02:12 AM11/27/18
to ansible...@googlegroups.com
loop takes a list and folders.files.filename is not a list but folders.files is so change to this

- debug:
msg: "{{ item.filename }}"
loop: "{{ folders.files }}"

--
Kai Stian Olstad


Dale Sykora

unread,
Nov 27, 2018, 11:26:25 AM11/27/18
to Ansible Project
Thanks Kai!!!
That worked perfectly. 
Reply all
Reply to author
Forward
0 new messages