使用加壳器将文件从主机复制到生成的图像而无需密码

Using packer to copy file from host to generated image without password

我目前正在使用加壳器根据给定配置生成自定义图像。加壳程序 .json 文件包含在 this packer tutorial 中描述的条款。

我没有在那里输入命令,而是使用了 shell 选项,我可以在其中编写一堆 sudo apt-get install 命令来自定义图像。

问题是我需要从我拥有的计算机上将文件复制到图像中。需要明确的是,我拥有的计算机也是我 运行 命令 packer build example.json.

在shell脚本中,如何进行安全拷贝,使得从新建镜像的角度,镜像可以安全的复制文件从我的计算机到它自己,无需 输入密码?这是一个 shell 脚本,所以如果我想输入,我无法输入。

我知道为了避免输入密码,我需要 public/private 密钥验证。在 shell 脚本中,我有:

sudo ssh-keygen -t rsa -b 2048
sudo scp ~/.ssh/id_rsa.pub user@example.com:/home/user/.ssh/uploaded_key.pub
sudo ssh user@example.com "echo `cat ~/.ssh/uploaded_key.pub` >> ~/.ssh/authorized_keys"

(摘自 the example here 和其他地方。我的理解是生成的图像是 运行 这些命令。)

这个问题以及我在 Whosebug 上看到的许多方法(例如 this related question)的问题是两种情况之一。

A 使用 "file" 提供类型,但我想使用 "shell" 类型执行此操作,但我不确定如何同时使用文件和shell 个选项。

我该如何解决这个问题?

您应该使用 file 配置器,例如:

"provisioners": [
  {
    "type": "file",
    "source": "source_file",
    "destination": "dest"
  },
  {
    "type": "script",
    "inline": [ "echo do something here" ]
  }
]

documentation: provisioners