Creating users & groups with vars - not working

51 views
Skip to first unread message

Anfield

unread,
Jul 6, 2017, 10:01:29 PM7/6/17
to Ansible Project


Trying to create users and groups with vars inline...this is not working..what am I missing here?

--- # Users and Groups
- hosts: localhost
  become: yes
  vars:
     groups:
        - devops
        - dbadbmins
        - serveradmins

     users:
        - frank
        - joe
        - dave
  tasks:
    - name: Create groups
      group:
        name: "{{ item.groups }}"
        state: present
      with_items: "{{groups}}"
      ignore_errors: yes

    - name: Create users
      user:
        name: "{{ item.user }}"
        state: present
      with_items: "{{users}}"

Output -

PLAY [localhost] *********************************************************************

TASK [Gathering Facts] ***************************************************************
ok: [localhost]

TASK [Create groups] *****************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'groups'\n\nThe error appears to have been in '/etc/ansible/playbooks/usersgroups.yml': line 15, 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    - name: Create groups\n      ^ here\n"}
...ignoring

TASK [Create users] ******************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'user'\n\nThe error appears to have been in '/etc/ansible/playbooks/usersgroups.yml': line 22, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Create users\n      ^ here\n"}
        to retry, use: --limit @/etc/ansible/playbooks/usersgroups.retry





Daniel JD

unread,
Jul 7, 2017, 4:35:50 AM7/7/17
to Ansible Project
You are trying to access item.groups, but there is not item.groups. The groupname is item itself. The same thing for item.user!

Just change item.groups to item and item.user to item

Brian Coca

unread,
Jul 7, 2017, 9:20:01 AM7/7/17
to Ansible Project
To put Daniel's response into clear example:

- name: Create groups
group:
name: "{{ item }}"
state: present
with_items: "{{groups}}"
ignore_errors: yes

- name: Create users
user:
name: "{{ item }}"
state: present
with_items: "{{users}}"

----------
Brian Coca

Daniel JD

unread,
Jul 7, 2017, 2:39:54 PM7/7/17
to Ansible Project
Thanks Brian!

Anfield

unread,
Jul 7, 2017, 3:24:48 PM7/7/17
to Ansible Project
Thanks. It works but only if I swap   with_items: "{{groups}}"   for with_items: "{{grouplist}}" 

and call my vars grouplist also. Something it didnt like about calling those 2 groups


Brian Coca

unread,
Jul 7, 2017, 6:22:57 PM7/7/17
to Ansible Project
i should have noticed, 'groups' is a reserved var, which has a
dictionary with the inventory groups and the hosts that belong to
them.

No matter what you set, the 'groups' from Ansible will overwrite it.
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages