AWS Logo
Menu
Automate AWS CLI Deployment Using Ansible – Step by Step

Automate AWS CLI Deployment Using Ansible – Step by Step

Leverage the power of Ansible to quickly prepare and install the AWS CLI in your environment, enabling you to get started with AWS faster than ever.

Published Jan 10, 2025
Today I'm here to show you how we can install the AWS CLI tool using Ansible via terminal using the AWS CLI (Command Line Interface). There are two ways to work with AWS services, one through the console, and the other through the terminal. I personally prefer using the AWS CLI because it allows you to do everything faster than through the web console and there are even more features through the AWS CLI than through the web console. You'll see that throughout this article I'll show you some very specific cases. The AWS CLI covers all AWS services for use via the command line.

AWS CLI Command Structure

$ aws <command> <subcommand> [options and parameters]
A few things to note:
  • The basic call to the aws program.
  • The top-level command that typically corresponds to an AWS service supported by the AWS CLI.
  • The subcommand that specifies the operation to be performed.
  • The general CLI options, or parameters required for the operation. You can specify these in any order, as long as you follow the first three parts. If a unique parameter is specified multiple times, only the last value applies.
There are a few ways in which AWS CLI commands output, which are:
  1. --output json
  2. --output table
  3. --output text
See this example below using the json output:

What is Ansible Automation?

Before showing the syllabus of what we are going to learn, it is worth explaining what this incredible tool is and how it works. Ansible is a tool for automating, configuring and orchestrating servers, and is maintained by Red-Hat and what's more, it is open-source.
There are other configuration managers such as Puppet and Chef, which are also excellent to work with. I considered Ansible because it does not require an agent on the host that will be reached, it only uses an SSH connection and you can already see how fantastic it is, leaving everything organized, reaching hundreds of machines, where it is also possible to make inventories and organize everything into groups, it is amazing, “sensational”.
Ansible Automation
Ansible Automation
It is rich in features and functionalities, with many modules to use, such as cloud providers (AWS, Azure, GCP), Docker, Grafana, Zabbix, VMware, MySQL, Postgres, Linode, among others. We can also use the ansible-galaxy command where it is possible to download and use playbooks written by other users in the community.

Ansible Use-Cases

  • Provisioning;
  • Configuration management;
  • Continuous delivery;
  • Security;
  • Orchestration;
  • Application deployment.

A little about the Ansible structure:

Inventory: this is the file that contains the list of hosts that will be managed by Ansible.
Modules: these are the resources (services, packages, files) that Ansible will use to control and manage the hosts.
Tasks: these are the tasks that will be executed on the hosts. We can also use the task mode called ad-hoc (commands executed directly from the command line without playbooks).

Step by Step to use Ansible for AWS CLI Deployment

Now that we are know some main points of AWS CLI and Ansible, so let's get started with AWS CLI installations using Ansible.

Ansible Playbook

Check out below my Ansible playbook to install the AWS CLI on an Ubuntu-based Linux system. Some tasks the Ansible Playbook performs:
  • Updates the APT package index.
  • Installs curl and unzip.
  • Downloads the AWS CLI installer.
  • Unzips the installer.
  • Installs the AWS CLI.
  • Verifies the installation and displays the version.
In my case, I had already installed the AWS CLI before, but I prefer to display the debug message on the end to show AWS CLI version. 😝
See my PLAY RECAP, the summary output shown at the end of an Ansible playbook run. It provides a high-level overview of the results of the executed tasks across all targeted hosts.
PLAY RECAP Ansible
PLAY RECAP Ansible
The aws configure command is the easiest way to configure your AWS CLI installation, here's an example:
Now that we have configured the AWS CLI, let's run a few commands so you can see how powerful this AWS feature is. In the example below, I am showing the default output in json format, which I defined in the AWS CLI configuration, and I am describing the IDs of my instances:
Now listing the VPC ID of my instances:
That's all!! ☁️☁️☁️
 

Comments