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

40 views
Skip to first unread message

ZillaYT

unread,
Sep 16, 2016, 9:53:25 AM9/16/16
to Ansible Project
I'm using Ansible's s3 module, 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.

Kane Kennedy

unread,
Sep 16, 2016, 11:54:04 AM9/16/16
to Ansible Project
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..

Kai Stian Olstad

unread,
Sep 16, 2016, 1:09:07 PM9/16/16
to ansible...@googlegroups.com
On 16. sep. 2016 15:53, ZillaYT wrote:
> 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

Check out lookup
https://docs.ansible.com/ansible/playbooks_lookups.html#the-ini-file-lookup

--
Kai Stian Olstad

ZillaYT

unread,
Sep 16, 2016, 1:54:44 PM9/16/16
to Ansible Project
Thanks, I'm reading on lookup now, and that may be the way to go.
Reply all
Reply to author
Forward
0 new messages