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

Add support for mitogen #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add support for mitogen #28

wants to merge 1 commit into from

Conversation

wilfriedroset
Copy link
Collaborator

Add support for mitogen

Compared to the Vanilla Ansible, Mitogen calls sshwrapper.py
differently. This imply a different parsing to extract the options, host
and command. Moreover, Mitogen does not passes the remote_user
therefore we must set as well BASTION_ANSIBLE_REMOTE_USER env var.

Apart from setting the BASTION_ANSIBLE_REMOTE_USER the wrapper works
for both vanilla and mitogen connection. Users should refer to mitogen
to learn about the installation process.

The following commit has been tested with:

  • Test case 1
    • Mitogen 0.3.7
    • Ansible 210.8
    • Python 3.9.2
    • Debian 11.9
  • Test case 2
    • Mitogen 0.3.7
    • Ansible 2.16.6
    • Python 3.12
    • MacOS 14.2

This playbook works as expected with and without mitogen enabled

❯ cat test.yaml
---
- name: test
  hosts: test
  gather_facts: false
  tasks:
    - name: Run the equivalent of "apt-get update" as a separate step
      ansible.builtin.apt:
        update_cache: true

    - name: Create files with copy content module
      copy:
        content: |
          test file {{ item }}
        dest: /tmp/file_{{item}}
      with_sequence: start=1 end=10

    - name: demo template
      ansible.builtin.template:
        src: demo.txt.j2
        dest: /tmp/demo.txt
        mode: 0640

Here is the ansible.cfg

[defaults]
interpreter_python = /usr/bin/python3
host_key_checking = False
deprecation_warnings = False
syslog_facility = LOG_USER
bin_ansible_callbacks = True

gathering = explicit
callbacks_enabled = ansible.posix.profile_tasks

strategy_plugins = ./mitogen/ansible_mitogen/plugins/strategy/
strategy = mitogen_linear

[ssh_connection]
scp_if_ssh = False
# Rely on bastion wrapper
pipelining = True
transfer_method = sftp
ssh_executable = ./bastion-ansible-wrapper/sshwrapper.py
sftp_executable = ./bastion-ansible-wrapper/sftpbastion.sh
retries = 1

Compared to the Vanilla Ansible, [Mitogen](https://github.com/mitogen-hq/mitogen) calls `sshwrapper.py`
differently. This imply a different parsing to extract the options, host
and command. Moreover, Mitogen does not passes the `remote_user`
therefore we must set as well `BASTION_ANSIBLE_REMOTE_USER` env var.

Apart from setting the `BASTION_ANSIBLE_REMOTE_USER` the wrapper works
for both vanilla and mitogen connection. Users should refer to mitogen
to learn about the installation process.

The following commit has been tested with:
* Test case 1
  * Mitogen 0.3.7
  * Ansible 210.8
  * Python 3.9.2
  * Debian 11.9
* Test case 2
  * Mitogen 0.3.7
  * Ansible 2.16.6
  * Python 3.12
  * MacOS 14.2

This playbook works as expected with and without mitogen enabled
```
❯ cat test.yaml
---
- name: test
  hosts: test
  gather_facts: false
  tasks:
    - name: Run the equivalent of "apt-get update" as a separate step
      ansible.builtin.apt:
        update_cache: true

    - name: Create files with copy content module
      copy:
        content: |
          test file {{ item }}
        dest: /tmp/file_{{item}}
      with_sequence: start=1 end=10

    - name: demo template
      ansible.builtin.template:
        src: demo.txt.j2
        dest: /tmp/demo.txt
        mode: 0640
```
Here is the ansible.cfg
```
[defaults]
interpreter_python = /usr/bin/python3
host_key_checking = False
deprecation_warnings = False
syslog_facility = LOG_USER
bin_ansible_callbacks = True

gathering = explicit
callbacks_enabled = ansible.posix.profile_tasks

strategy_plugins = ./mitogen/ansible_mitogen/plugins/strategy/
strategy = mitogen_linear

[ssh_connection]
scp_if_ssh = False
pipelining = True
transfer_method = sftp
ssh_executable = ./bastion-ansible-wrapper/sshwrapper.py
sftp_executable = ./bastion-ansible-wrapper/sftpbastion.sh
retries = 1
```

Signed-off-by: Wilfried Roset <wilfriedroset@users.noreply.github.com>
@wilfriedroset
Copy link
Collaborator Author

Please note that I've found cases where ansible fails due to an error similar to:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ImportError: attempted relative import with no known parent package

According to my research, this is more related to an issue with the collection rather than mitogen itself or the wrapper.
See:

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.

None yet

1 participant