2

My plan is to copy file from ec2 to s3 bucket using ansible, Here I've make playbook but getting some error:

copy2s3.yml

---
- name: Copy to s3
   s3:
     aws_access_key: "{{ lookup('env','aws_key') }}"
     aws_secret_key: "{{ lookup('env','aws_secret') }}"
     bucket: "{{ aws_packages_bucket }}"
     object: "/JI79IML/my_part_X86_64_c7.15.tar.gz"
     dest: "/data/parts/JI79IML/my_part_X86_64_c7.15.tar.gz"
     mode: get
     overwrite: no

Getting below error:

$ ansible-playbook copy2s3.yml -i 172.18.2.12,

 ERROR! 's3' is not a valid attribute for a Play

The error appears to have been in '/home/ubuntu/bk/copy2s3.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: Copy to s3
^ here

6 Answers 6

2

Module name (s3) should be at the same indentation level as the name:

- name: Copy to s3
  s3:
    aws_access_key: "{{ lookup('env','aws_key') }}"
    aws_secret_key: "{{ lookup('env','aws_secret') }}"
    bucket: "{{ aws_packages_bucket }}"
    object: "/JI79IML/my_part_X86_64_c7.15.tar.gz"
    dest: "/data/parts/JI79IML/my_part_X86_64_c7.15.tar.gz"
    mode: get
    overwrite: no
4
  • I use same but still getting same error, showing YAML syntax ok !, Is need to install s3 plugins in ansible? Apr 4, 2017 at 4:33
  • If syntax is ok, then, what other error are you getting? S3 module should be available by default. Also, try updating your Ansible and boto libraries: pip install -U ansible boto. Apr 4, 2017 at 9:20
  • I resolve issue with above but need to write hosts, tasks, connection and gather_facts above on script then its works Apr 4, 2017 at 9:25
  • Yes, the above is just a single task, if you want to run it as a playbook, you'll need the parameters you described. Apr 4, 2017 at 9:29
0

To Copy Object from Local Server to S3 using Ansible module, Use

mode: put

get will be used to download the object.

Reference

0

I had a similar issue when using aws_s3, the replacement module for s3.

Check to see if you have boto (for s3 and aws_s3) and boto3 (for aws_s3) correctly installed.

I had both boto & boto3 installed but, due to playing with virtual environments, they were only installed for Python3.5 and no other versions of python. Thus python (Python2.7 on my setup) that Ansible uses could not import the boto's and failed with this very esoteric error message.

To ensure that all is correctly installed is run python on the command line and try to import the boto's manually:

13:20 $ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto
>>> import boto3
>>> 
13:21 $ python3
Python 3.5.2 (default, Sep 14 2017, 22:51:06) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto
>>> import boto3
>>> 

If you receive an error in python then you get the error in Ansible.

0

Add task: above - name like this:

---
- hosts: localhost
  tasks:
    - name: Copy to s3
      s3:
          aws_access_key: "{{ lookup('env','aws_key') }}"
          aws_secret_key: "{{ lookup('env','aws_secret') }}"
          bucket: "{{ aws_packages_bucket }}"
          object: "/JI79IML/my_part_X86_64_c7.15.tar.gz"
          dest: "/data/parts/JI79IML/my_part_X86_64_c7.15.tar.gz"
          mode: put
          overwrite: no
0
0

this source maybe helps you

In this source code, there are 4 major tasks.

a. Get a list of directories in your S3 bucket.

b. Create a directory structure on the machine of Your S3 bucket.

c. Download files and Directories From the S3 bucket into an already created directory structure.

d. Provide access privileges to your downloaded S3 buckets files.

create varlist.yml and add variable

1 . Bucket_name 2 . aws_access_key 3 . aws_secret_key

---
 - name: Bucket copy
   hosts: localhost
   become_method: sudo
   become_user: root
   become: true
   gather_facts: False
   vars_files:
     - varlist.yml
   tasks:
     - name: Get s3 objects     # Make list of directory and files in register
       aws_s3:
         bucket: "{{ Bucket_name }}"
         mode: list
         aws_access_key: "{{ aws_access_key }}"
         aws_secret_key: "{{ aws_secret_key }}"
       register: s3_object_list
     - name: Create download directory  # Create directory for download latest code on s3 bucket 
       file:
         path: "S3/{{ item }}"
         state: directory
       with_items:
         - "{{ s3_object_list.s3_keys }}"
       ignore_errors: true
     - name: Download s3 objects       # Download files in there appropriate directory on serverside  
       aws_s3:
         bucket: "{{ Bucket_name }}"
         object: "{{ item }}"
         mode: get
         dest: "S3/{{ item }}"
         aws_access_key: "{{ aws_access_key }}"
         aws_secret_key: "{{ aws_secret_key }}"
       with_items:
         - "{{ s3_object_list.s3_keys  }}"
       ignore_errors: true
     - name: Folder permissions
       file:
         path: S3/*                                         
         state: touch
         mode: "u=rw,g=r,o=r"
1
  • 1
    An explanation would also help.
    – RalfFriedl
    Oct 5, 2019 at 7:00
0

this source maybe helps you

  ---
    - name: Testing windows modules Download S3
      hosts: hostIp
      gather_facts: false
      vars_files:                                      # Import varibale list 
        - varlist.yml
      tasks:
        - name: Download s3 objects       # Download files in the there appropriate directory on the server-side  
          aws_s3:
            bucket: "{{ Bucket_name }}"
            object: "{{item.component_download_path}}/{{ item.component_zip_name }}"
            dest: 'download/path'
            mode: get
            aws_access_key: "{{ aws_access_key }}"
            aws_secret_key: "{{ aws_secret_key }}"
          with_items:
            - "{{ components }}"

varlist.yml


    Bucket_name: bucketname       
    aws_access_key: key
    aws_secret_key: key
    base_path: /opt/
    components:
      - component_name: xyz
        component_zip_name: edf
        component_install_path: install/application/
        component_download_path: applications

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .