鉴于以下剧本:
--- - 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 does not exists or it is not a symlink." failed_when: > log_dir.results[0].stat.islnk is 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
使用以下变量:
--- wl_instances: - name: aservers servers: - AdminServer - name: mservers servers: - "{{ ansible_hostname }}"
第二个任务目前只使用两个可能的结果之一(results[0]
).
我的问题是:如何迭代存储的所有可用项log_dir.results
?
示例输出debug:hostvars[inventory_hostname]
如下:
"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", ...
小智.. 7
循环遍历数组中的结果(由[]表示),将完成为
with_items: somelist
或者如果它是包含列表的字典,就像在这种情况下一样
with_items: log_dir.results
请注意,这也可以写
with_items: log_dir['results']
所以在你的任务中
- name: Check if log directory exists - Step 2 fail: msg="Log directory does not exists or it is not a symlink." failed_when: > item.stat.islnk is not defined or item.stat.islnk != true or item..stat.lnk_source != "{{ wl_base }}/logs/{{ wl_dom }}/{{ item.1 }}" with_items: log_dir.results
有关更多信息和示例,请访问http://docs.ansible.com/playbooks_loops.html#standard-loops.
这里的主要内容是您只想访问已注册变量的一部分.
循环遍历数组中的结果(由[]表示),将完成为
with_items: somelist
或者如果它是包含列表的字典,就像在这种情况下一样
with_items: log_dir.results
请注意,这也可以写
with_items: log_dir['results']
所以在你的任务中
- name: Check if log directory exists - Step 2 fail: msg="Log directory does not exists or it is not a symlink." failed_when: > item.stat.islnk is not defined or item.stat.islnk != true or item..stat.lnk_source != "{{ wl_base }}/logs/{{ wl_dom }}/{{ item.1 }}" with_items: log_dir.results
有关更多信息和示例,请访问http://docs.ansible.com/playbooks_loops.html#standard-loops.
这里的主要内容是您只想访问已注册变量的一部分.