使用Ansible将本地文件复制到远程AWS EC2实例 [英] Copy local file to remote AWS EC2 instance with Ansible

查看:443
本文介绍了使用Ansible将本地文件复制到远程AWS EC2实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试同时使用Packer和Ansible来构建AWS AMI,以配置我的AMI.我一直无法使用Ansible将某些本地文件复制到新启动的EC2实例中.我正在使用Ansible中的copy模块来执行此操作.这是我的Ansible代码的样子:

I'm attempting to build an AWS AMI using both Packer and Ansible to provision my AMI. I'm getting stuck on being able to copy some local files to my newly spun up EC2 instance using Ansible. I'm using the copy module in Ansible to do this. Here's what my Ansible code looks like:

    - name: Testing copy of the local remote file
      copy:
        src: /tmp/test.test
        dest: /tmp

这是我得到的错误:

 amazon-ebs: TASK [Testing copy of the local remote file] ***********************************

 amazon-ebs: fatal: [127.0.0.1]: FAILED! => {"changed": false, "failed": true, "msg": "Unable to find '/tmp/test.test' in expected paths."}

我已验证文件/tmp/test.test在运行Ansible的本地计算机上是否存在.

I've verified that the file /tmp/test.test exists on my local machine from which Ansible is running.

对于我的宿主文件,我只包含localhost,因为打包程序告诉Ansible它需要了解在哪里运行Ansible命令的所有信息.

For my host file I just have localhost in it since packer is telling Ansible everything it needs to know about where to run Ansible commands.

我不确定该从何处去或如何正确调试此错误,因此希望能提供一些帮助.

I'm not sure where to go from here or how to properly debug this error, so I'm hoping for a little help.

这是我的Packer脚本的样子:

Here's what my Packer script looks like:

  {
  "variables": {
    "aws_access_key": "{{env `access_key`}}",
    "aws_secret_key": "{{env `secret_key`}}"
  },
  "builders": [{
    "type": "amazon-ebs",
    "access_key": "{{user `aws_access_key`}}",
    "secret_key": "{{user `aws_secret_key`}}",
    "region": "us-east-1",
    "source_ami": "ami-116d857a",
    "instance_type": "t2.micro",
    "ssh_username": "admin",
    "ami_name": "generic_jenkins_image",
    "ami_description": "Testing AMI building with Packer",
    "vpc_id": "xxxxxxxx",
    "subnet_id": "xxxxxxxx",
    "associate_public_ip_address": "true",
    "tags": {"Environment" : "Dev", "Product": "SharedOperations"}
  }],
  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "sleep 30",
        "sudo rm -f /var/lib/dpkg/lock",
        "sudo apt-get update -y --fix-missing",
        "sudo apt-get -y install libpq-dev python-dev libxml2-dev libxslt1-dev libldap2-dev libsasl2-dev libffi-dev gcc build-essential python-pip",
        "sudo pip install ansible"
      ]
    },
    {
      "type": "ansible-local",
      "playbook_file": "ansible/main.yml"
    }
  ]
}

这是我的整个Ansible文件:

And here's my entire Ansible file:

---
- hosts: all
  sudo: yes
  tasks:
    - name: Testing copy of the local remote file
      copy:
        src: /tmp/test.test
        dest: /tmp

推荐答案

您正在使用

You are using ansible-local provisioner which runs the playbooks directly on targets ("local" in HashiCorp's products like Vagrant, Packet is used to describe the point of view of the provisioned machine).

目标没有/tmp/test.test文件,因此会出现错误.

The target does not have the /tmp/test.test file, hence you get the error.

您实际上想使用常规的 Ansible设置程序来运行该剧本.

You actually want to run the playbook using the regular Ansible provisioner.

这篇关于使用Ansible将本地文件复制到远程AWS EC2实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆