使用Ansible将本地文件复制到远程AWS EC2实例

时间:2017-02-21 00:09:32

标签: amazon-web-services amazon-ec2 ansible packer

我尝试使用Packer和Ansible构建AWS AMI来配置我的AMI。我不能使用Ansible将一些本地文件复制到我新发布的EC2实例上。我在Ansible中使用copy模块来执行此操作。这是我的Ansible代码的样子:

    - 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."}

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

对于我的主机文件,我只有localhost,因为打包器告诉Ansible它需要知道运行Ansible命令的位置。

我不确定从何处开始或如何正确调试此错误,所以我希望能提供一些帮助。

这是我的Packer脚本的样子:

  {
  "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文件:

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

1 个答案:

答案 0 :(得分:2)

您正在使用ansible-local provisioner直接在目标上运行剧本(HashiCorp的产品中的“本地”,如Vagrant,Packet用于描述配置机器的观点)。

目标没有/tmp/test.test文件,因此您收到错误。

您实际上想要使用常规Ansible provisioner来运行剧本。