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

ansible_mitogen: Support templated become_user #1148

Merged
merged 1 commit into from
Oct 16, 2024

Conversation

mordekasg
Copy link

@mordekasg mordekasg commented Oct 8, 2024

refs #1083

@moreati moreati changed the title fix: allow ansible_user and become_user to be templated #1083 fix: allow become_user to be templated Oct 8, 2024
moreati
moreati previously requested changes Oct 8, 2024
Copy link
Member

@moreati moreati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, but this PR has multiple issues

  • as mentioned it needs to use the become plugin, not the connection plugin
  • the changelog should be narrower in its claim
  • It needs additional tests to confirm that templated become user now works, and that it will stay working in the future.

ansible_mitogen/transport_config.py Outdated Show resolved Hide resolved
docs/changelog.rst Outdated Show resolved Hide resolved
@moreati
Copy link
Member

moreati commented Oct 8, 2024

This is my work in progress for handling become plugin options

    # Better to use ActionBase.get_become_option? Probably not, as long as
    # ansible_mitogen.connection.Connection.reset() assigns a half lobotomised
    # self._action
    def _become_option(self, name):
        plugin = self._connection.become
        if plugin is not None:
            return plugin.get_option(
                name, hostvars=self._task_vars, playcontext=self._play_context,
            )
        else:
            # FIXME BecomeBase.get_option() only does this for become_user,
            #       become_pass, become_flags, & become_exe.
            LOG.warning(
                '%r: Used play_context fallback for option %r', self, name,
            )
            return getattr(self._play_context, name)

The FIXME is part of why it's still WIP, the other is that I haven't worked out how to test it yet.

@mordekasg mordekasg marked this pull request as draft October 8, 2024 14:30
@mordekasg
Copy link
Author

This is my work in progress for handling become plugin options
[...]
The FIXME is part of why it's still WIP, the other is that I haven't worked out how to test it yet.

Should I close this PR? I feel like it`s not the best idea to leave this PR if you are already working on that topic.
Maybe it would be better to help you with testing?

@moreati
Copy link
Member

moreati commented Oct 8, 2024

Lets leave it open, in draft. It's as agood a place as any to collect notes, intermiediate test results and generally collaborate.

Have you manually tested your code change? Is it working for your playbook(s)?

@moreati
Copy link
Member

moreati commented Oct 11, 2024

Notes:

  • Ansible PR become - stop using play context in more places ansible/ansible#62373
    • moved common become* parameters to become options
    • created ansible.plugins.become.BecomeBase.get_option() containing a limited getattr(play_context, ...) fallback
    • is present in all ansible-core >= 2.10
  • ansible-core become plugins subclass BecomeBase. This may be a requirement of all Ansible become plugins.
  • ansible_mitogen.plugins.connection.mitogen_sudo subclasses ConnectionBase, not BecomeBase.
  • ansible_mitogen is entirely unaware of become plugins. Existing become support relies on legacy Ansible < 2.10 fallbacks.
  • ansible_mitogen.transport_config.PlayContextSpec._connect_option() will silently return non-connection options such as become_user, due to the unrestricted getattr(play_context, ...) fallback.

@moreati
Copy link
Member

moreati commented Oct 14, 2024

Noticed several commented out tests related to become_user and become_pasword while looking at this. Probably not critical path for this feature, but I want to take a look soon. Refs #658, #692

$ ag https://github.com/dw/mitogen/issues/692 -A1   
ag https://github.com/dw/mitogen/issues/692 -A1       
tests/ansible/integration/playbook_semantics/with_items.yml
8:    # TODO: https://github.com/dw/mitogen/issues/692
9-    # - name: Spin up a few interpreters

tests/ansible/integration/action/make_tmp_path.yml
145:    # TODO: https://github.com/dw/mitogen/issues/692
146-    # - name: "Try writing to temp directory for the readonly_homedir user"

tests/ansible/integration/action/synchronize.yml
43:    # TODO: https://github.com/dw/mitogen/issues/692
44-    # - file:
--
73:    # TODO: https://github.com/dw/mitogen/issues/692
74-    # - file:

tests/ansible/integration/become/sudo_password.yml
62:    # TODO: https://github.com/dw/mitogen/issues/692
63-    # - name: Ensure password sudo succeeds.

tests/ansible/integration/become/sudo_requiretty.yml
8:    # TODO: https://github.com/dw/mitogen/issues/692
9-    # - name: Verify we can login to a non-passworded requiretty account
--
22:    # TODO: https://github.com/dw/mitogen/issues/692
23-    # - name: Verify we can login to a passworded requiretty account

@moreati moreati changed the title fix: allow become_user to be templated ansible_mitogen: Support templated become_user Oct 14, 2024
This reads the become username from the `become_user` attribute of the play
context, to the `"become_user"` option of the loaded become plugin. This has
been supported by vanilla Ansible since Ansible 2.10 (ansible-base 2.10).

To support this I've also switched from using the `play_context.become` (a
bool), to `connection.become` (an instance of the appropriate) become plugin.

New tests have been added, modelled on those for templated connection
parameters (see mitogen-hq#1147, mitogen-hq#1153, mitogen-hq#1159).

See
- ansible/ansible@480b106

refs mitogen-hq#1083

Co-authored-by: mordek <[email protected]>
@moreati moreati force-pushed the #1083 branch 2 times, most recently from a98ee83 to bf6607e Compare October 14, 2024 11:23
@moreati
Copy link
Member

moreati commented Oct 14, 2024

@mordekasg I've updated the branch with what I think is the correct combination to get templated become_user working, plus tests to cover it. Could you try it and let me know if it works for you?. Comments, questions and code reviews welcomed.

I also added you to the list of contributers. If you'd prefer not to be listed or to change the entry, then please let me know.

@moreati moreati marked this pull request as ready for review October 14, 2024 12:04
@mordekasg
Copy link
Author

Lets leave it open, in draft. It's as agood a place as any to collect notes, intermiediate test results and generally collaborate.

Have you manually tested your code change? Is it working for your playbook(s)?

I tested my change then and now - you were right, it`s not working or working randomly.

@mordekasg I've updated the branch with what I think is the correct combination to get templated become_user working, plus tests to cover it. Could you try it and let me know if it works for you?. Comments, questions and code reviews welcomed.

Thank you! Will try to do it today.

@mordekasg
Copy link
Author

mordekasg commented Oct 16, 2024

@moreati
Tested - everything is working flawlessly.
I have also done some runs on my infrastructure and nothing crashed - LGTM.

@mordekasg mordekasg requested a review from moreati October 16, 2024 11:09
@moreati
Copy link
Member

moreati commented Oct 16, 2024

Tested - everything is working flawlessly.
I have also done some runs on my infrastructure and nothing crashed - LGTM.

thank you, great news.

@moreati moreati dismissed their stale review October 16, 2024 11:35

The code reviewed has been replaced by commits I made

@moreati moreati merged commit a07489d into mitogen-hq:master Oct 16, 2024
24 checks passed
@mordekasg mordekasg deleted the #1083 branch October 16, 2024 11:36
moreati added a commit to moreati/mitogen that referenced this pull request Nov 7, 2024
The code change to support this was already made in transport_config.py, as
part of templated become_user support (commit bf6607e, PR mitogen-hq#1148). This
commit adds tests to confirm the functionality.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants