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: 
  connection: local

  - name: Describe Load Balancers 
    register: elbs 
      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/", line 16, in <module>
    import botocore.session

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

- hosts: 
  connection: local

   AWS_DEFAULT_REGION: "eu-west-1"

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

Special thanks to Alberto Tablado ;)