I am writing a cookbook which will be run on Ubuntu. It will create a directory in home of the default user.

directory "/home/<default-user>/my-directory" do
  owner <default-user>
end

The problem is, this default user is different across environments:

  • It is vagrant when running on virtual machine using Vagrant.
  • And it is ubuntu when running on EC2 instance.

What is a good practice to solve this kind of problem? And how to do it?

Thank you!

有帮助吗?

解决方案

Make the user an attribute and set that according to your environment.

directory "/home/#{node[:my_app][:default_user]}/my-directory" do
  owner node[:my_app][:default_user]
end

Then, on your attributes/default.rb file:

default[:my_app][:default_user] = 'ubuntu'

and on your Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.provision "chef_solo" do |chef|
    # ...

    chef.json = {
      "my_app" => {
        "default_user" => "vagrant"
      }
    }
  end
end

This will set your default user to ubuntu, but that will be overridden when running in the Vagrant VM.

其他提示

Checkout the configuration entry config.ssh.username: http://docs-v1.vagrantup.com/v1/docs/config/ssh/username.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top