Tung Nguyen
Tung Nguyen

Reputation: 1924

Chef: How to use different OS default users based on environment (Vagrant, EC2)?

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:

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

Thank you!

Upvotes: 1

Views: 179

Answers (2)

cassianoleal
cassianoleal

Reputation: 2566

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.

Upvotes: 1

shawnzhu
shawnzhu

Reputation: 7585

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

Upvotes: 0

Related Questions