我的计划是使用 ansible 将文件从存储桶复制到ec2
存储s3
桶,这里我制作了剧本,但出现了一些错误:
复制2s3.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
出现以下错误:
$ 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
答案1
模块名称 (s3) 应与以下缩进级别相同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
答案2
答案3
我在使用 s3 的替换模块 aws_s3 时遇到了类似的问题。
检查您是否正确安装了 boto(适用于 s3 和 aws_s3)和 boto3(适用于 aws_s3)。
我安装了 boto 和 boto3,但是由于使用虚拟环境,它们只安装了 Python3.5,没有安装其他版本的 python。因此,Ansible 使用的 python(我的设置中的 Python2.7)无法导入 boto,并且失败并显示此非常深奥的错误消息。
为了确保所有内容都正确安装,请在命令行上运行 python 并尝试手动导入 boto:
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
>>>
如果您在 python 中收到错误,那么您在 Ansible 中也会收到错误。
答案4
这个来源也许对你有帮助
在这个源代码中,有4个主要任务。
A。获取 S3 存储桶中的目录列表。
b.在您的 S3 存储桶的计算机上创建目录结构。
C。将文件和目录从 S3 存储桶下载到已创建的目录结构中。
d.提供对您下载的 S3 存储桶文件的访问权限。
创建 varlist.yml 并添加变量
1.存储桶名称 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"