Skip to content

Commit

Permalink
Update on documentation and the way the playbook should work
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Sep 4, 2023
1 parent 11962a6 commit 5fa31fa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
12 changes: 9 additions & 3 deletions playbooks/UPLOAD_FILE_TO_S3.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## cloud.aws_ops.upload_file_to_s3

A playbook to upload a local file to S3.
A playbook to upload a local file to S3. When running this playbook the file to upload is expected to be located on the remote host, the controller host is responsible of the PUT operation on the S3 bucket.

## Variables

Expand All @@ -26,7 +26,7 @@ __vars.yaml__
---
aws_profile: sample-profile
upload_file_to_s3_bucket_name: my-test-bucket
upload_file_to_s3_file_path: path_to_a_valid_file
upload_file_to_s3_file_path: /path/to/file/on/remote/host
```
__playbook.yaml__
Expand All @@ -35,10 +35,16 @@ __playbook.yaml__
ansible.builtin.import_playbook: cloud.aws_ops.upload_file_to_s3
```
__inventory.ini__
```
[all]
sample_host ansible_ssh_user=some_user ansible_host=xxx.xxx.xxx.xxx
```

Run the following command:

```shell
ansible-playbook ./playbook.yaml -e "@./vars.yaml"
ansible-playbook ./playbook.yaml -e "@./vars.yaml" -i inventory.ini
```

## License
Expand Down
40 changes: 31 additions & 9 deletions playbooks/upload_file_to_s3.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Upload a local file to S3
hosts: localhost
hosts: all
gather_facts: false

module_defaults:
Expand Down Expand Up @@ -31,11 +31,33 @@
msg: 'File {{ upload_file_to_s3_file_path }} does not exist.'
when: not _stat.stat.exists

- name: Upload object into S3
amazon.aws.s3_object:
mode: put
bucket: "{{ upload_file_to_s3_bucket_name }}"
object: "{{ upload_file_to_s3_object_name | default(upload_file_to_s3_file_path | basename) }}"
src: "{{ upload_file_to_s3_file_path }}"
permission: "{{ upload_file_to_s3_object_permission | default(omit) }}"
overwrite: "{{ upload_file_to_s3_object_overwrite | default(omit) }}"
- name: Fetch file from remote host and put into S3 bucket
vars:
upload_file_to_s3_file_basename: "{{ upload_file_to_s3_file_path | basename }}"
block:
- name: Create temporary directory to download file in
ansible.builtin.tempfile:
suffix: .s3_download
register: _tempfile

- name: Download file from Remote host
ansible.builtin.fetch:
src: "{{ upload_file_to_s3_file_path }}"
dest: "{{ _tempfile.path }}"
flat: true

- name: Upload object into S3
amazon.aws.s3_object:
mode: put
bucket: "{{ upload_file_to_s3_bucket_name }}"
object: "{{ upload_file_to_s3_object_name | default(upload_file_to_s3_file_basename) }}"
src: "{{ _tempfile.path }}"
permission: "{{ upload_file_to_s3_object_permission | default(omit) }}"
overwrite: "{{ upload_file_to_s3_object_overwrite | default(omit) }}"
delegate_to: localhost

always:
- name: Delete temporary directory
ansible.builtin.file:
state: absent
path: "{{ _tempfile.path }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[all]
upload_to_s3_host ansible_connection=local ansible_python_interpreter=auto
2 changes: 1 addition & 1 deletion tests/integration/targets/test_upload_file_to_s3/runme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trap 'cleanup "${@}"' ERR
ansible-playbook setup.yaml "$@"

# Upload to S3
ansible-playbook upload_file.yaml -e "@upload_file_vars.yaml" "$@"
ansible-playbook upload_file.yaml -e "@upload_file_vars.yaml" -i ./inventory/upload_file.ini "$@"

# Validate that file has been successfully uploaded as expected
ansible-playbook validate.yaml -e "@upload_file_vars.yaml" "$@"
Expand Down

0 comments on commit 5fa31fa

Please sign in to comment.