diff --git a/changelogs/fragments/user-local-warning-fix.yaml b/changelogs/fragments/user-local-warning-fix.yaml new file mode 100644 index 00000000000..1155fce3b93 --- /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 f05bab8f570..e096250d7a7 100644 --- a/lib/ansible/modules/system/user.py +++ b/lib/ansible/modules/system/user.py @@ -869,9 +869,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 3438929e33c..5563fe187a0 100644 --- a/test/integration/targets/user/tasks/main.yml +++ b/test/integration/targets/user/tasks/main.yml @@ -872,6 +872,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 @@ -952,10 +960,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