RE: Ansible – Copying files from local machine to AWS S3 Bucket in YAML

John Q Reviewer Asked on August 14, 2019 in Linux.
Add Comment
1 Answers

I have written a simple Ansible yaml script for copying files from local to S3 bucket for restored it which deployed files in client instance. Variables are

{{SERVICE}}}, {{CONFIG_LOCATION}}, {{APP_NAME}} and {{ S3_BUCKET }} you have to specify in vars-main.yaml file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
---
- hosts: hostname
  become: yes
  become_user: username
## You can include vars_files
  vars_files:
    - vars-main.yml
## Restart the service once you made any changes
  handlers:
    - name: restart "{{ SERVICE }}"
      service: name={{SERVICE}}.service state=restarted
  tasks:
    - name: Copying credentials and confg files are from your location to .aws
      copy:
        src: "{{CONFIG_LOCATION}}/{{item}}"
        dest: /username/.aws
        owner: username
        group: username
        mode: 0600
      with_items:
        ['credentials', 'config']
 
#### S3
    - name: "copy to S3 bucket"
      s3_sync:
        bucket: your-bucket-name
        file_username: "/tmp/{{APP_NAME}}_rollback"
        key_prefix: Deployment/{{APP_NAME}}_rollback
        file_change_strategy: force
        include: "*"
        permission: private
        mode: push
 
    - name: Remove credentials and config files
      file:
        path: /username/.aws/{{item}}
        state: absent
      with_items:
        ['credentials', 'config']
Trainee Answered on August 14, 2019.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.