On 06. nov. 2016 04:18, Willard Dennis wrote:
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


--
Kai Stian Olstad

--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/2e159dc1-70ff-ca1e-2eaf-672190de4023%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to