# How to set environment variable with value obtained from ~/.aws/credentials?

**URL:** <https://forum.ansible.com/t/how-to-set-environment-variable-with-value-obtained-from-aws-credentials/22568>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [September 16, 2016, 1:53pm UTC](https://forum.ansible.com/t/how-to-set-environment-variable-with-value-obtained-from-aws-credentials/22568 "2016-09-16T13:53:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [September 16, 2016, 1:53pm UTC](https://forum.ansible.com/t/how-to-set-environment-variable-with-value-obtained-from-aws-credentials/22568/1 "2016-09-16T13:53:25Z")

</div>

I’m using Ansible’s [s3 module](http://docs.ansible.com/ansible/s3_module.html), and it needs aws\_access\_key and aws\_secret\_key to work in my AWS environment, even though the page says they’re not required. These values are in my $HOME/.aws/credentials file

`[default] aws_access_key_id = LAFHALFHLAHFLFAH aws_secret_access_key = IRUPQORUQPORUQEPORUQPORUQEP`

I want to get these values to set the AWS\_ACCESS\_KEY\_ID and AWS\_SECRET\_ACCESS\_KEY environment variables. So how do I write an Ansible task to…

1. Read the contents of the $HOME/.aws/credentiials
2. Get the key/value pairs
3. Set the corresponding environment variables?  
Thanks.

---

<div class="post-metadata">

**Author:** ![Kane\_Kennedy](https://avatars.discourse-cdn.com/v4/letter/k/b9e5f3/32.png) [@Kane\_Kennedy](https://forum.ansible.com/u/Kane_Kennedy)\
**Post date:** [September 16, 2016, 3:00pm UTC](https://forum.ansible.com/t/how-to-set-environment-variable-with-value-obtained-from-aws-credentials/22568/2 "2016-09-16T15:00:20Z")

</div>

Sorry I can’t help with reading the file but this is how I am setting environment variables to use with a shell line

vars\_prompt:

- name: ABC\_environment  
prompt: “Enter live or preprod to set environment”  
private: no  
when: ABC\_environment is undefined

- name: ABC\_password  
prompt: “Enter the ABC Encryption key”  
private: yes  
when: ABC\_password is undefined

environment:  
ABCENV: “{{ ABC\_environment }}”  
ABCPWD: “{{ ABC\_password }}”

- name: install ABC file  
shell: unzip -P ${ABCPWD} -o -d/application /tmp/ABC\_files.zip ABC\_${ABCENV}\_application.file

shell: sudo -u xyz install -o xyz -g xyz -m 550 /application/ABC\_${ABCENV}\_application /application/application.file

Hope this helps…

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 16, 2016, 5:08pm UTC](https://forum.ansible.com/t/how-to-set-environment-variable-with-value-obtained-from-aws-credentials/22568/3 "2016-09-16T17:08:30Z")

</div>

Check out lookup  
[https://docs.ansible.com/ansible/playbooks\_lookups.html#the-ini-file-lookup](https://docs.ansible.com/ansible/playbooks_lookups.html#the-ini-file-lookup)

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [September 16, 2016, 5:54pm UTC](https://forum.ansible.com/t/how-to-set-environment-variable-with-value-obtained-from-aws-credentials/22568/4 "2016-09-16T17:54:44Z")

</div>

Thanks, I’m reading on lookup now, and that may be the way to go.
