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 connection by URL endpoint when region not specified #24

Closed
wants to merge 1 commit into from

Conversation

eric-belhomme
Copy link

SUMMARY

The ec2_vol module connects to AWS endpoint using legacy boto Python library (instead of newer boto3 Python lib) and require to specify the AWS region because it uses boto.ec2.connect_to_region() method

Altrough this is not an issue when operation with official Amazon Web Services this will irremebiably fail with alternate cloud providers that propose an AWS-compliant API, like Outscale
In such case, we require to specify an API endpoint instead of an AWS region, and use boto.connect_ec2_endpoint() in place of boto.ec2.connect_to_region()

This PR adds an alternate EC2 connection method when region parameter is not set:

    if region:
        try:
            ec2 = connect_to_aws(boto.ec2, region, **aws_connect_params)
        except (boto.exception.NoAuthHandlerFound, AnsibleAWSError) as e:
            module.fail_json(msg=str(e))
    elif ec2_url:
        try:
            ec2 = boto.connect_ec2_endpoint(ec2_url, **aws_connect_params)
        except (boto.exception.NoAuthHandlerFound, AnsibleAWSError, boto.provider.ProfileNotFoundError) as e:
            module.fail_json(msg=str(e))
    else:
        module.fail_json(msg="Either region or ec2_url must be specified")
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

ec2_vol

ADDITIONAL INFORMATION

Without this patch, ec2_vol will fail with an erroneous error message:

 ansible localhost -vv -m ec2_vol -a "ec2_url=https://fcu.eu-west-2.outscale.com region=eu-west-2 volume_type=gp2 volume_size=5 zone=eu-west-2a"
ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/rico/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/ansible-lint/lib/python3.7/site-packages/ansible
  executable location = /opt/ansible-lint/bin/ansible
  python version = 3.7.3 (default, Dec 20 2019, 18:57:59) [GCC 8.3.0]
Using /etc/ansible/ansible.cfg as config file
META: ran handlers
localhost | FAILED! => {
    "changed": false,
    "msg": "AuthFailure: AWS was not able to validate the provided access credentials"
}

With the patch, things got better:

$ ansible localhost -vv -m ec2_vol -a "ec2_url=https://fcu.eu-west-2.outscale.com volume_type=gp2 volume_size=5 zone=eu-west-2a"
Wed 01 Apr 2020 10:31:36 PM CEST
ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/rico/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/ansible-lint/lib/python3.7/site-packages/ansible
  executable location = /opt/ansible-lint/bin/ansible
  python version = 3.7.3 (default, Dec 20 2019, 18:57:59) [GCC 8.3.0]
Using /etc/ansible/ansible.cfg as config file
META: ran handlers
localhost | CHANGED => {
    "changed": true,
    "device": null,
    "volume": {
        "attachment_set": {
            "attach_time": null,
            "device": null,
            "instance_id": null,
            "status": null
        },
        "create_time": "2020-04-01T20:31:38.620Z",
        "encrypted": null,
        "id": "vol-29e31b6d",
        "iops": 100,
        "size": 5,
        "snapshot_id": "",
        "status": "available",
        "tags": {},
        "type": "gp2",
        "zone": "eu-west-2a"
    },
    "volume_id": "vol-29e31b6d",
    "volume_type": "gp2"
}

Copy link
Contributor

@tremble tremble left a comment

Choose a reason for hiding this comment

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

We have a helper for this.
https://github.com/ansible-collections/amazon.aws/blob/master/plugins/module_utils/ec2.py#L370

Rather than copy and paste you should be able to do

from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ec2_connect

and then replace from line 544 through 553 (original line numbers) with just

    ec2 = ec2_connect(module)

@tremble
Copy link
Contributor

tremble commented May 19, 2020

If this isn't merged first, then this would be fixed by the boto3 migration:

resolved_by #53

@jillr jillr changed the base branch from master to main July 2, 2020 19:22
@ansibullbot
Copy link

@ansibullbot ansibullbot added affects_2.10 bug This issue/PR relates to a bug module module needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR new_contributor Help guide this first time contributor python3 stale_ci CI is older than 7 days, rerun before merging labels Aug 18, 2020
@tremble tremble removed the stale_ci CI is older than 7 days, rerun before merging label Aug 19, 2020
@ansibullbot ansibullbot added the stale_ci CI is older than 7 days, rerun before merging label Aug 19, 2020
@ansibullbot ansibullbot added the plugins plugin (any type) label Aug 27, 2020
@tremble
Copy link
Contributor

tremble commented Nov 16, 2020

Thanks for taking the time to submit this PR. With the migration to boto3 (#53) support for specifying the EC2 endpoint should now be in place.

@tremble tremble closed this Nov 16, 2020
abikouo pushed a commit to abikouo/amazon.aws that referenced this pull request Oct 24, 2023
…ions#218)

* aws_ssm connection plugin: add STS token parameters.

Fixes ansible-collections#24

Add the following parameters to aws_ssm.py connection plugin:

* ansible_aws_ssm_access_key_id
* ansible_aws_ssm_secret_access_key
* ansible_aws_ssm_session_token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects_2.10 bug This issue/PR relates to a bug module module needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR new_contributor Help guide this first time contributor plugins plugin (any type) python3 stale_ci CI is older than 7 days, rerun before merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants