1

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!

Tung Nguyen
  • 1,874
  • 3
  • 18
  • 28

2 Answers2

1

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.

cassianoleal
  • 2,556
  • 19
  • 22
  • Hey cbl, your solution works nicely for me! Actually I am doing a bit differently by making use of environments: `local` environment where `default_user` is `vagrant`; `ec2` environment where that user is `ubuntu`. And I use `chef.environment` in `Vagrantfile`. Thank you very much! – Tung Nguyen Oct 09 '13 at 16:58
  • Yes, your solution looks good too. I'm glad to help. If you don't mind, please mark my answer as accepted. It both increases my points and helps others who might come to this question to go straight to the working answer. Thanks! – cassianoleal Oct 09 '13 at 19:26
0

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

shawnzhu
  • 7,233
  • 4
  • 35
  • 51
  • shawnzhu, thank you. That config does not seem to solve the problem since: (1) I set `config.ssh.username = "ubuntu"` but when I ssh into the virtual machine and issue whoami command, I get "vagrant". (2) I am looking for a solution where I can use the same cookbook and the user is still "vagrant" for virtual machine and it is "ubuntu" when running with EC2 instance – Tung Nguyen Oct 08 '13 at 02:15