How to use aws-cli from Ansible Tower

Ansible offers several modules to handle with the AWS services.

If any time you need to execute an specific command that is not include in those modules, you can use the aws-cli tools.

An easy way to show this:

- hosts: 127.0.0.1 
  connection: local

  tasks: 
  - name: Describe Load Balancers 
    register: elbs 
    local_action: 
      module: shell aws elb describe-load-balancers

  - name: Print ELBs 
    debug: var=elbs

Great, it works!... if you have the python3 version.

$ aws --version
aws-cli/1.2.9 Python/3.4.0 Linux/3.16.0-45-generic  

What if you want to run the same playbook from Ansible Tower running with python2 ?

First of all you must install aws-cli at the Ansible Tower host.
Then Ansible Tower will attempt to run aws-cli using python2 giving an error similar to this one:

$ python2 /usr/bin/aws elb describe-load-balancers
Traceback (most recent call last):  
  File "/usr/bin/aws", line 15, in <module>
    import awscli.clidriver
  File "/usr/share/awscli/awscli/clidriver.py", line 16, in <module>
    import botocore.session

An option to solve it using -E
-E : ignore PYTHON* environment variables (such as PYTHONPATH)

- hosts: 127.0.0.1 
  connection: local

vars:  
  aws_env: 
   AWS_ACCESS_KEY_ID: "TICKETBIS" 
   AWS_SECRET_ACCESS_KEY: "ENGINEERING" 
   AWS_DEFAULT_REGION: "eu-west-1"

tasks:  
  - name: Describe Load Balancers 
    register: elbs 
    environment: aws_env 
    local_action: 
      module: shell python3 -E /usr/bin/aws elb describe-load-balancers

Special thanks to Alberto Tablado ;)