# how use aws profile when using ansible ec2.py module

**URL:** <https://forum.ansible.com/t/how-use-aws-profile-when-using-ansible-ec2-py-module/23577>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [January 27, 2017, 3:24pm UTC](https://forum.ansible.com/t/how-use-aws-profile-when-using-ansible-ec2-py-module/23577 "2017-01-27T15:24:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronak\_Patel](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Ronak\_Patel](https://forum.ansible.com/u/Ronak_Patel)\
**Post date:** [January 27, 2017, 3:24pm UTC](https://forum.ansible.com/t/how-use-aws-profile-when-using-ansible-ec2-py-module/23577/1 "2017-01-27T15:24:37Z")

</div>

I wrote a quick ansible playbook to launch a simple ec2 instance but I think I have an issue on how I want to authenticate.

What I don’t want to do is set my aws access/secret keys as env variables since they expire each hour and I need to regenerate the `~/.aws/credentials` file via a script.

Right now, my ansible playbook looks like this:

— # Launch ec2

- name: Create ec2 instance  
hosts: local  
connection: local  
gather\_facts: false  
vars:  
profile: profile\_xxxx  
key\_pair: usrxxx  
region: us-east-1  
subnet: subnet-38xxxxx  
security\_groups: [‘sg-e54xxxx’, ‘sg-bfcxxxx’, ‘sg-a9dxxx’]  
image: ami-031xxx  
instance\_type: t2.small  
num\_instances: 1  
tag\_name: ansibletest  
hdd\_volumes:
- device\_name: /dev/sdf  
volume\_size: 50  
delete\_on\_termination: true
- device\_name: /dev/sdh  
volume\_size: 50  
delete\_on\_termination: true  
tasks:
- name: launch ec2  
ec2:  
count: 1  
key\_name: “{{ key\_pair }}”  
profile: “{{ profile }}”  
group\_id: “{{ security\_groups }}”  
instance\_type: “{{ instance\_type }}”  
image: “{{ image }}”  
region: “{{ region }}”  
vpc\_subnet\_id: “{{ subnet }}”  
assign\_public\_ip: false  
volumes: “{{ hdd\_volumes }}”  
instance\_tags:  
Name: “{{ tag\_name }}”  
ASV: “{{ tag\_asv }}”  
CMDBEnvironment: “{{ tag\_cmdbEnv }}”  
EID: “{{ tag\_eid }}”  
OwnerContact: “{{ tag\_eid }}”  
register: ec2
- name: print ec2 vars  
debug: var=ec

my hosts file is this:

[local]  
localhost ansible\_python\_interpreter=/usr/local/bin/python2.7

I run my playbook like this:

ansible-playbook -i hosts launchec2.yml -vvv

and then get this back:

PLAYBOOK: launchec2.yml \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
1 plays in launchec2.yml

PLAY [Create ec2 instance] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [launch ec2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /Users/usrxxx/Desktop/cloud-jumper/Ansible/launchec2.yml:27  
Using module file /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ansible/modules/core/cloud/amazon/ec2.py  
 ESTABLISH LOCAL CONNECTION FOR USER: usrxxx  
 EXEC /bin/sh -c ‘( umask 77 && mkdir -p “`echo ~/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730`” && echo ansible-tmp-1485527483.82-106272618422730=“`echo ~/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730`” ) && sleep 0’  
 PUT /var/folders/cx/\_fdv7nkn6dz21798p\_bn9dp9ln9sqc/T/tmpnk2rh5 TO /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/ec2.py  
 PUT /var/folders/cx/\_fdv7nkn6dz21798p\_bn9dp9ln9sqc/T/tmpEpwenH TO /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/args  
 EXEC /bin/sh -c ‘chmod u+x /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/ /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/ec2.py /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/args && sleep 0’  
 EXEC /bin/sh -c ‘/usr/bin/env python /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/ec2.py /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/args; rm -rf “/Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/” \> /dev/null 2\>&1 && sleep 0’  
fatal: [localhost]: FAILED! =\> {  
“changed”: false,  
“failed”: true,  
“invocation”: {  
“module\_name”: “ec2”  
},  
“module\_stderr”: “usage: ec2.py [-h] [–list] [–host HOST] [–refresh-cache]\n [–profile BOTO\_PROFILE]\nec2.py: error: unrecognized arguments: /Users/usrxxx/.ansible/tmp/ansible-tmp-1485527483.82-106272618422730/args\n”,  
“module\_stdout”: “”,  
“msg”: “MODULE FAILURE”  
}  
to retry, use: --limit @/Users/usrxxx/Desktop/cloud-jumper/Ansible/launchec2.retry

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=0 changed=0 unreachable=0 failed=1

I noticed in the `ec2.py` file it says this:

NOTE: This script assumes Ansible is being executed where the environment  
variables needed for Boto have already been set:  
export AWS\_ACCESS\_KEY\_ID=‘AK123’  
export AWS\_SECRET\_ACCESS\_KEY=‘abc123’

This script also assumes there is an ec2.ini file alongside it. To specify a  
different path to ec2.ini, define the EC2\_INI\_PATH environment variable:

export EC2\_INI\_PATH=/path/to/my\_ec2.ini

If you’re using eucalyptus you need to set the above variables and  
you need to define:

export EC2\_URL=http://hostname\_of\_your\_cc:port/services/Eucalyptus

If you’re using boto profiles (requires boto\>=2.24.0) you can choose a profile  
using the --boto-profile command line argument (e.g. ec2.py --boto-profile prod) or using  
the AWS\_PROFILE variable:

AWS\_PROFILE=prod ansible-playbook -i ec2.py myplaybook.yml

so I ran it like this:

AWS\_PROFILE=profile\_xxxx ansible-playbook -i hosts launchec2.yml -vvv

but still got the same results…
