2017-07-28 2 views
2

Ich weiß, dass diese Frage schon oft gestellt wurde, aber ich muss etwas hier fehlen!Ansible with_dict erwartet ein dict

Dies ist ein minimales Playbook, um das Problem zu reproduzieren. Hier

ist das Textbuch:

--- 
- 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 }}" 

Hier ist der Ausgang:

$ 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 

Ich würde das Element variablen erwarten in task2 bedruckenden enthalten (zum Beispiel):

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

Was vermissen wir?

Antwort

2

Sieht so aus, als ob die Dokumentation von Ansible aktualisiert wurde oder Sie einen Fehler gefunden haben. http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-hashes verwendet Ihre with_dict Syntax, aber es scheint, dass das nicht mehr funktioniert. Das Wörterbuch muss in derselben Zeile wie with_dict stehen.

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

Es ist eine Dokumentation Bug - https://github.com/ansible/ansible/issues/17636. – kfreezy

2
with_dict: 
    - "{{ zones_hash }}" 

deklariert eine Liste mit einem dict als erster Index und ansible Recht beanstandet, da sie eine dict erwartet.

Die Lösung kfreezy erwähnten Werke, da es tatsächlich ein Wörterbuch zu with_dict und keine Liste gibt:

with_dict: "{{ zones_hash }}" 
Verwandte Themen