【发布时间】:2016-07-12 10:03:38
【问题描述】:
我正在尝试遍历一个列表,该列表存储在一个字典中,该字典是另一个列表的一部分。我的剧本是这样的:
---
- hosts: all
vars:
copy_certs:
- { domain: 'domainname', copy_to: ['/tmp/foo', '/tmp/bar'], restart: [["mailhost", "postfix"], ["mailhost", "dovecot"]] }
- { domain: 'domainname2', copy_to: ['/tmp/foo2', '/tmp/bar2'], restart: [["mail.lxc", "postfix"]] }
tasks:
[...]
- name: Copy Private Key
register: copied_key
copy: src=/etc/letsencrypt/live/{{ item.0.domain }}/privkey.pem dest="{{ item.1 }}/"
with_subelements:
- copy_certs
- copy_to
- name: Debug (here should be delegates to "item.restart.NUM.0" to restart "item.restart.NUM.1" with_subelements: ...)
debug: var=item
with_items: copied_key.results
现在我正在迭代列表,因为 ansible 似乎不支持真正的嵌套,而只是两个嵌套循环的一些预定义结构。
我需要类似的东西(不起作用):
with_subelements:
- copied_key.results
- item.domain.restart
或(子元素的语法错误):
with_subelements:
- copied_key.results
- item.domain
- restart
我已经尝试过使用冗余列表:
vars:
restart_services:
domainname: [["mailhost", "postfix"]]
tasks:
- name: Debug
debug: var=restart_services[item.item.0.domain]
with_items: copied_key.results
正如你所见,item.item 已经开始变得丑陋了。
它需要像
这样的东西 register: [to_restart for to_restart in item['restart']] as services_to_rstart
- debug: var=item # item.0 = hostname item.1 = servicename
with_items: services_to_restart
或者如果它不需要是列表就可以对其进行迭代,即使是item.hostname 而不是元组(实际上是列表)。
如果能有一些方法来指定带有嵌套的循环,而不是使用像with_subelements 这样的预制过滤器,那就太好了。
【问题讨论】: