2014-10-29 17 views
12
--- 
- hosts: test 
    tasks: 
    - name: print phone details 
     debug: msg="user {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})" 
     with_dict: users 
    vars: 
    users: 
     alice: "Alice" 
     telephone: 123 

Quando eseguo questo playbook, sto ottenendo questo errore:Ansible: non può accedere valore dizionario - ha ottenuto l'errore: 'oggetto dict' non ha alcun attributo

One or more undefined variables: 'dict object' has no attribute 'name' 

Questo in realtà funziona bene :

debug: msg="user {{ item.key }} is {{ item.value }}" 

Cosa mi manca?

risposta

9

Questo non è lo stesso codice esatto. Se osservi attentamente l'esempio, vedrai che sotto users hai diversi dict.

Nel tuo caso, si hanno due dicts, ma con un solo tasto (alice, o telephone) con rispettivi valori di "Alice", 123.

si preferisce fare:

- hosts: localhost 
    gather_facts: no 
    tasks: 
    - name: print phone details 
     debug: msg="user {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})" 
     with_dict: users 
    vars: 
    users: 
     alice: 
     name: "Alice" 
     telephone: 123 

(nota che ho cambiato host in localhost così posso eseguirlo facilmente e ho aggiunto gather_facts: no poiché non è necessario qui. YMMV.)

+0

Oh, errore di battitura ... grazie! – user1692261

Problemi correlati