diff --git a/changelogs/fragments/9063-django-version.yml b/changelogs/fragments/9063-django-version.yml new file mode 100644 index 0000000000..3d0287a756 --- /dev/null +++ b/changelogs/fragments/9063-django-version.yml @@ -0,0 +1,5 @@ +minor_changes: + - django module utils - always retrieve version (https://github.com/ansible-collections/community.general/pull/9063). + - django_check - add return value ``version`` (https://github.com/ansible-collections/community.general/pull/9063). + - django_command - add return value ``version`` (https://github.com/ansible-collections/community.general/pull/9063). + - django_createcachetable - add return value ``version`` (https://github.com/ansible-collections/community.general/pull/9063). diff --git a/plugins/module_utils/django.py b/plugins/module_utils/django.py index 5fb375c6fd..8314ed945e 100644 --- a/plugins/module_utils/django.py +++ b/plugins/module_utils/django.py @@ -32,6 +32,7 @@ _django_std_arg_fmts = dict( verbosity=cmd_runner_fmt.as_opt_val("--verbosity"), no_color=cmd_runner_fmt.as_fixed("--no-color"), skip_checks=cmd_runner_fmt.as_bool("--skip-checks"), + version=cmd_runner_fmt.as_fixed("--version"), ) _django_database_args = dict( @@ -60,6 +61,9 @@ class _DjangoRunner(PythonRunner): ) return super(_DjangoRunner, self).__call__(args_order, output_process, ignore_value_none, check_mode_skip, check_mode_return, **kwargs) + def bare_context(self, *args, **kwargs): + return super(_DjangoRunner, self).__call__(*args, **kwargs) + class DjangoModuleHelper(ModuleHelper): module = {} @@ -98,16 +102,20 @@ class DjangoModuleHelper(ModuleHelper): arg_formats=self.arg_formats, venv=self.vars.venv, check_rc=True) + + run_params = self.vars.as_dict() + if self._check_mode_arg: + run_params.update({self._check_mode_arg: self.check_mode}) + + rc, out, err = runner.bare_context("version").run() + self.vars.version = out.strip() + with runner() as ctx: - run_params = self.vars.as_dict() - if self._check_mode_arg: - run_params.update({self._check_mode_arg: self.check_mode}) results = ctx.run(**run_params) self.vars.stdout = ctx.results_out self.vars.stderr = ctx.results_err self.vars.cmd = ctx.cmd - if self.verbosity >= 3: - self.vars.run_info = ctx.run_info + self.vars.set("run_info", ctx.run_info, verbosity=3) return results diff --git a/plugins/modules/django_check.py b/plugins/modules/django_check.py index 1553da7a30..7a12ec94e2 100644 --- a/plugins/modules/django_check.py +++ b/plugins/modules/django_check.py @@ -8,48 +8,49 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type DOCUMENTATION = """ +--- module: django_check author: - - Alexei Znamensky (@russoz) +- Alexei Znamensky (@russoz) short_description: Wrapper for C(django-admin check) version_added: 9.1.0 description: - - This module is a wrapper for the execution of C(django-admin check). +- This module is a wrapper for the execution of C(django-admin check). extends_documentation_fragment: - - community.general.attributes - - community.general.django +- community.general.attributes +- community.general.django options: database: description: - - Specify databases to run checks against. - - If not specified, Django will not run database tests. + - Specify databases to run checks against. + - If not specified, Django will not run database tests. type: list elements: str deploy: description: - - Include additional checks relevant in a deployment setting. + - Include additional checks relevant in a deployment setting. type: bool default: false fail_level: description: - - Message level that will trigger failure. - - Default is the Django default value. Check the documentation for the version being used. + - Message level that will trigger failure. + - Default is the Django default value. Check the documentation for the version being used. type: str choices: [CRITICAL, ERROR, WARNING, INFO, DEBUG] tags: description: - - Restrict checks to specific tags. + - Restrict checks to specific tags. type: list elements: str apps: description: - - Restrict checks to specific applications. - - Default is to check all applications. + - Restrict checks to specific applications. + - Default is to check all applications. type: list elements: str notes: - - The outcome of the module is found in the common return values RV(ignore:stdout), RV(ignore:stderr), RV(ignore:rc). - - The module will fail if RV(ignore:rc) is not zero. +- The outcome of the module is found in the common return values RV(ignore:stdout), RV(ignore:stderr), RV(ignore:rc). +- The module will fail if RV(ignore:rc) is not zero. attributes: check_mode: support: full @@ -58,6 +59,7 @@ attributes: """ EXAMPLES = """ +--- - name: Check the entire project community.general.django_check: settings: myproject.settings @@ -65,18 +67,25 @@ EXAMPLES = """ - name: Create the project using specific databases community.general.django_check: database: - - somedb - - myotherdb + - somedb + - myotherdb settings: fancysite.settings pythonpath: /home/joedoe/project/fancysite venv: /home/joedoe/project/fancysite/venv """ RETURN = """ +--- run_info: description: Command-line execution information. type: dict returned: success and C(verbosity) >= 3 +version: + description: Version of Django. + type: str + returned: always + sample: 5.1.2 + version_added: 10.0.0 """ from ansible_collections.community.general.plugins.module_utils.django import DjangoModuleHelper diff --git a/plugins/modules/django_command.py b/plugins/modules/django_command.py index dcb8d26313..2d6d36ad74 100644 --- a/plugins/modules/django_command.py +++ b/plugins/modules/django_command.py @@ -8,16 +8,17 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type DOCUMENTATION = """ +--- module: django_command author: - - Alexei Znamensky (@russoz) +- Alexei Znamensky (@russoz) short_description: Run Django admin commands version_added: 9.0.0 description: - - This module allows the execution of arbitrary Django admin commands. +- This module allows the execution of arbitrary Django admin commands. extends_documentation_fragment: - - community.general.attributes - - community.general.django +- community.general.attributes +- community.general.django attributes: check_mode: support: none @@ -26,17 +27,18 @@ attributes: options: command: description: - - Django admin command. It must be a valid command accepted by C(python -m django) at the target system. + - Django admin command. It must be a valid command accepted by C(python -m django) at the target system. type: str required: true extra_args: type: list elements: str description: - - List of extra arguments passed to the django admin command. + - List of extra arguments passed to the django admin command. """ EXAMPLES = """ +--- - name: Check the project community.general.django_command: command: check @@ -51,10 +53,17 @@ EXAMPLES = """ """ RETURN = """ +--- run_info: description: Command-line execution information. type: dict returned: success and O(verbosity) >= 3 +version: + description: Version of Django. + type: str + returned: always + sample: 5.1.2 + version_added: 10.0.0 """ import shlex diff --git a/plugins/modules/django_createcachetable.py b/plugins/modules/django_createcachetable.py index b038e0358f..85f5774294 100644 --- a/plugins/modules/django_createcachetable.py +++ b/plugins/modules/django_createcachetable.py @@ -8,17 +8,18 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type DOCUMENTATION = """ +--- module: django_createcachetable author: - - Alexei Znamensky (@russoz) +- Alexei Znamensky (@russoz) short_description: Wrapper for C(django-admin createcachetable) version_added: 9.1.0 description: - - This module is a wrapper for the execution of C(django-admin createcachetable). +- This module is a wrapper for the execution of C(django-admin createcachetable). extends_documentation_fragment: - - community.general.attributes - - community.general.django - - community.general.django.database +- community.general.attributes +- community.general.django +- community.general.django.database attributes: check_mode: support: full @@ -27,6 +28,7 @@ attributes: """ EXAMPLES = """ +--- - name: Create cache table in the default database community.general.django_createcachetable: settings: myproject.settings @@ -40,10 +42,17 @@ EXAMPLES = """ """ RETURN = """ +--- run_info: description: Command-line execution information. type: dict returned: success and O(verbosity) >= 3 +version: + description: Version of Django. + type: str + returned: always + sample: 5.1.2 + version_added: 10.0.0 """ from ansible_collections.community.general.plugins.module_utils.django import DjangoModuleHelper diff --git a/tests/unit/plugins/modules/test_django_check.yaml b/tests/unit/plugins/modules/test_django_check.yaml index 91a8ff1953..74374c01c9 100644 --- a/tests/unit/plugins/modules/test_django_check.yaml +++ b/tests/unit/plugins/modules/test_django_check.yaml @@ -7,11 +7,18 @@ - id: success input: settings: whatever.settings + output: + version: "5.1.2" mocks: run_command: - - command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings] + - command: [/testbin/python, -m, django, --version] environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} rc: 0 + out: "5.1.2\n" + err: "" + - command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings] + environ: *env-def + rc: 0 out: "whatever\n" err: "" - id: multiple_databases @@ -20,8 +27,15 @@ database: - abc - def + output: + version: "5.1.2" mocks: run_command: + - command: [/testbin/python, -m, django, --version] + environ: *env-def + rc: 0 + out: "5.1.2\n" + err: "" - command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings, --database, abc, --database, def] environ: *env-def rc: 0 diff --git a/tests/unit/plugins/modules/test_django_command.yaml b/tests/unit/plugins/modules/test_django_command.yaml index 2a19351083..960dc1a24f 100644 --- a/tests/unit/plugins/modules/test_django_command.yaml +++ b/tests/unit/plugins/modules/test_django_command.yaml @@ -15,9 +15,14 @@ settings: whatever.settings mocks: run_command: - - command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings, babaloo, yaba, daba, doo] + - command: [/testbin/python, -m, django, --version] environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} rc: 0 + out: "5.1.2\n" + err: "" + - command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings, babaloo, yaba, daba, doo] + environ: *env-def + rc: 0 out: "whatever\n" err: "" - id: command_fail @@ -33,6 +38,11 @@ failed: true mocks: run_command: + - command: [/testbin/python, -m, django, --version] + environ: *env-def + rc: 0 + out: "5.1.2\n" + err: "" - command: [/testbin/python, -m, django, check, --no-color, --settings=whatever.settings, babaloo, yaba, daba, doo] environ: *env-def rc: 1 diff --git a/tests/unit/plugins/modules/test_django_createcachetable.yaml b/tests/unit/plugins/modules/test_django_createcachetable.yaml index 22b7dcb304..a58146144a 100644 --- a/tests/unit/plugins/modules/test_django_createcachetable.yaml +++ b/tests/unit/plugins/modules/test_django_createcachetable.yaml @@ -9,8 +9,13 @@ settings: whatever.settings mocks: run_command: + - command: [/testbin/python, -m, django, --version] + environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + rc: 0 + out: "5.1.2\n" + err: "" - command: [/testbin/python, -m, django, createcachetable, --no-color, --settings=whatever.settings, --noinput, --database=default] - environ: {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true} + environ: *env-def rc: 0 out: "whatever\n" err: ""