Playbook error "list object has no attribute" (vars, lists, dictionaries)

4,884 views
Skip to first unread message

Rob Dumas

unread,
Jun 5, 2017, 6:37:46 PM6/5/17
to Ansible Project
I'm trying to set up a few users on a Linux box using Ansible and I keep running into a weird error. First, here's the playbook:

---
- name: common/users | ensure presence, gid of default group
  group: 
    name: "{{ default_group.name }}"
    gid: "{{ default_group.gid }}"
    state: present

- name: common/users | ensure presence, sudo privileges of admin users
  user:
    name: "{{ default_users.username }}"
    uid: "{{ default_users.uid }}"
    group: "{{ default_group.name }}"
    groups: sudo
    state: present

and here's the `group_vars/all` file:

---
default_group: 
  gid: 2000
  name: groupname
default_users: 
  - username: user1
    uid: 2001
  - username: user2
    uid: 2002

However, when I run the playbook, I get the following error:

fatal: [vm-server]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears
to include a variable that is undefined. The error was: 'list object' has no attribute 'username'

The error appears to have been in '/vagrant/roles/common/tasks/users.yml': line 8, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: common/users | ensure presence, sudo privileges of admin users
  ^ here"}


My list object "default_users" is supposed to set up two users and each one definitely contains the attribute "username", so why is it refusing to create the users and giving me this error? My YAML seems to follow the syntax and it passes YAMLlint with no problem.

Matt Martz

unread,
Jun 5, 2017, 6:58:23 PM6/5/17
to ansible...@googlegroups.com
default_users is a list, you need to loop over it.  Look into with_items


--
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/09b3b7b9-763f-40f4-8fb3-6c005c51aee2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Matt Martz
@sivel
sivel.net

Rob Dumas

unread,
Jun 5, 2017, 9:15:54 PM6/5/17
to Ansible Project
So I can't loop over variables like this?

Matt Martz

unread,
Jun 5, 2017, 9:26:32 PM6/5/17
to ansible...@googlegroups.com
Like what?  You aren't looping anything in those tasks you showed.  You'll need to use with_items to actually do a loop.



For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages