Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various issues with become variables when using delegate_to in a loop #1020

Open
ed-velez opened this issue Aug 18, 2023 · 0 comments
Open
Labels
affects-0.3 Issues related to 0.3.X Mitogen releases bug Code feature that hinders desired execution outcome

Comments

@ed-velez
Copy link

The easiest way to see the issue is just by using a simple play that calls the mitogen_get_stack module and uses the delegate_to option with a loop:

---
- hosts: all
  become: true
  gather_facts: false
  tasks:
    - name: Print mitogen debug
      mitogen_get_stack:
      run_once: true
      delegate_to: "{{ node }}"
      loop: "{{ ansible_play_hosts }}"
      loop_control:
        loop_var: node

When you run this on a simple inventory of 2 hosts with one host using sudo and the other using su - you will see that the sudo/su data is incorrect for one of the hosts. You will get garbage like the following for one of the hosts:

        {
            "enable_lru": true,
            "kwargs": {
                "connect_timeout": 10,
                "password": "<redacted>", <------- Incorrect password for escalation
                "python_path": [
                    "/usr/bin/python"
                ],
                "remote_name": null,
                "sudo_args": [],  <------- This can also be incorrect
                "sudo_path": "su", <------ Incorrect evaluation for become_exe
                "username": "root"
            },
            "method": "sudo"
        }

I've been able to come up with something that sort of roughly resolves my issue:

❯ git diff --patch
diff --git a/ansible_mitogen/transport_config.py b/ansible_mitogen/transport_config.py
index 5fc78185..780e0b81 100644
--- a/ansible_mitogen/transport_config.py
+++ b/ansible_mitogen/transport_config.py
@@ -410,7 +410,12 @@ class PlayContextSpec(Spec):
         self._play_context = play_context
         self._transport = transport
         self._inventory_name = inventory_name
-        self._task_vars = self._connection._get_task_vars()
+        task_vars = self._connection._get_task_vars()
+        if 'ansible_delegated_vars' in task_vars and self._connection.delegate_to_hostname:
+            self._task_vars = task_vars['ansible_delegated_vars'][self._connection.delegate_to_hostname]
+        else:
+            self._task_vars = task_vars
+
         # used to run interpreter discovery
         self._action = connection._action

@@ -436,9 +441,7 @@ class PlayContextSpec(Spec):
         return self._play_context.become_user

     def become_pass(self):
-        become_method = self.become_method()
-        become_plugin = ansible_mitogen.loaders.become_loader.get(become_method)
-        become_pass = become_plugin.get_option('become_pass', hostvars=self._task_vars)
+        become_pass = C.config.get_config_value("become_pass", plugin_type="become", plugin_name=self.become_method(), variables=self._task_vars)
         return optional_secret(become_pass)

     def password(self):
@@ -486,28 +489,13 @@ class PlayContextSpec(Spec):
         ]

     def become_exe(self):
-        # In Ansible 2.8, PlayContext.become_exe always has a default value due
-        # to the new options mechanism. Previously it was only set if a value
-        # ("somewhere") had been specified for the task.
-        # For consistency in the tests, here we make older Ansibles behave like
-        # newer Ansibles.
-        exe = self._play_context.become_exe
-        if exe is None and self._play_context.become_method == 'sudo':
-            exe = 'sudo'
-        return exe
+        return C.config.get_config_value("become_exe", plugin_type="become", plugin_name=self.become_method(), variables=self._task_vars)

     def sudo_args(self):
         return [
             mitogen.core.to_text(term)
             for term in ansible.utils.shlex.shlex_split(
-                first_true((
-                    self._play_context.become_flags,
-                    # Ansible <=2.7.
-                    getattr(self._play_context, 'sudo_flags', ''),
-                    # Ansible <=2.3.
-                    getattr(C, 'DEFAULT_BECOME_FLAGS', ''),
-                    getattr(C, 'DEFAULT_SUDO_FLAGS', '')
-                ), default='')
+                C.config.get_config_value("become_flags", plugin_type="become", plugin_name="sudo", variables=self._task_vars) or ''
             )
         ]

I encountered the bug with mitogen 0.3.4 and I am currently using Ansible 2.11.12. I am happy to submit the above in a PR but I noticed you had a couple of PR's working on similar areas in the transport_config. Let me know if I can help get this addressed.

@ed-velez ed-velez added affects-0.3 Issues related to 0.3.X Mitogen releases bug Code feature that hinders desired execution outcome labels Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-0.3 Issues related to 0.3.X Mitogen releases bug Code feature that hinders desired execution outcome
Projects
None yet
Development

No branches or pull requests

1 participant