How Can I Use AWS CLI Through Ansible Automation Platform?

Solution Verified - Updated -

Environment

  • Red Hat Ansible Automation Platform

Issue

  • In some instances, it may be necessary to utilize AWS CLI to get the full berth of features available in AWS. You can utilize playbooks from AAP controller to automate these features.

Resolution

  • AWS CLI needs to be installed on the control machine:

    # yum install -y awscli
    
  • Next, AWS access keys must be made available to the playbook. To retrieve the AWS access keys, a lookup can be used to pull the credentials from the job environment. When an AWS credential is added to a job template, the AWS Access Key is appended as an environment variable. This plugin allows the variable to be referenced within a task for use with the AWS CLI. Documentation for the plugin can be found here

  • Sample syntax for this is as follows:

    ---
    - hosts: localhost
      vars:
        AWS_ACCESS_KEY_ID: "{{ lookup('env','AWS_ACCESS_KEY_ID') }}"
    

    This variable will retrieve the access key from the environment and make it reusable.

  • Finally, once the access key is available to the playbook, AWS CLI commands can be executed using the shell module.

  • One of the other approaches is to include awscli as a Python package inside the execution environment by adding awscli to the requirements.txt file and rerunning the builder. Please check the article to know more about How to create a new execution environment for Ansible Automation Platform?

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments