Problem using results of 'find' module...

Hi all,

Trying to use the ‘find’ module to collect a list of files that I will then operate on. To test this out, I wrote a simple playbook as follows:

  • name: test playbook
    #remote_user: root
    hosts: localhost

tasks:

  • name: find all vbox SysV init scripts in /etc/rc[0-6].d
    find:
    paths: “{{ item }}”
    patterns: “^.*virtualbox$”
    use_regex: True
    with_items:

  • /etc/rc0.d

  • /etc/rc1.d

  • /etc/rc2.d

  • /etc/rc3.d

  • /etc/rc4.d

  • /etc/rc5.d

  • /etc/rc6.d
    register: vbox_sysv_scripts

  • debug: var=vbox_sysv_scripts verbosity=2

  • name: Show me the files!
    command: ls -l {{ item.path }}
    with_items: vbox_sysv_scripts.files

However, when I run the playbook, I get an error on the “Show me the files!” task, which uses the result of the find registered var:

bash-4.3$ ansible-playbook -i inventory/testing -k test-find.yml
SSH password:

PLAY [test playbook] ***********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [find all vbox SysV init scripts in /etc/rc[0-6].d] ***********************
ok: [localhost] => (item=/etc/rc0.d)
ok: [localhost] => (item=/etc/rc1.d)
ok: [localhost] => (item=/etc/rc2.d)
ok: [localhost] => (item=/etc/rc3.d)
ok: [localhost] => (item=/etc/rc4.d)
ok: [localhost] => (item=/etc/rc5.d)
ok: [localhost] => (item=/etc/rc6.d)

TASK [debug] *******************************************************************
skipping: [localhost]

TASK [Show me the files!] ******************************************************
fatal: [localhost]: FAILED! => {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘unicode object’ has no attribute ‘path’\n\nThe error appears to have been in ‘/home/istgroup/Documents/ansible-stuff/test-find.yml’: line 24, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Show me the files!\n ^ here\n”}

If I print the “vbox_sysv_scripts” var via the debug statement, I get the following:
https://gist.githubusercontent.com/wdennis/13197fe430a66da28b03de809474c294/raw/ae9d77ddc64bc3c3f55f46e0a7469bb5d1848de6/vbox_sysv_scripts

Can someone tell me where I’m going wrong here?

When using with_ and register the result is always a list stored in the <variable>.results, for you that is vbox_sysv_scripts.results, as shown by you debug

   ok: [localhost] => {
     "vbox_sysv_scripts": {
       "changed": false,
       "msg": "All items completed",
       "results": [
         {
           "_ansible_item_result": true,
           "_ansible_no_log": false,
           "_ansible_parsed": true,
           "changed": false,
           "examined": 17,
           "files": [

The find module add all the files it find per item to a list stored in
vbox_sysv_scripts.results.0.files for the fist item
vbox_sysv_scripts.results.1.files for the second item
vbox_sysv_scripts.results.2.files for the third item and so on.

To get the paths you will have to use the with_subelements: to get them in your "Show me the files!"
https://docs.ansible.com/ansible/playbooks_loops.html#looping-over-subelements

   - name: Show me the files!
     command: ls -l {{ item.1.path }}
     with_subelements:
       - "{{ vbox_sysv_scripts.results }}"
       - files

An alternative is to use a list in the module finds paths, then your "Show me the files!" will work

   - name: find all vbox SysV init scripts in /etc/rc[0-6].d
     find:
       paths: ['/etc/rc0.d', '/etc/rc1.d', '/etc/rc2.d',
               '/etc/rc3.d', '/etc/rc4.d', '/etc/rc5.d', '/etc/rc6.d']
       patterns: "^.*virtualbox$"
       use_regex: True
     register: vbox_sysv_scripts

Thanks, Kai, for your kind help. I’ve never had to use “with_subelements” before, and so it didn’t come to mind… I did see that there was a “results” list in the vbox_sysv_scripts var debug output, and thought that I may have to specify vbox_sysv_scripts.results to get at the files, but didn’t know how Ansible operates on that sort of structure… Also did not know that you could pass the file mod’s paths parameter a list, that’s pretty cool. So in any case, I rewrote the playbook as follows, which works:

`

  • name: test playbook
    #remote_user: root
    hosts: localhost

tasks:

  • name: find all vbox SysV init scripts in /etc/rc[0-6].d
    find:
    patterns: “^.*virtualbox$”
    use_regex: True
    paths:
    [ ‘/etc/rc0.d’,
    ‘/etc/rc1.d’,
    ‘/etc/rc2.d’,
    ‘/etc/rc3.d’,
    ‘/etc/rc4.d’,
    ‘/etc/rc5.d’,
    ‘/etc/rc6.d’ ]
    register: vbox_sysv_scripts

  • debug: var=vbox_sysv_scripts verbosity=2

  • name: Show me the files!
    command: ls -l {{ item.path }}
    with_items: “{{ vbox_sysv_scripts.files }}”
    `