Repository: ansible/ansible
Pull request: yum action plugin: 'use' as an alias of 'use_backend'
##### SUMMARY
Made changes to the yum action plugin to support **use** as an alias of **use_backend**.
Fixes #70774 

##### ISSUE TYPE
- Bugfix Pull Request

##### COMPONENT NAME
yum action plugin


Problem statement:
error while installing packages in ec2 RHEL instance using yum module
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Explain the problem briefly below -->
I am trying to install a few packages in a fresh installed ec2 instance (Red Hat Image) using both the yum and package module but unable to install the packages as I am encountering the error everytime


##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
module used : yum and package both tried
Task: Install packages

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible 2.9.7
  config file = /home/chuck/Desktop/Projects/Ansible-Kube/ansible.cfg
  configured module search path = ['/home/chuck/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.9 (default, Apr 18 2020, 01:56:04) [GCC 8.4.0]


```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below

```

##### OS / ENVIRONMENT
<!--- Provide all relevant information below, e.g. target OS versions, network device firmware, etc. -->


##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml

This is how I am connecting to the instance

hosts: 
  vars:
    ansible_python_interpreter: /usr/bin/python2
  remote_user: ec2-user
  connection: ssh
  become: yes
  gather_facts: no

(python2 installed)
and tried installing packages using both package and yum module (in place of package moudle used yum also)

name: install packages needed
  package:
    name: "{{ item }}"
    state: present 
    use_backend: yum
  with_items:
    - lvm2
    - device-mapper
    - curl 
    - device-mapper-persistent-data
    - device-mapper-event 
    - device-mapper-libs
    - device-mapper-event-libs

```

<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
It should install all the neccessary packages


##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->
Unsupported parameters for (dnf) module: use_backend Supported parameters include: allow_downgrade, autoremove, bugfix, conf_file, disable_excludes, disable_gpg_check, disable_plugin, disablerepo, download_dir, download_only, enable_plugin, enablerepo, exclude, install_repoquery, install_weak_deps, installroot, list, lock_timeout, name, releasever, security, skip_broken, state, update_cache, update_only, validate_certs"

<!--- Paste verbatim command output between quotes -->
```paste below

```

Hints from issue discussion:
Files identified in the description:
None

If these files are incorrect, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
Hi @DiptoChakrabarty, **use_backend** parameter is not a part of the package module. Try with **use** instead of **use_backend**
like this:
```
name: install packages needed
  package:
    name: "{{ item }}"
    state: present 
    use: yum
  with_items:
    - lvm2
    - device-mapper
```
code documentation I referenced: https://github.com/ansible/ansible/blob/v2.9.7/lib/ansible/modules/packaging/os/package.py
The `use_backend` argument, is specific to the `yum` action plugin, to instruct the `yum` action plugin whether to use `yum` or `dnf`.

In the `package` action, the name `use` is used to specify which underlying module to use.  The `package` action, calls modules directly, and does not call the `yum` action plugin.

As such, you just want to supply `use: yum`.

I'm however not going to close this issue, as we should probably normalize this and make the `yum` action plugin also support `use` as an alias of `use_backend`.
@sivel can I try to come up with a pull request?
@rukman7 works for me. I'm not actively working on this.
@sivel done.

Commit message: Update changelogs/fragments/70792-yum-action-plugin-use-as-alias-of-use-backend.yml

Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com>

Patch to review:
diff --git a/changelogs/fragments/70792-yum-action-plugin-use-as-alias-of-use-backend.yml b/changelogs/fragments/70792-yum-action-plugin-use-as-alias-of-use-backend.yml
new file mode 100644
index 00000000000000..6e14eb040630c4
--- /dev/null
+++ b/changelogs/fragments/70792-yum-action-plugin-use-as-alias-of-use-backend.yml
@@ -0,0 +1,2 @@
+bugfixes:
+  - yum - yum action plugin changes to support 'use' as an alias of 'use_backend' (https://github.com/ansible/ansible/issues/70774).
diff --git a/lib/ansible/plugins/action/yum.py b/lib/ansible/plugins/action/yum.py
index aee8e987cbd503..8b3d98c12f0da1 100644
--- a/lib/ansible/plugins/action/yum.py
+++ b/lib/ansible/plugins/action/yum.py
@@ -17,6 +17,7 @@
 from __future__ import (absolute_import, division, print_function)
 __metaclass__ = type
 
+from ansible.errors import AnsibleActionFail
 from ansible.plugins.action import ActionBase
 from ansible.utils.display import Display
 
@@ -47,7 +48,15 @@ def run(self, tmp=None, task_vars=None):
         del tmp  # tmp no longer has any effect
 
         # Carry-over concept from the package action plugin
-        module = self._task.args.get('use_backend', "auto")
+        module = 'auto'
+
+        if 'use' in self._task.args and 'use_backend' in self._task.args:
+            raise AnsibleActionFail("parameters are mutually exclusive: ('use', 'use_backend')")
+
+        use_backend = self._task.args.get('use', self._task.args.get('use_backend', None))
+
+        if use_backend:
+            module = use_backend
 
         if module == 'auto':
             try:
@@ -85,6 +94,8 @@ def run(self, tmp=None, task_vars=None):
                 new_module_args = self._task.args.copy()
                 if 'use_backend' in new_module_args:
                     del new_module_args['use_backend']
+                if 'use' in new_module_args:
+                    del new_module_args['use']
 
                 display.vvvv("Running %s as the backend for the yum action plugin" % module)
                 result.update(self._execute_module(module_name=module, module_args=new_module_args, task_vars=task_vars, wrap_async=self._task.async_val))
diff --git a/test/integration/targets/package/tasks/main.yml b/test/integration/targets/package/tasks/main.yml
index 4fc3a8a67966f3..666401b2610f40 100644
--- a/test/integration/targets/package/tasks/main.yml
+++ b/test/integration/targets/package/tasks/main.yml
@@ -111,4 +111,108 @@
     - name: verify at command is installed
       shell: which at
 
+    - name: remove at package
+      package:
+        name: at
+        state: absent
+      register: at_install0
+
+    - name: validate package removal
+      assert:
+        that:
+          - "at_install0 is changed"
+
   when: ansible_distribution in package_distros
+
+##
+## yum
+##
+#Validation for new parameter 'use' in yum action plugin which aliases to 'use_backend'
+#Issue: https://github.com/ansible/ansible/issues/70774
+- block:
+    - name: verify if using both the parameters 'use' and 'use_backend' throw error
+      yum:
+        name: at
+        state: present
+        use_backend: yum
+        use: yum
+      ignore_errors: yes
+      register: result
+
+    - name: verify error
+      assert:
+        that:
+          - "'parameters are mutually exclusive' in result.msg"
+          - "not result is changed"
+
+    - name: verify if package installation is successful using 'use' parameter
+      yum:
+        name: at
+        state: present
+        use: dnf
+      register: result
+
+    - name: verify the result
+      assert:
+        that:
+          - "result is changed"
+
+    - name: remove at package
+      yum:
+        name: at
+        state: absent
+        use: dnf
+      register: result
+
+    - name: verify package removal
+      assert:
+        that:
+          - "result is changed"
+
+    - name: verify if package installation is successful using 'use_backend' parameter
+      yum:
+        name: at
+        state: present
+        use_backend: dnf
+      register: result
+
+    - name: verify the result
+      assert:
+        that:
+          - "result is changed"
+
+    - name: remove at package
+      yum:
+        name: at
+        state: absent
+        use_backend: dnf
+      register: result
+
+    - name: verify package removal
+      assert:
+        that:
+          - "result is changed"
+
+    - name: verify if package installation is successful without using 'use_backend' and 'use' parameters
+      yum:
+        name: at
+        state: present
+      register: result
+
+    - name: verify the result
+      assert:
+        that:
+          - "result is changed"
+
+    - name: remove at package
+      yum:
+        name: at
+        state: absent
+      register: result
+
+    - name: verify package removal
+      assert:
+        that:
+          - "result is changed"
+
+  when: ansible_distribution == "Fedora"
\ No newline at end of file


Code review comments:
diff_hunk: @@ -47,7 +48,15 @@ def run(self, tmp=None, task_vars=None):
         del tmp  # tmp no longer has any effect
 
         # Carry-over concept from the package action plugin
-        module = self._task.args.get('use_backend', "auto")
+        module = 'auto'
+
+        if 'use' in self._task.args and 'use_backend' in self._task.args:
+            raise AnsibleActionFail("parameters are mutually exclusive: ('use', 'use_backend')")
+
+        use_backend = self._task.args.get('use', self._task.args.get('use_backend', None))
path: lib/ansible/plugins/action/yum.py
text: @user1:
I think this should probably be `'auto'` instead of `None` ... unless I missed something (which I'm always open to the possibility that I have):

```python
use_backend = self._task.args.get('use', self._task.args.get('use_backend', 'auto'))
```

@author:
Agreed. Done