diff --git a/changelogs/fragments/getent-empty-split-fail-json.yml b/changelogs/fragments/getent-empty-split-fail-json.yml new file mode 100644 index 00000000000..947cb261e9e --- /dev/null +++ b/changelogs/fragments/getent-empty-split-fail-json.yml @@ -0,0 +1,2 @@ +bugfixes: + - getent - handle non-empty string for split parameter value (https://github.com/ansible/ansible/issues/85720). diff --git a/lib/ansible/modules/getent.py b/lib/ansible/modules/getent.py index e195b7ef7ea..f08fc33b6e4 100644 --- a/lib/ansible/modules/getent.py +++ b/lib/ansible/modules/getent.py @@ -36,6 +36,7 @@ options: description: - Character used to split the database values into lists/arrays such as V(:) or V(\\t), otherwise it will try to pick one depending on the database. + - The value must be a non-empty string. type: str fail_key: description: @@ -148,6 +149,9 @@ def main(): if service is not None: cmd.extend(['-s', service]) + if not split and split is not None: + module.fail_json(msg="Invalid split value. The value must be a non-empty string") + if split is None and database in colon: split = ':' diff --git a/test/integration/targets/getent/tasks/main.yml b/test/integration/targets/getent/tasks/main.yml index bd17bd62587..c596fd27b50 100644 --- a/test/integration/targets/getent/tasks/main.yml +++ b/test/integration/targets/getent/tasks/main.yml @@ -1,20 +1,7 @@ # Test code for the getent module. # (c) 2017, James Tanner +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . - name: check for getent command shell: which getent failed_when: False @@ -30,17 +17,34 @@ service: files register: getent_test0 when: ansible_system != 'FreeBSD' and ansible_distribution != 'Alpine' + - name: run getent w/o specified service (FreeBSD) getent: database: passwd key: root register: getent_test0 when: ansible_system == 'FreeBSD' or ansible_distribution == 'Alpine' + - debug: var=getent_test0 + - name: validate results assert: that: - 'getent_passwd is defined' - 'getent_passwd.root is defined' - 'getent_passwd.root|length == 6' + + - name: run getent with invalid split value + getent: + database: group + split: "" + register: getent_test1 + ignore_errors: True + + - name: validate results + assert: + that: + - 'getent_test1 is failed' + - 'getent_test1.msg is contains "Invalid split value. The value must be a non-empty string"' + when: getent_check.rc == 0