User modification task --check fails if user already exists but its groups do not
Hi !
New Ansible user here, nice to meet you ![]()
My problem
ansible-core version used: 2.19.8
My playbook creates groups and creates (or modifies) users on Linux remote servers:
- it loops on a CSV file
- creates groups with ansible.builtin.group
- then creates (or modifies ) the users with ansible.builtin.user
Everything was nice and fancy until I had to add existing users to new groups.
The playbook --check has a wierd behaviour on the user task:
- it succeeds if the user and groups do not already exist (both are flagged as changed)
- it fails if the user already exists but at least one of the groups doesnât, with the message:
Group <whatever> does not exist
I feel like this behaviour isnât normal : the --check should flag the user task as a change whether the user already exists or not.
Steps to reproduce
- create a test_existinguser on a target server
- run a playbook with the following tasks in
--checkmode against the target server:
tasks:
- name: CREATE NEW GROUP
ansible.builtin.group:
name: test_newgroup
state: present
- name: CREATE NEW USER AND ADD IT TO THE NEW GROUP
ansible.builtin.user:
name: test_newuser
groups: test_newgroup
- name: MODIFY EXISTING USER BY ADDING IT TO THE NEW GROUP
ansible.builtin.user:
name: test_existinguser
groups: test_newgroup
- outputs to this:
TASK [CREATE NEW GROUP] **************************************************************************************************************************
changed: [<target_server>]
TASK [CREATE NEW USER] ***************************************************************************************************************************
changed: [<target_server>]
TASK [CHANGE EXISTING USER] **********************************************************************************************************************
fatal: [<target_server>]: FAILED! => {"changed": false, "msg": "Group test_newgroup does not exist"}
PLAY RECAP ***************************************************************************************************************************************
<target_server> : ok=3 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Debug tentative
The Group does not exist message:
- has multiple instances in user.py/modify_user_usermod()
- in my case it seems to be outputted by
useraddwhich is called by ansible/lib/ansible/modules/user.py at a7fcd92081e3182825e74eb857dfa5bb0f8d5fcc ¡ ansible/ansible ¡ GitHub
The useradd source shows it returns this message from itâs error 6 :
#define E_NOTFOUND 6 /* specified user/group doesn't exist */
grp = prefix_getgr_nam_gid (optarg);
if (NULL == grp) {
fprintf (stderr,
_("%s: group '%s' does not exist\n"),
Prog, optarg);
exit (E_NOTFOUND);
}