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 }}"
On Sunday, November 6, 2016 at 3:34:50 AM UTC-5, Kai Stian Olstad wrote:
>
>
> 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/8a7a8526-3c56-4ce7-9475-cdd03fd6f6bf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.