Ansible - Getting Started with the Amazon Web Services (AWS) modules
by
Jeremy Canfield |
Updated: July 11 2023
| Ansible articles
If you are not familiar with modules, check out Ansible - Getting Started with Modules.
Prerequisites
- Before you can use the Ansible Amazon Web Services (AWS) modules, you will need to install the AWS CLI tool on the hosts that will be using the Ansible Amazon Web Services (AWS) modules. Check out my article on Getting Started with the Ansible Amazon Web Services (AWS) modules.
- You will also need to set your Amazon Web Services (AWS) Profile Configurations. Check out my article Set Amazon Web Services (AWS) Profile Configurations.
- The aws_s3_bucket_info requires the following packages. Check out my article Resolve "boto3 required for this module".
- botocore version 1.25.0 or higher
- boto3 version 1.22.0 or higher
- Python 3.6 or higher must be used. The ansible --version command can be used to list the version of Python being used with Ansible. If your Ansible installation is used a version lower than Python 3.6, one solution would be to install Ansible in a Python virtual environment using Python 3.6 or higher.
- The amazon.aws collection will need to be installed. Check out my article on Install a collection using the ansible-galaxy collection install command.
Here is a playbook that can be used to install the AWS CLI. This playbook uses the following modules and parameters.
- block - Getting Started with the block parameter
- debug - Print output to the console using the debug module
- fail - End play using the fail module
- file - Remove files and directories using the file module
- get_url - Download files from a remote system using get_url
- register - Store output JSON in variable using the register parameter
- set_fact - Create variable, list or dictionary using set_fact
- shell - Run a command using the shell module
- stat - Determine if a file or directory exists using the stat module
- unarchive - Extract a tar zip bzip2 gzip archive using the unarchive module
- uri - Determine if a remote file or directory exists using the uri module
- when - when parameter (if else statement)
---
- name: aws setup CLI play
hosts: all
tasks:
- name: use stat to determine if the AWS CLI exists
stat:
path: /usr/local/bin/aws
register: out
- debug:
var: out.stat.exists
- name: enter this block when /usr/local/bin/aws does not exist
block:
- name: set_fact aws_cli_url
set_fact:
aws_cli_url: https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip
- debug:
var: aws_cli_url
- name: use uri to determine if {{ aws_cli_url }} exists
uri:
url: "{{ aws_cli_url }}"
method: GET
register: uri
- name: display uri.status (should be 200 meaning {{ aws_cli_url }} exists)
debug:
var: uri.status
- name: fail if uri.status is NOT 200
fail:
msg: it appears that {{ aws_cli_url }} does NOT exist
when: uri.status != 200
- name: remove /tmp/awscli-exe-linux-x86_64.zip (if exists)
file:
path: /tmp/awscli-exe-linux-x86_64.zip
state: absent
- name: use get_url to download {{ aws_cli_url }} to /tmp
get_url:
url: "{{ aws_cli_url }}"
dest: /tmp
register: get_url
- name: display get_url.status_code (should be 200 meaning {{ aws_cli_url }} was downloaded to /tmp)
debug:
var: get_url.status_code
- name: fail if get_url.status_code is NOT 200
fail:
msg: it appears {{ aws_cli_url }} was NOT downloaded to /tmp
when: get_url.status_code != 200
- name: display get_url.dest (should be /tmp/awscli-exe-linux-x86_64.zip)
debug:
var: get_url.dest
- name: use unarchive to extract /tmp/awscli-exe-linux-x86_64.zip to
unarchive:
src: "{{ get_url.dest }}"
dest: /tmp
remote_src: true
register: unarchive
- name: remove /tmp/awscli-exe-linux-x86_64.zip
file:
path: "{{ get_url.dest }}"
state: absent
- name: issue the /tmp/aws/install command to install the AWS CLI
shell: /tmp/aws/install
register: shell
- name: display the shell.rc (should be 0 meaning the AWS CLI was successfully installed)
debug:
var: shell.rc
- name: display the shell.stdout (should be something like 'You can now run /usr/local/bin/aws')
debug:
var: shell.stdout
- name: remove the /tmp/aws directory
file:
path: /tmp/aws
state: absent
when: out.stat.exists == false
...
Did you find this article helpful?
If so, consider buying me a coffee over at