[Ansible 2.*] Problem with variables

131 views
Skip to first unread message

George Khan

unread,
Jan 16, 2016, 3:55:53 PM1/16/16
to Ansible Project
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?

Brian Coca

unread,
Jan 18, 2016, 8:27:29 PM1/18/16
to Ansible Project
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}}"



--
Brian Coca

George Khan

unread,
Jan 19, 2016, 2:48:48 AM1/19/16
to Ansible Project
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?


вторник, 19 января 2016 г., 6:27:29 UTC+5 пользователь Brian Coca написал:

George Khan

unread,
Jan 19, 2016, 6:50:56 AM1/19/16
to Ansible Project
I'll must declare it in /etc/ansible/roles/preconf/defaults/main.yml
Thanks to navern ^_^

воскресенье, 17 января 2016 г., 1:55:53 UTC+5 пользователь George Khan написал:

Brian Coca

unread,
Jan 19, 2016, 10:22:55 AM1/19/16
to Ansible Project
you can use 'vars' or 'defaults', but not 'variables:

/etc/ansible/roles/main/{vars,defaults}/main.yml
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/e3cceb37-1ef0-4cc0-b940-203b0e9d1e61%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

George Khan

unread,
Jan 19, 2016, 10:39:37 AM1/19/16
to Ansible Project
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"} 

вторник, 19 января 2016 г., 20:22:55 UTC+5 пользователь Brian Coca написал:
Reply all
Reply to author
Forward
0 new messages