dict2items需要字典

时间:2019-04-17 08:32:50

标签: ansible

我有以下值列表:

bind_zones:
- "test.com.":
    ttl: 3h
    allow_transfer:
    - "0.0.0.0/24"
    masters:
    - dnsmaster.test.com
    records:
    .....

我现在尝试像这样调用此值列表:

- name: configure zone databases
  template:
    src: zonefile.j2
    dest: "{{bind_db_dir}}/db.{{item.key|regex_replace('^(.*)\\.$', '\\1')}}"
    owner: "{{bind_user}}"
    group: "{{bind_group}}"
    mode: 0640
    validate: "named-checkzone -d {{item.key|regex_replace('^(.*)\\.$', '\\1.')}} %s"
  when: inventory_hostname in item.value.masters|default([])
  loop: "{{ bind_zones|dict2items }}"
  loop_control:
    label: "{{item.key}}"
  notify: reload-bind

哪个错误并失败

  

dict2items需要字典,取而代之。

然后在我的用例中使用此方法的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

如果dict2items需要字典,让我们给它提供字典。下面的播放符合预期

vars:
  bind_zones:
    test.com.:
      ttl: 3h
      allow_transfer:
        - "0.0.0.0/24"
      masters:
        - dnsmaster.test.com
    test.org.:
      ttl: 3h
      allow_transfer:
        - "0.0.0.0/24"
      masters:
        - dnsmaster.test.org
tasks:
  - debug:
      var: item
    loop: "{{ bind_zones|dict2items }}"
  - debug:
      var: item
    with_dict: "{{ bind_zones }}"

播放中的循环是等效的。