1

I was trying to create users using ansible. I created a variable called users with the list of users and other options inside it. Here it is:

users:
  - { user_name: "user1", sudo_type: "full", password: "pass", ssh_pub_key: "ssh-rsa AAAAB..." }
  - { user_name: "user2", sudo_type: "limited", password: "somepass", ssh_pub_key: "ssh-rsa AAAAB3..." }

I then created the following task:

- name: create local users
  local_action: user name={{ users.user_name }} createhome=yes state=present password={{ users.password }} shell=/bin/bash generate_ssh_key=yes ssh_key_type=rsa ssh_key_comment="{{ ansible_hostname }}-{{ users.user_name }}"

When I ran the playbook, it failed with the following error:

fatal: [10.60.16.1]: 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 'user_name'\n\nThe error appears to have been in '/root/ansible-builds/roles/users/tasks/main.yml': line 11, column 3, 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 local users\n  ^ here\n"}

Where is the issue at?

techraf
  • 64,883
  • 27
  • 193
  • 198
zozo6015
  • 557
  • 2
  • 11
  • 27
  • I rolled back the edits which you made based on the first version of the answer which introduced a different problem. Now the question and the answer match. – techraf Sep 05 '17 at 04:55

1 Answers1

2

users is a list which has no attribute user_name. It has only items and each item has an attribute user_name. You have to loop through the list of users. Something like:

vars:
  users:
    - { user_name: "user1", sudo_type: "full", password: "pass", ssh_pub_key: "ssh-rsa AAAAB..." }
    - { user_name: "user2", sudo_type: "limited", password: "somepass", ssh_pub_key: "ssh-rsa AAAAB3..." }

tasks:
  - name: create local users
    shell: echo {{ item.user_name }}
    with_items: "{{ users }}"

So your task will be like:

  - name: create local users
    local_action: user name={{ item.user_name }} createhome=yes state=present password={{ item.password }} shell=/bin/bash generate_ssh_key=yes ssh_key_type=rsa ssh_key_comment="{{ ansible_hostname }}-{{ item.user_name }}"
    with_items: "{{ users }}"
helloV
  • 50,176
  • 7
  • 137
  • 145
  • I have corrected the playbook but I am still getting the same error. – zozo6015 Sep 04 '17 at 20:28
  • I have just did it – zozo6015 Sep 04 '17 at 20:30
  • Your updated playbook is not same as the one I suggested. Read again. – helloV Sep 04 '17 at 20:32
  • I could not find any different so I have added an extra task with your example which fails with the same error. – zozo6015 Sep 04 '17 at 20:44
  • I am sorry. I do not know the solution. When you posted the question, you did not provide the correct context. For me, it looks like you posted few lines from two different files or from two different places. – helloV Sep 04 '17 at 20:51
  • the users are located in group_vars/all files where documentation say variables should be defined. The task is part of a role. – zozo6015 Sep 04 '17 at 20:59
  • Your suggestion was correct. The only mistake was that the users should've been taken as a variable therefore it had to be put like "{{ users }}". Thanks for the suggestion. – zozo6015 Sep 04 '17 at 21:31
  • 1
    @techraf fixed it. zozo6015 you are correct. See techraf's comment. – helloV Sep 05 '17 at 04:34