---
- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
  # ===========================================================
  # While CI uses a dedicated session, the easiest way to run
  # tests outside of CI is with a simple access/secret key pair.
  #
  # For consistency, use sts_session_token to grab session
  # credentials if we're not already using a session
  # Note: this can't be done within a session, hence the slightly
  # strange dance
  - name: 'Get a session token if we are using a basic key'
    when:
    - security_token is not defined
    block:
    - name: 'Get a session token'
      sts_session_token:
        region: '{{ aws_region }}'
        aws_access_key: '{{ aws_access_key }}'
        aws_secret_key: '{{ aws_secret_key }}'
      register: session_token
      no_log: true
    - name: 'Override initial tokens'
      set_fact:
        session_access_key: '{{ session_token.sts_creds.access_key }}'
        session_secret_key: '{{ session_token.sts_creds.secret_key }}'
        session_security_token: '{{ session_token.sts_creds.session_token }}'
      no_log: true

  - name: 'Write out credentials'
    template:
      dest: './session_credentials.yml'
      src: 'session_credentials.yml.j2'

  - name: 'Write out boto config file'
    template:
      dest: './boto3_config'
      src: 'boto_config.j2'
