Ansible with_dict 需要一个 dict [英] Ansible with_dict expects a dict

查看:34
本文介绍了Ansible with_dict 需要一个 dict的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题以前被问过很多次,但我一定在这里遗漏了一些东西!

I know this question has been asked many times before but I must be missing something here!

这是重现问题的最小剧本.

This is a minimal playbook to reproduce the issue.

这是剧本:

---
- hosts:
    - localhost
  gather_facts: false
  vars:
    zones_hash:
      location1:
        id: 1
        control_prefix: '10.1.254'
        data_prefix: '10.1.100'
      location2:
        id: 2
        control_prefix: '10.2.254'
        data_prefix: '10.2.100'
  tasks:
    - name: "test1"
      debug: var="zones_hash"

    - name: "test2"
      debug: var="item"
      with_dict:
      - "{{ zones_hash }}"

输出如下:

$ ansible --version
ansible 2.3.1.0
  config file = /home/configs/_ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
$ ansible-playbook playbook.yml

PLAY [localhost] *******************************************************************************

TASK [test1] ***********************************************************************************
ok: [localhost] => {
    "zones_hash": {
        "location1": {
            "control_prefix": "10.1.254",
            "data_prefix": "10.1.100",
            "id": 1
        },
        "location2": {
            "control_prefix": "10.2.254",
            "data_prefix": "10.2.100",
            "id": 2
        }
    }
}

TASK [test2] ***********************************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "with_dict expects a dict"}

PLAY RECAP *************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1

我希望 task2 中打印的项目变量包含(例如):

I would expect the item variable printed in task2 to contain (for example):

key: location1
value: {
  id: 1
  control_prefix: '10.1.254'
  data_prefix: '10.1.100'
}

我们缺少什么?

推荐答案

看起来 Ansible 的文档需要更新,或者您发现了一个错误.http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-hashes 使用您的 with_dict 语法,但它似乎不再起作用.字典需要与 with_dict 位于同一行.

Looks like Ansible's documentation needs updated or you've found a bug. http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-hashes uses your with_dict syntax but it seems like that doesn't work anymore. The dictionary needs to be on the same line as with_dict.

- name: "test2"
  debug: var="item"
  with_dict: "{{ zones_hash }}"

这篇关于Ansible with_dict 需要一个 dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆