Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible and s3 module

I'm trying to use Ansible to download some files to my various EC2 instances. The problem I'm having is when it comes to my AWS credentials. The AWS Ansible modules all work great, including the S3 module. The following (when I substitute in my AWS credentials) works like a charm.

  - name: upload data import file
    s3: aws_access_key=<accesskey> aws_secret_key=<secretkey> bucket=my-bucket object=/data.zip mode=get

However, I need Ansible playbooks and roles I'm writing to be utilized by anyone, and I don't want to have any AWS credentials hardcoded. Everywhere else I use the Ansible AWS modules, I've eliminated aws_access_key and aws_secret_key and it works just fine as Ansible looks for those values in environment variables. However, with every other use, I'm running them as local actions. So, it's pulling the credentials from my local machine, which is what I want. The problem is when I'm running the S3 module on one of my instances, if I eliminate the credential parameters, I get:

failed: [54.173.19.238] => {"failed": true}
msg: No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV1Handler'] Check your credentials

I imagine that this is because since I've not specified the credentials, it's looking for them in environment variables on my instance, where they are not set. Nor would I want to set them in environment variables on the instance.

Is there a way I can download a file from S3 with ansible and not have to specify my AWS credentials?

like image 382
Lee Fox Avatar asked Mar 16 '23 19:03

Lee Fox


1 Answers

S3 module in ansible doesn't support the profile option, but you can use like this, if you have exported the aws_key and aws_secret as variables:

export aws_key="AAAAAAAAAAAAAAAAAAAAAAAAAA"
export aws_secret="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

Then you can use them like this:

s3:
  aws_access_key: "{{ lookup('env','aws_key') }}"
  aws_secret_key: "{{ lookup('env','aws_secret') }}"
  bucket: "my-bucket"
  object: "/data.zip"
  mode: get

Hope this will help you or anyone, who is looking for, to use the local environment variables inside the ansible playbook. Thanks

like image 192
Arbab Nazar Avatar answered Mar 24 '23 17:03

Arbab Nazar