how to access dictionary key and value from group_vars it is not working

18 views
Skip to first unread message

Atul Sharma

unread,
Sep 30, 2018, 7:19:02 PM9/30/18
to Ansible Project
group_vars/vars.yml 

[root@master mywork]# cat  group_vars/vars
---
MyVariable:
  var1: 'hello'
  var2: 'world'



inventory file:
[mylocal]
localhost

[docker_con]
con1
con2


[ofsaavar:children]
mylocal
docker_con

[ofsaavar:vars]
ansible_host=con1
ansible_host=con2
ansible_ssh_user=root




to access group_vars dictionary key is not working . please check the following playbook

test.yml 

---
- hosts: docker_con
  remote_user: root
  gather_facts: False

  tasks:
    - debug:
         msg: "{{MyVariable.var1}}"



error:

task path: /root/mywork/stat.yml:9
fatal: [con1]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'MyVariable' is undefined\n\nThe error appears to have been in '/root/mywork/stat.yml': line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - debug:\n      ^ here\n"
}
fatal: [con2]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'MyVariable' is undefined\n\nThe error appears to have been in '/root/mywork/stat.yml': line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - debug:\n      ^ here\n"
}


Kuldip Madnani

unread,
Oct 1, 2018, 12:03:59 AM10/1/18
to Ansible Project
The group_vars directory contains files for each hostgroup defined in your inventory file. The files define variables that member hosts can use. So in your case the file should be group_vars/docker_con.yml
Reply all
Reply to author
Forward
0 new messages