I am trying to create local users on remote systems using Ansible.
The user list is read from a CSV formatted file using Ansible's read_csv.
The CSV file is formatted in UTF-8 with Byte order mark (BOM).
I am getting the error 'dict object' has no attribute 'Username'.
But 'Username' does in fact exist in the CSV file's header line, and it is the first word of the file.
Property file:
Username,UID
user1,70001
Playbook:
---
- name: Reading users from CSV file
community.general.read_csv:
path: files/users.csv
register: users
delegate_to: localhost
- name: Create from CSV file
user:
name: "{{ item.Username }}"
uid: "{{ item.UID }}"
state: present
loop: "{{ users.list }}"
Error:
TASK [manage_local_users : Create from CSV file] ******************************************************************************************************************************************************************************
task path: /local/ansible/manage-users/playbooks/roles/manage_local_users/tasks/config.yml:12
fatal: [myhost.acme.com]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'Username'
The error appears to be in '/local/ansible/manage-users/playbooks/roles/manage_local_users/tasks/config.yml': line 12, column 3, but maybe elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Create from CSV file
^ here"
}
Output from previous task ("Reading users from CSV file") in -vvvv mode:
ok: [myhost.acme.com -> localhost] => {
"changed": false,
"dict": {},
"invocation": {
"module_args": {
"delimiter": null,
"dialect": "excel",
"fieldnames": null,
"key": null,
"path": "files/users.csv",
"skipinitialspace": null,
"strict": null,
"unique": true
}
},
"list": [
{
"UID": "70001",
"Username": "user1"
}
]
}
I tried:
Running against another host
Create a new property file from scratch
Reduce data in the property file
Tried both
loop: "{{ users.list }}"andwith_dict: "{{ users }}"Reduce number of tasks in the Ansible role
EDIT:
I have figured out that the input csv file has caused this issue.
This is the file causing the issue:
# file files/users_BROKEN.csv
files/users_BROKEN.csv: UTF-8 Unicode (with BOM) text
# hexdump -C files/users_BROKEN.csv
00000000 ef bb bf 55 73 65 72 6e 61 6d 65 2c 55 49 44 0a |...Username,UID.|
00000010 75 73 65 72 31 2c 37 30 30 30 31 0a |user1,70001.|
0000001c
While this file is not causing the issue:
# file files/users_CORRECT.csv
users.csv: ASCII text
# hexdump -C files/users_CORRECT.csv
00000000 55 73 65 72 6e 61 6d 65 2c 55 49 44 0a 75 73 65 |Username,UID.use|
00000010 72 31 2c 37 30 30 30 31 0a |r1,70001.|
00000019