---
- name: "Retrive credentials from env vars"
  ignore_errors: true
  no_log: True
  set_fact:
    aws_access_key: "{{ lookup('env','AWS_ACCESS_KEY_ID') }}"
    aws_secret_key: "{{ lookup('env','AWS_SECRET_ACCESS_KEY') }}"

- name: "Retrive credentials from creds file"
  ignore_errors: true
  no_log: True
  set_fact:
    aws_access_key: "{{ lookup('ini', 'aws_access_key_id section={{ aws_cred_profile }} file=~/.aws/credentials') }}"
    aws_secret_key: "{{ lookup('ini', 'aws_secret_access_key section={{ aws_cred_profile }} file=~/.aws/credentials') }}"
  when: ( not aws_access_key ) and ( not aws_secret_key )

- debug:
    msg:
      - 'Error in env; Unable to discover AWS_ACCESS_KEY_ID'
  when: not aws_access_key
  failed_when: not aws_access_key

- debug:
    msg:
      - 'Error in env; Unable to discover AWS_SECRET_ACCESS_KEY'
  when: not aws_secret_key
  failed_when: not aws_secret_key

- name: 'Set fact: arg for lookup queries'
  set_fact:
    creds:
      aws_access_key: "{{ aws_access_key }}"
      aws_secret_key: "{{ aws_secret_key }}"
      aws_region: "{{ aws_region }}"
