How Can I Use AWS CLI Through Ansible Automation Platform?

Solution In Progress - 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

  • First, AWS CLI needs to be installed on the control machine: yum install -y awscli

    Next, you'll need your AWS access keys in the playbook. In order to call the AWS access keys, you can use a lookup to pull the credential from the job environment. When you add an AWS credential to a job template, it appends the AWS Access Key as an environment variable. Using this plugin, you can call the variable to a task in order to utilize it for AWS CLI. You can find documentation referencing that plugin here

  • Sample syntax for this is as follows:

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

    This variable will pull the access key from the environment, and make it a reusable variable.

  • Lastly, once the access key is available to the playbook, you can execute awscli commands using the shell module.

  • One of the other approach is to include the awscli as python package inside Execution Environment by adding the awscli inside requiremnets.txt file and rerun 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