diff --git a/changelogs/fragments/6848-npm-required-if.yml b/changelogs/fragments/6848-npm-required-if.yml new file mode 100644 index 0000000000..4356f53700 --- /dev/null +++ b/changelogs/fragments/6848-npm-required-if.yml @@ -0,0 +1,2 @@ +minor_changes: + - npm - minor improvement on parameter validation (https://github.com/ansible-collections/community.general/pull/6848). diff --git a/plugins/modules/npm.py b/plugins/modules/npm.py index 013fd6e570..3ad2a64793 100644 --- a/plugins/modules/npm.py +++ b/plugins/modules/npm.py @@ -285,7 +285,8 @@ def main(): arg_spec['global'] = dict(default=False, type='bool') module = AnsibleModule( argument_spec=arg_spec, - supports_check_mode=True + required_if=[('state', 'absent', ['name'])], + supports_check_mode=True, ) name = module.params['name'] @@ -304,8 +305,6 @@ def main(): if not path and not glbl: module.fail_json(msg='path must be specified when not using global') - if state == 'absent' and not name: - module.fail_json(msg='uninstalling a package is only available for named packages') npm = Npm(module, name=name, path=path, version=version, glbl=glbl, production=production, executable=executable, registry=registry, ignore_scripts=ignore_scripts, diff --git a/tests/integration/targets/npm/tasks/test.yml b/tests/integration/targets/npm/tasks/test.yml index c8e83f6027..0ab8a98d22 100644 --- a/tests/integration/targets/npm/tasks/test.yml +++ b/tests/integration/targets/npm/tasks/test.yml @@ -12,10 +12,10 @@ # sample: node-v8.2.0-linux-x64.tar.xz node_path: '{{ remote_dir }}/{{ nodejs_path }}/bin' package: 'iconv-lite' + environment: + PATH: '{{ node_path }}:{{ ansible_env.PATH }}' block: - shell: npm --version - environment: - PATH: '{{ node_path }}:{{ ansible_env.PATH }}' register: npm_version - debug: @@ -24,11 +24,8 @@ - name: 'Install simple package without dependency' npm: path: '{{ remote_dir }}' - executable: '{{ node_path }}/npm' state: present name: '{{ package }}' - environment: - PATH: '{{ node_path }}:{{ ansible_env.PATH }}' register: npm_install - assert: @@ -39,11 +36,8 @@ - name: 'Reinstall simple package without dependency' npm: path: '{{ remote_dir }}' - executable: '{{ node_path }}/npm' state: present name: '{{ package }}' - environment: - PATH: '{{ node_path }}:{{ ansible_env.PATH }}' register: npm_reinstall - name: Check there is no change @@ -60,11 +54,8 @@ - name: 'reinstall simple package' npm: path: '{{ remote_dir }}' - executable: '{{ node_path }}/npm' state: present name: '{{ package }}' - environment: - PATH: '{{ node_path }}:{{ ansible_env.PATH }}' register: npm_fix_install - name: Check result is changed and successful