loops - ansible: how to iterate over all registered results? -
given following playbook:
--- - name: check if log directory exists - step 1 stat: path="{{ wl_base }}/{{ item.0.name }}/{{ wl_dom }}/servers/{{ item.1 }}/logs" get_md5=no register: log_dir with_subelements: - wl_instances - servers - name: check if log directory exists - step 2 fail: msg="log directory not exists or not symlink." failed_when: > log_dir.results[0].stat.islnk not defined or log_dir.results[0].stat.islnk != true or log_dir.results[0].stat.lnk_source != "{{ wl_base }}/logs/{{ wl_dom }}/{{ item.1 }}" with_subelements: - wl_instances - servers
that using following vars:
--- wl_instances: - name: aservers servers: - adminserver - name: mservers servers: - "{{ ansible_hostname }}"
the second task uses 1 of 2 possible results (results[0]
).
my question is: how iterate on available items stored in log_dir.results
?
a sample output debug:hostvars[inventory_hostname]
follows:
"log_dir": { "changed": false, "msg": "all items completed", "results": [ { "changed": false, "invocation": { "module_args": "path=\"/path/to/servers/aservers/domain/adminserver/logs\" get_md5=no", "module_name": "stat" }, "item": [ { "name": "aservers" }, "adminserver" ], "stat": { ... "lnk_source": "/path/to/logs/domain/adminserver", ... } }, { "changed": false, "invocation": { "module_args": "path=\"/path/to/servers/mservers/domain/servers/some_hostname/logs\" get_md5=no", "module_name": "stat" }, "item": [ { "name": "mservers" }, "some_hostname" ], "stat": { ... "lnk_source": "/path/to/logs/domain/some_hostname", ...
looping on results in array (denoted []), done as
with_items: somelist
or if it's dict contains list, in case
with_items: log_dir.results
note can written
with_items: log_dir['results']
so in task
- name: check if log directory exists - step 2 fail: msg="log directory not exists or not symlink." failed_when: > item.stat.islnk not defined or item.stat.islnk != true or item..stat.lnk_source != "{{ wl_base }}/logs/{{ wl_dom }}/{{ item.1 }}" with_items: log_dir.results
more information , examples available in http://docs.ansible.com/playbooks_loops.html#standard-loops.
the main thing here you're wanting access part of registered variable.
Comments
Post a Comment