diff --git a/changelogs/fragments/user-local-warning-fix.yaml b/changelogs/fragments/user-local-warning-fix.yaml new file mode 100644 index 0000000000..1155fce3b9 --- /dev/null +++ b/changelogs/fragments/user-local-warning-fix.yaml @@ -0,0 +1,2 @@ +bugfixes: + - 'user - do not warn when using ``local: yes`` if user already exists (https://github.com/ansible/ansible/issues/58063)' diff --git a/lib/ansible/modules/system/user.py b/lib/ansible/modules/system/user.py index 7dfbcdb5d0..dc00c32d03 100644 --- a/lib/ansible/modules/system/user.py +++ b/lib/ansible/modules/system/user.py @@ -863,9 +863,11 @@ class User(object): exists = True break - self.module.warn( - "'local: true' specified and user was not found in {file}. " - "The local user account may already exist if the local account database exists somewhere other than {file}.".format(file=self.PASSWORDFILE)) + if not exists: + self.module.warn( + "'local: true' specified and user '{name}' was not found in {file}. " + "The local user account may already exist if the local account database exists " + "somewhere other than {file}.".format(file=self.PASSWORDFILE, name=self.name)) return exists diff --git a/test/integration/targets/user/tasks/main.yml b/test/integration/targets/user/tasks/main.yml index 798f3aa80d..ab49d0d3dc 100644 --- a/test/integration/targets/user/tasks/main.yml +++ b/test/integration/targets/user/tasks/main.yml @@ -829,6 +829,14 @@ tags: - user_test_local_mode +- name: Create local account that already exists to check for warning + user: + name: root + local: yes + register: local_existing + tags: + - user_test_local_mode + - name: Create local_ansibulluser user: name: local_ansibulluser @@ -909,10 +917,12 @@ tags: - user_test_local_mode -- name: Ensure warnings were displayed +- name: Ensure warnings were displayed properly assert: that: - local_user_test_1['warnings'] | length > 0 - - "'user was not found in /etc/passwd. The local user account may already exist if the local account - database exists somewhere other than /etc/passwd.' in local_user_test_1['warnings'][0]" + - local_user_test_1['warnings'] | first is search('The local user account may already exist') + - local_existing['warnings'] is not defined when: ansible_facts.system in ['Linux'] + tags: + - user_test_local_mode