# Adding Files and Folders to S3 using Ansible

# What you’ll learn

In this tutorial, you’ll learn how to use Ansible to add some files and folders to your buckets on S3. This tutorial works for LocalStack as well as for AWS. The same process works for adding files and folders consistently into any environment.

# Prerequisites

# Why Ansible

We touched briefly on it in a recap section of Adding Files and Folders to S3 using Commandeer tutorial. Adding files and folders with Commandeer is a great way to add some files here and there. That being said if you need to add a lot more files or you would like to add the same files in the exact same way to multiple AWS accounts, you’ll need an IaC tool like Ansible.

# Choose LocalStack or AWS

Feel free to choose between LocalStack or AWS in the account select dropdown. If you have the local account selected, your Ansible configuration will apply to LocalStack. On the other hand, if you have AWS selected, you’ll run against a real AWS account.

# Write Ansible

Navigate to the Ansible section in Commandeer. It’s located under the Infrastructure section in the side navigation pane. We’ll need to create a couple of files.

# Create Tank Configuration File

First, let’s create a configuration file for our A1 tank. Click on the New File button. In the folder of your choice create a file with the name a1.json. Please make sure the file has the name a1 with the extension .json since we’ll reference it from our main Ansible configuration. Once the file is open, add the following content to the file and click Save.

{
  "name": "a1",
  "status": "active",
  "health": 100
}

# Create Ansible Configuration File

Next, let’s create our Ansible configuration. Click on the New File button. In the same folder from where we just created a1.json file, create a new file with the name tanks-bucket.yml. Add the following content to the file and click save.

- hosts: localhost
  tasks:
  - name: Create the S3 Tanks bucket
    s3_bucket:
      name: commandeer-tanks-test
      versioning: yes
      tags:
        name: Tanks
        type: demo
  - name: Upload tank A1 configuration
    aws_s3:
      bucket: commandeer-tanks-test
      object: tanks/a1.json
      src: "./a1.json"
      mode: put
  - name: Create a hangar folder for tanks
    aws_s3:
      bucket: commandeer-tanks-test
      object: tanks/hangar
      mode: create

Full code is located under the Ansible folder in Commandeer Open Source Github repo.

# Run Ansible

Make sure you saved the file and click the deploy button. You’ll see some output in the results pane.

Ansbile finished running and created some resources

Once your Ansible finished running, head over to the S3 screen to verify our new files and folders are created. Choose S3 under the AWS menu in the side navigation panel. Click the refresh button and Voila! Our new file and folders are there!

Ansible created a1.json file inside the tanks folder

Our test bucket was also created by Ansible

# Recap

We just learned a very powerful way of adding multiple files and folders to your S3 buckets using Ansible. Once you write your configuration, you can run it in any environment. Just switch your account in the Account dropdown and run it again. The same configuration will be applied in a different account consistently. Using Ansible for adding files and folders is a great way to go about it if you know the file/folder structure in advance and if you need consistency. On the other end, if you’re experimenting with just a few files and having consistency isn’t a requirement, you can create some files and folders using Commandeer with this tutorial.