mitogen
mitogen copied to clipboard
Some amazon.aws modules fail on relative import error
I first noticed this issue with the amazon.aws.ec2_tag module:
TASK [amazon.aws.ec2_tag] **************************************************************************************************************************************************************************************************************************************************
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
fatal: [127.0.0.1]: FAILED! => {"changed": false, "module_stderr": "", "module_stdout": "Traceback (most recent call last):\r\n File \"/root/.ansible/tmp/ansible_mitogen_runner_l2jizk1f/ec2_tag.py\", line 116, in <module>\r\n from ..module_utils.core import AnsibleAWSModule\r\nImportError: attempted relative import with no known parent package\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
Module error with line-breaks replaced for readability:
Traceback (most recent call last):
File "/root/.ansible/tmp/ansible_mitogen_runner_l2jizk1f/ec2_tag.py", line 116, in <module>
from ..module_utils.core import AnsibleAWSModule
ImportError: attempted relative import with no known parent package
The same module/task runs successfully without Mitogen. In compiling data for this bug I found that the amazon.aws.ec2_tag_info module also exhibits this behavior, but other modules in the collection which follow the same pattern of importing AnsibleAWSModule
from the same relative path (from ..module_utils.core
) work just fine, and we use several of them:
- amazon.aws.aws_caller_info
- amazon.aws.aws_s3
- amazon.aws.cloudformation_info
- amazon.aws.ec2_group
- amazon.aws.ec2_group_info
- amazon.aws.ec2_instance_info
- amazon.aws.ec2_metadata_facts
- amazon.aws.ec2_vol_info
The only common pattern I can see in most/all of these working modules is that they have this specific additional import before AnsibleAWSModule
:
...
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
from ..module_utils.core import AnsibleAWSModule
...
Manually adding this line to the amazon.aws.ec2_tag module above the AnsibleAWSModule
relative import (i.e., here) seems to resolve the issue by way of triggering additional imports from the collection and/or using a different Mitogen planner (as observed in verbose logging), but as the module works fine without Mitogen, and that module doesn't actually need camel_dict_to_snake_dict
, I'm starting the "bug train" here. I'll attach verbose output from the task itself with and without the "patch" to the amazon.aws.ec2_tag module.
- Which version of Ansible are you running?
ansible [core 2.12.1]
config file = /root/ec2_tag_example/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/ansible212-venv/lib64/python3.8/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/lib/ansible212-venv/bin/ansible-playbook
python version = 3.8.5 (default, Feb 18 2021, 01:24:20) [GCC 7.3.1 20180712 (Red Hat 7.3.1-12)]
jinja version = 3.0.3
libyaml = True
- Is your version of Ansible patched in any way?
No.
- Are you running with any custom modules, or
module_utils
loaded?
No.
- Have you tried the latest master version from Git?
Yes (master and stable branches, and latest 0.3.2 release).
- Do you have some idea of what the underlying problem may be?
Not really, only as mentioned above the additional import which seems to trigger some additional imports from the collection which aren't loaded otherwise and the use of a different planner.
- Mention your host and target OS and versions
Amazon Linux 2 (stock image from AWS; no modifications); running against localhost
Linux version 4.14.256-197.484.amzn2.x86_64 (mockbuild@ip-10-0-37-156) (gcc version 7.3.1 20180712 (Red Hat 7.3.1-13) (GCC)) #1 SMP Tue Nov 30 00:17:50 UTC 2021
- Mention your host and target Python versions
Python 3.8.5 (default, Feb 18 2021, 01:24:20) [GCC 7.3.1 20180712 (Red Hat 7.3.1-12)] on linux
Example playbook:
---
- hosts: localhost
connection: local
vars:
ansible_python_interpreter: /usr/lib/ansible212-venv/bin/python
tasks:
- amazon.aws.ec2_tag:
region: us-east-1
resource: eni-05ab88838accd7aed
state: present
purge_tags: false
tags:
some_tag_key: some_tag_value
Example ansible.cfg:
[defaults]
strategy_plugins = ./mitogen/ansible_mitogen/plugins/strategy
strategy = mitogen_linear
Ansible verbose (-vvv
) output for task using amazon.aws.ec2_tag module:
Ansible verbose (-vvv
) output with "patched" amazon.aws.ec2_tag module:
We have the same problem.
Should be fixed with upcoming amazon.aws release.