2017-07-28 3 views
2

Je sais que cette question a été posée plusieurs fois auparavant, mais il me manque quelque chose ici!Ansible with_dict attend un dict

Ceci est un playbook minimal pour reproduire le problème.

Voici le PlayBook:

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

est ici la sortie:

$ 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 

je me attends à la variable d'élément imprimé à task2 contenir (par exemple):

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

Qu'est-ce qui nous manque?

Répondre

2

On dirait que la documentation d'Ansible a besoin d'être mise à jour ou que vous avez trouvé un bug. http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-hashes utilise votre syntaxe with_dict mais il semble que cela ne marche plus. Le dictionnaire doit être sur la même ligne que with_dict.

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

Il est un bug de documentation - https://github.com/ansible/ansible/issues/17636. – kfreezy

2
with_dict: 
    - "{{ zones_hash }}" 

déclare une liste avec un dict comme le premier indice, et à juste titre Ansible se plaint car il attend un dict.

La kfreezy solution mentionnée travaux, car il donne en fait un dictionnaire à with_dict et non une liste:

with_dict: "{{ zones_hash }}"