Vagrant:config.vm.provision 不允许我将文件复制到 etc/nginx/conf.d?

Vagrant: config.vm.provision does not allow me to copy a file to etc/nginx/conf.d?

我正在使用 Nginx 服务器。 我想使用 Vagrantfile 将配置文件复制到 /etc/nginx/conf.d。 我使用的命令是:

config.vm.provision "file", source: "./bolt.local.conf", destination: "/etc/nginx/conf.d/bolt.local.conf"

我收到的错误是:

Failed to upload a file to the guest VM via SCP due to a permissions
error. This is normally because the SSH user doesn't have permission
to write to the destination location. Alternately, the user running
Vagrant on the host machine may not have permission to read the file.

我正在使用 bento/ubuntu-16.04 盒子。

我试图寻找一种方法来更改配置命令的权限,但我只找到了更改 config.vm.share_folder 命令的所有者的方法。

你知道答案吗?

正如错误消息所暗示的那样,也来自 documentation:

The file uploads by the file provisioner are done as the SSH or PowerShell user. This is important since these users generally do not have elevated privileges on their own. If you want to upload files to locations that require elevated privileges, we recommend uploading them to temporary locations and then using the shell provisioner to move them into place.

因此 vagrant 用户(如果未修改)用于对文件进行 scp,但您无法使用它访问 /etc/

要使其正常工作,您需要将其上传到临时位置,然后使用 shell 配置器将其移动到目标目录:

config.vm.provision "file", 
  source: "./bolt.local.conf", 
  destination: "/tmp/bolt.local.conf"

config.vm.provision "shell",
  inline: "mv /tmp/bolt.local.conf /etc/nginx/conf.d/bolt.local.conf"

之所以可行,是因为 privileged 选项在 shell provisioners 上默认为真。但是有两个provisioner只是为了拷贝一个配置文件有点绕口吧?

好吧,如果文件已经在你的共享文件夹中,你可以使用 shell provisioner 将它复制到 nginx 目录中,这样你最终会得到这样的东西:

# This is the default and serve just as a reminder
config.vm.synced_folder ".", "/vagrant"
config.vm.provision "shell",
  inline: "cp /vagrant/bolt.local.conf /etc/nginx/conf.d/bolt.local.conf"