# Ec2 Module doesn't seem to take up / use environment variables - AWS\_SECRET\_KEY / AWS\_ACCESS\_KEY

**URL:** <https://forum.ansible.com/t/ec2-module-doesnt-seem-to-take-up-use-environment-variables-aws-secret-key-aws-access-key/13636>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [October 9, 2013, 8:04am UTC](https://forum.ansible.com/t/ec2-module-doesnt-seem-to-take-up-use-environment-variables-aws-secret-key-aws-access-key/13636 "2013-10-09T08:04:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sundar2](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Sundar2](https://forum.ansible.com/u/Sundar2)\
**Post date:** [October 9, 2013, 8:04am UTC](https://forum.ansible.com/t/ec2-module-doesnt-seem-to-take-up-use-environment-variables-aws-secret-key-aws-access-key/13636/1 "2013-10-09T08:04:48Z")

</div>

Am using Ansible 1.3.2 (on Ubuntu 12.04)

I’ve the following lines in my playbook.

- name: Launch instance  
local\_action: ec2 keypair={{ ec2\_keypair\_name }} group={{ ec2\_security\_group\_name }} instance\_type={{ ec2\_instance\_type }} image={{ ec2\_ami\_id }} wait=yes region={{ ec2\_region }}  
register: ec2

And I’ve these in my .bashrc

export AWS\_ACCESS\_KEY=XXX

export AWS\_SECRET\_KEY=YYY  
export aws\_access\_key=XXX  
export aws\_secret\_key=YYY  
export ec2\_access\_key=XXX  
export ec2\_secret\_key=YYY  
export EC2\_ACCESS\_KEY=XXX  
export EC2\_SECRET\_KEY=YYY  
export EC2\_REGION=ap-southeast-1

And I did a reload of env variables for the current session.  
. .bashrc  
and verified that the env variables are available using `printenv`

But still, while running the playbook, it throws this error,

**msg: No handler was ready to authenticate. 1 handlers were checked. [‘QuerySignatureV2AuthHandler’] Check your credentials**

am I missing something here?

Also would like to know, which env variable would ansible use → ec2\_access\_key & similar or aws\_access\_key & similar (only in upper case?)

Thank you.

---

<div class="post-metadata">

**Author:** ![tannerjc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tannerjc/32/985_2.png) [@tannerjc](https://forum.ansible.com/u/tannerjc)\
**Post date:** [October 9, 2013, 1:27pm UTC](https://forum.ansible.com/t/ec2-module-doesnt-seem-to-take-up-use-environment-variables-aws-secret-key-aws-access-key/13636/2 "2013-10-09T13:27:21Z")

</div>

Sundar,

I think the best explanation for how the ec2 module finds credentials is the code …

[https://github.com/ansible/ansible/blob/devel/library/cloud/ec2](https://github.com/ansible/ansible/blob/devel/library/cloud/ec2)

Within main(), the module checks for passed in arguments …

```auto

def main():
    module = AnsibleModule(
        argument_spec = dict(

            region = dict(aliases=['aws_region', 'ec2_region'], choices=AWS_REGIONS),
            zone = dict(aliases=['aws_zone', 'ec2_zone']),

            ec2_url = dict(),
            aws_secret_key = dict(aliases=['ec2_secret_key', 'secret_key'], no_log=True),

            aws_access_key = dict(aliases=['ec2_access_key', 'access_key']),
        )

    )

```

```auto

    ec2_url = module.params.get('ec2_url')
    aws_secret_key = module.params.get('aws_secret_key')

    aws_access_key = module.params.get('aws_access_key')
    region = module.params.get('region')

```

So it populates region, zone, ec2\_url, aws\_secret\_key and aws\_access\_key based on input parameters OR if variables exist in the play by those names.

Next, the module checks your environment variables if any of the parameters are unset …

```auto

    # allow eucarc environment variables to be used if ansible vars aren't set
    if not ec2_url and 'EC2_URL' in os.environ:

        ec2_url = os.environ['EC2_URL']

    if not aws_secret_key:

        if 'AWS_SECRET_KEY' in os.environ:
            aws_secret_key = os.environ['AWS_SECRET_KEY']

        elif 'EC2_SECRET_KEY' in os.environ:
            aws_secret_key = os.environ['EC2_SECRET_KEY']

    if not aws_access_key:
        if 'AWS_ACCESS_KEY' in os.environ:

            aws_access_key = os.environ['AWS_ACCESS_KEY']
        elif 'EC2_ACCESS_KEY' in os.environ:

            aws_access_key = os.environ['EC2_ACCESS_KEY']

    if not region:
        if 'AWS_REGION' in os.environ:
            region = os.environ['AWS_REGION']

        elif 'EC2_REGION' in os.environ:
            region = os.environ['EC2_REGION']

```

You mentioned that you are trying to set all of your ec2 parameters in your bashrc, but do you also set any variables in your plays that match the parameters the module looks for?

---

<div class="post-metadata">

**Author:** ![Sundar1](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@Sundar1](https://forum.ansible.com/u/Sundar1)\
**Post date:** [October 10, 2013, 5:14am UTC](https://forum.ansible.com/t/ec2-module-doesnt-seem-to-take-up-use-environment-variables-aws-secret-key-aws-access-key/13636/3 "2013-10-10T05:14:38Z")

</div>

Hi,

This is Sundar (with my personal id).

Thanks James, for the details.

Wrt your question → Yes, only ec2\_region in group\_vars/all.yml.

Copying the code (as it was executed) below for clarity.

**------------------------------------------------------ Copy Starts -------------------------------------------------------------------------------------------------------------------------------------------------------**

scale\_up.yml

---

<div class="post-metadata">

**Author:** ![Sundar1](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@Sundar1](https://forum.ansible.com/u/Sundar1)\
**Post date:** [October 15, 2013, 8:40am UTC](https://forum.ansible.com/t/ec2-module-doesnt-seem-to-take-up-use-environment-variables-aws-secret-key-aws-access-key/13636/4 "2013-10-15T08:40:41Z")

</div>

Hi,

Any lights on this one?
