[Ansible 2.*] Problem with variables

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

`

[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:

`

[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?

Many things in just one task.

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

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

should be :
  debug: var=item

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

Also "$vars" were removed a long time ago, it needs to not be:
  with_items:
    - $adm_users
but:
  with_items: "{{adm_users}}"

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?

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

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

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

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