# \[Ansible 2.\*\] Problem with variables

**URL:** <https://forum.ansible.com/t/ansible-2-problem-with-variables/20236>\
**Category:** Ansible Project\
**Created:** [January 16, 2016, 10:53am UTC](https://forum.ansible.com/t/ansible-2-problem-with-variables/20236 "2016-01-16T10:53:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![George\_Khan](https://avatars.discourse-cdn.com/v4/letter/g/a587f6/32.png) [@George\_Khan](https://forum.ansible.com/u/George_Khan)\
**Post date:** [January 16, 2016, 10:53am UTC](https://forum.ansible.com/t/ansible-2-problem-with-variables/20236/1 "2016-01-16T10:53:49Z")

</div>

Hi there! I have some troubles with vars in ansible…  
What i have:

`

```auto
[root@ansible ansible]# cat /etc/ansible/roles/preconf/tasks/main.yml 
- name: installing pkg
  apt: pkg=$item
  with_items:
    - htop
    - sudo
    - tcpdump
    - mc
    - wget
    - vim
    - facter

- name: Add users
# user: name={{item.user}} comment={{item.comment}} uid={{item.uid}} password={{item.password}} shell='/bin/bash' groups='sudo'
  debug: msg={{item.user}} msg={{item.comment}} msg={{item.uid}} msg={{item.password}}
  with_items:
    - $adm_users
        
        
[root@ansible ansible]# cat /etc/ansible/roles/main/variables/main.yml
adm_users:
 - { user: 'user1', comment: 'user11', uid: '15139', password: '$gsdJDGASgdashdakshdg' }
 - { user: 'user2', comment: 'user22', uid: '15217', password: '$tERuJGEAgdfSAGDVaGV' }

```

`

What i do:

`

```auto
[root@ansible ansible]# time ansible-playbook -vvvv main.yml --ask-pass --ask-sudo-pass
Using /etc/ansible/ansible.cfg as config file
SSH password:
SUDO password[defaults to SSH password]:
1 plays in main.yml
Loaded callback default of type stdout, v2.0

PLAY ***************************************************************************

TASK [setup] *******************************************************************...
ok: [192.168.0.2]

TASK [preconf : installing pkg] ************************************************...
ok: [192.168.0.2] => (item=[u'htop', u'sudo', u'tcpdump', u'mc', u'wget', u'vim', u'facter']) => {"cache_update_time": 0, "cache_updated": false, "changed": false, "invocation": {"module_args": {"name": ["htop", "sudo", "tcpdump", "mc", "wget", "vim", "facter"]}, "module_name": "apt"}, "item": ["htop", "sudo", "tcpdump", "mc", "wget", "vim", "facter"]}

TASK [preconf : Add users] *****************************************************
task path: /etc/ansible/roles/preconf/tasks/main.yml:12
fatal: [192.168.0.2]: FAILED! => {"failed": true, "msg": "ERROR! 'unicode object' has no attribute 'password'"}

PLAY RECAP *********************************************************************
192.168.0.2 : ok=2 changed=0 unreachable=0 failed=1   

real 0m3.745s
user 0m0.357s
sys 0m0.100s

```

`

Ansible version:

`

ansible 2.0.0.2 (stable-2.0.0.1 7de237c5a1) last updated 2016/01/16 10:27:31 (GMT +500)  
lib/ansible/modules/core: (detached HEAD ce6619bf5d) last updated 2016/01/16 10:27:57 (GMT +500)  
lib/ansible/modules/extras: (detached HEAD 29af26884e) last updated 2016/01/16 10:28:02 (GMT +500)  
config file = /etc/ansible/ansible.cfg  
configured module search path = Default w/o overrides

`

What i’m doing wrong?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 19, 2016, 1:27am UTC](https://forum.ansible.com/t/ansible-2-problem-with-variables/20236/2 "2016-01-19T01:27:25Z")

</div>

Many things in just one task.

first debug only takes 1 msg= or 1 var= argument, so instead of:

&nbsp;&nbsp;debug: msg={{item.user}} msg={{item.comment}} msg={{item.uid}}  
msg={{item.password}}

should be :  
&nbsp;&nbsp;debug: var=item

or:  
&nbsp;&nbsp;debug: msg="{{item.user}}, {{item,comment}}, {{item.uid}} ..."  
or:  
&nbsp;&nbsp;debug: msg="{{[item,user, item.comment, item.uid,...]|join(',')}}"

Also "$vars" were removed a long time ago, it needs to not be:  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- $adm\_users  
but:  
&nbsp;&nbsp;with\_items: "{{adm\_users}}"

---

<div class="post-metadata">

**Author:** ![George\_Khan](https://avatars.discourse-cdn.com/v4/letter/g/a587f6/32.png) [@George\_Khan](https://forum.ansible.com/u/George_Khan)\
**Post date:** [January 19, 2016, 7:48am UTC](https://forum.ansible.com/t/ansible-2-problem-with-variables/20236/3 "2016-01-19T07:48:48Z")

</div>

Hi, Brian! Thank you for your answer!  
In this case, i got “FAILED! =\> {“failed”: true, “msg”: “ERROR! ‘adm\_users’ is undefined”}”. I declare my vars in /etc/ansible/roles/main/variables/main.yml , maybe i must declare variables somewhere else?

---

<div class="post-metadata">

**Author:** ![George\_Khan](https://avatars.discourse-cdn.com/v4/letter/g/a587f6/32.png) [@George\_Khan](https://forum.ansible.com/u/George_Khan)\
**Post date:** [January 19, 2016, 11:50am UTC](https://forum.ansible.com/t/ansible-2-problem-with-variables/20236/4 "2016-01-19T11:50:55Z")

</div>

I’ll must declare it in /etc/ansible/roles/preconf/defaults/main.yml  
Thanks to navern 🙂

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 19, 2016, 3:22pm UTC](https://forum.ansible.com/t/ansible-2-problem-with-variables/20236/5 "2016-01-19T15:22:50Z")

</div>

you can use 'vars' or 'defaults', but not 'variables:

/etc/ansible/roles/main/{vars,defaults}/main.yml

---

<div class="post-metadata">

**Author:** ![George\_Khan](https://avatars.discourse-cdn.com/v4/letter/g/a587f6/32.png) [@George\_Khan](https://forum.ansible.com/u/George_Khan)\
**Post date:** [January 19, 2016, 3:39pm UTC](https://forum.ansible.com/t/ansible-2-problem-with-variables/20236/6 "2016-01-19T15:39:37Z")

</div>

Brian, i try to use /etc/ansible/roles/main/vars/main.yml but all that i got: FAILED! =\> {“failed”: true, “msg”: “ERROR! ‘adm\_users’ is undefined”}
