2

I try to master the work with dictionaries at ansible. When I include dict to playbook as at example all works like charm. But when I try to include vars block to my host_vars/myserv.yml file like this:

host_vars/myserv.yml <- There is wrong syntax don't use this example!

  vars:
    users:
      alice:
        name: Alice Appleworth
        telephone: 123-456-7890
      bob:
        name: Bob Bananarama
        telephone: 987-654-3210

playbook.yml


- name: "myserv"
  hosts: "myserv"
  gather_facts: yes
  remote_user: root

  tasks:

  - name: "Ping hosts"
    ping:
  
  roles:
   - {role: 'test-role'}

roles/test-role/task/main.yml

- name: Print phone records
  debug:
    msg: "User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
  loop: "{{ lookup('dict', users) }}"

I get an error:

FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'dict'. Error was a <class 'ansible.errors.AnsibleError'>, original message: with_dict expects a dict"}

So what I missed? I need to use some lookup option?

UPD: the trouble was pretty simple - just remove "vars:" line from host_vars file and all work's fine :).

Regards

Kein
  • 121
  • 3
  • 14
  • 2
    Glad that you solved it. Please post your solution as an answer and accept it, don't edit your question. Otherwise the question is marked as "unsolved" in the system and pops up again and again. – Gerald Schneider Mar 12 '19 at 10:23

1 Answers1

1

UPD: the trouble was pretty simple - just remove "vars:" line from host_vars file and all work's fine :).

Kein
  • 121
  • 3
  • 14