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

Unable to import amazon.aws.ec2_instance due to invalid syntax #925

Closed
1 task done
tobiasehlert opened this issue Jul 11, 2022 · 5 comments
Closed
1 task done

Unable to import amazon.aws.ec2_instance due to invalid syntax #925

tobiasehlert opened this issue Jul 11, 2022 · 5 comments
Labels
bug This issue/PR relates to a bug module module needs_triage plugins plugin (any type) python3

Comments

@tobiasehlert
Copy link

Summary

I try to use the amazon.aws.ec2_instance module from the amazon.aws collection, but it returns that there is some syntax error when importing.

Works perfect to run various other modules, it's just the ec2_instance one that always fails (tested multiple examples as well).

fatal: [localhost]: FAILED! => {
    "msg": "Unable to import amazon.aws.ec2_instance due to invalid syntax"
}

Issue Type

Bug Report

Component Name

ec2_instance

Ansible Version

$ ansible --version
ansible 2.9.23
  config file = /opt/actions-runner/_work/cicd-infra/cicd-infra/ansible/ansible.cfg
  configured module search path = [u'/home/ec2-user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.18 (default, May 25 2022, 14:30:51) [GCC 7.3.1 20180712 (Red Hat 7.3.1-15)]

Collection Versions

$ ansible-galaxy collection list
usage: ansible-galaxy collection [-h] COLLECTION_ACTION ...
ansible-galaxy collection: error: argument COLLECTION_ACTION: invalid choice: u'list' (choose from 'init', 'build', 'publish', 'install')

AWS SDK versions

$ pip show boto boto3 botocore
Name: boto
Version: 2.49.0
Summary: Amazon Web Services Library
Home-page: https://github.com/boto/boto/
Author: Mitch Garnaat
Author-email: [email protected]
License: MIT
Location: /home/ec2-user/.local/lib/python3.7/site-packages
Requires: 
Required-by: 
---
Name: boto3
Version: 1.24.26
Summary: The AWS SDK for Python
Home-page: https://github.com/boto/boto3
Author: Amazon Web Services
Author-email: None
License: Apache License 2.0
Location: /home/ec2-user/.local/lib/python3.7/site-packages
Requires: jmespath, s3transfer, botocore
Required-by: 
---
Name: botocore
Version: 1.27.26
Summary: Low-level, data-driven core of boto 3.
Home-page: https://github.com/boto/botocore
Author: Amazon Web Services
Author-email: None
License: Apache License 2.0
Location: /home/ec2-user/.local/lib/python3.7/site-packages
Requires: jmespath, python-dateutil, urllib3
Required-by: s3transfer, boto3, awscli

Configuration

$ ansible-config dump --only-changed

OS / Environment

Amazon Linux 2 on ami-0917076ab9780844d (amzn2-ami-kernel-5.10-hvm-2.0.20220606.1-x86_64-gp2)

Steps to Reproduce

---
- name: Generate EC2 baseimage
  hosts: localhost
  vars_files:
    - vars.yaml

  tasks:
    - name: restart a particular instance by its ID
      amazon.aws.ec2_instance:
        state: restarted
        instance_ids:
          - i-12345678

Expected Results

I expected a message saying that the instance with that ID doesn't exist so it can't restart it.

Actual Results

ansible-playbook 2.9.23
  config file = /opt/actions-runner/_work/cicd-infra/cicd-infra/ansible/ansible.cfg
  configured module search path = [u'/home/ec2-user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible-playbook
  python version = 2.7.18 (default, May 25 2022, 14:30:51) [GCC 7.3.1 20180712 (Red Hat 7.3.1-15)]
Using /opt/actions-runner/_work/cicd-infra/cicd-infra/ansible/ansible.cfg as config file
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
script declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Parsed /etc/ansible/hosts inventory source with ini plugin
Warning: : provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
Skipping callback 'actionable', as we already have a stdout callback.
Skipping callback 'counter_enabled', as we already have a stdout callback.
Skipping callback 'debug', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'full_skip', as we already have a stdout callback.
Skipping callback 'json', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'null', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
Skipping callback 'selective', as we already have a stdout callback.
Skipping callback 'skippy', as we already have a stdout callback.
Skipping callback 'stderr', as we already have a stdout callback.
Skipping callback 'unixy', as we already have a stdout callback.
Skipping callback 'yaml', as we already have a stdout callback.
PLAYBOOK: generate-baseimage.yaml **********************************************
1 plays in generate-baseimage.yaml
Read vars_file 'vars.yaml'
Read vars_file 'vars.yaml'
Read vars_file 'vars.yaml'
PLAY [Generate EC2 baseimage] **************************************************
Read vars_file 'vars.yaml'
TASK [Gathering Facts] *********************************************************
task path: /opt/actions-runner/_work/cicd-infra/cicd-infra/ansible/generate-baseimage.yaml:2
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ec2-user
<127.0.0.1> EXEC /bin/sh -c 'echo ~ec2-user && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ec2-user/.ansible/tmp `"&& mkdir "` echo /home/ec2-user/.ansible/tmp/ansible-tmp-1657539734.02-7532-242864283276567 `" && echo ansible-tmp-1657539734.02-7532-242864283276567="` echo /home/ec2-user/.ansible/tmp/ansible-tmp-1657539734.02-7532-242864283276567 `" ) && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py
<127.0.0.1> PUT /home/ec2-user/.ansible/tmp/ansible-local-7525ugXWTl/tmpUvk2KL TO /home/ec2-user/.ansible/tmp/ansible-tmp-1657539734.02-7532-242864283276567/AnsiballZ_setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/ec2-user/.ansible/tmp/ansible-tmp-1657539734.02-7532-242864283276567/ /home/ec2-user/.ansible/tmp/ansible-tmp-1657539734.02-7532-242864283276567/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python3 /home/ec2-user/.ansible/tmp/ansible-tmp-1657539734.02-7532-242864283276567/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/ec2-user/.ansible/tmp/ansible-tmp-1657539734.02-7532-242864283276567/ > /dev/null 2>&1 && sleep 0'
ok: [localhost]
Read vars_file 'vars.yaml'
TASK [restart a particular instance by its ID] *********************************
task path: /opt/actions-runner/_work/cicd-infra/cicd-infra/ansible/generate-baseimage.yaml:42
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ec2-user
<127.0.0.1> EXEC /bin/sh -c 'echo ~ec2-user && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ec2-user/.ansible/tmp `"&& mkdir "` echo /home/ec2-user/.ansible/tmp/ansible-tmp-1657539734.8-7583-58503937194919 `" && echo ansible-tmp-1657539734.8-7583-58503937194919="` echo /home/ec2-user/.ansible/tmp/ansible-tmp-1657539734.8-7583-58503937194919 `" ) && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/ec2-user/.ansible/tmp/ansible-tmp-1657539734.8-7583-58503937194919/ > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: FAILED! => {
    "msg": "Unable to import amazon.aws.ec2_instance due to invalid syntax"
}
PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0 

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibullbot
Copy link

Files identified in the description:

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

click here for bot help

@ansibullbot
Copy link

@ansibullbot ansibullbot added bug This issue/PR relates to a bug module module needs_triage plugins plugin (any type) python3 labels Jul 11, 2022
@tremble
Copy link
Contributor

tremble commented Jul 11, 2022

Hi @tobiasehlert,

We have sanity tests which should generally block code that can't be imported.

Looking at the information you've provided, it looks like you're using Python 2.7. This collection has dropped support for Python 2.7 due to the boto3 and botocore SDKs dropping support for Python 2.7. Assuming you're trying to use a recent copy of the collection rather than the version shipped with Ansible 2.9, the most likely cause of the problem is that Python 3+ syntax is now in the ec2_instance module.

As this issue appears to be both a version of Ansible which is no longer supported by upstream, and a version of Python which is no longer supported by this collection, I'm going to close this issue. If you're able to reproduce the issue with Python >= 3.6 then feel free to reopen the issue.

If you're running Ansible Tower / Ansible Automation Platform and the ec2_instance module that shipped with the product, then please open a support case with Red Hat support

@tremble tremble closed this as completed Jul 11, 2022
@tremble
Copy link
Contributor

tremble commented Jul 11, 2022

Just a quick note ec2_instance does indeed have non-Python 2.7 code:

$ python2.7 -m py_compile plugins/modules/ec2_instance.py 
  File "plugins/modules/ec2_instance.py", line 1826
    result = {**state_results, **alter_config_result}
               ^
SyntaxError: invalid syntax

It is however valid Python 3.6+:

$ python3.6 -m py_compile plugins/modules/ec2_instance.py 

@tobiasehlert
Copy link
Author

Thanks @tremble!

Switched to a Ubuntu based GitHub runner from Amazon Linux 2 and there Ansible was running with Python 3.
Now my playbook works as expected :)

abikouo pushed a commit to abikouo/amazon.aws that referenced this issue Sep 18, 2023
…ons#925)

New module for creating Cloudfront header policies…

.. used for response headers
SUMMARY
New Cloudfront module for CF response headers policies, see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/adding-response-headers.html
This is still a relavily new feature, see https://aws.amazon.com/de/blogs/networking-and-content-delivery/amazon-cloudfront-introduces-response-headers-policies/
ISSUE TYPE

New Module Pull Request

COMPONENT NAME
cloudfront_response_headers_policy.py

Reviewed-by: Mark Woolley <[email protected]>
Reviewed-by: Stefan Horning <None>
Reviewed-by: Alina Buzachis <None>
Reviewed-by: Markus Bergholz <[email protected]>
abikouo pushed a commit to abikouo/amazon.aws that referenced this issue Sep 18, 2023
…ons#925)

New module for creating Cloudfront header policies…

.. used for response headers
SUMMARY
New Cloudfront module for CF response headers policies, see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/adding-response-headers.html
This is still a relavily new feature, see https://aws.amazon.com/de/blogs/networking-and-content-delivery/amazon-cloudfront-introduces-response-headers-policies/
ISSUE TYPE

New Module Pull Request

COMPONENT NAME
cloudfront_response_headers_policy.py

Reviewed-by: Mark Woolley <[email protected]>
Reviewed-by: Stefan Horning <None>
Reviewed-by: Alina Buzachis <None>
Reviewed-by: Markus Bergholz <[email protected]>
abikouo pushed a commit to abikouo/amazon.aws that referenced this issue Oct 24, 2023
…ons#925)

New module for creating Cloudfront header policies…

.. used for response headers
SUMMARY
New Cloudfront module for CF response headers policies, see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/adding-response-headers.html
This is still a relavily new feature, see https://aws.amazon.com/de/blogs/networking-and-content-delivery/amazon-cloudfront-introduces-response-headers-policies/
ISSUE TYPE

New Module Pull Request

COMPONENT NAME
cloudfront_response_headers_policy.py

Reviewed-by: Mark Woolley <[email protected]>
Reviewed-by: Stefan Horning <None>
Reviewed-by: Alina Buzachis <None>
Reviewed-by: Markus Bergholz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug module module needs_triage plugins plugin (any type) python3
Projects
None yet
Development

No branches or pull requests

3 participants