1
votes

Writing my first vagrant chef solo recipe I figured something simple, like changing the hostname. In the vagrant file, I have this;

  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "../../chef/cookbooks"
    chef.roles_path = "../../chef/roles"
    chef.data_bags_path = "../../chef/data_bags"

    chef.json = { :hostname => "foofoo" }
    chef.add_recipe "myrecipe"

in myrecipe/recipes/default.rb;

myvar = node[:hostname]

template "/tmp/myfile" do
    source "myfile.erb"
    owner "root"
    group "root"
    mode 0644
    variables(
        :myvar => myvar
    )
end

in myrecipe/templates/default/myfile.erb;

<%= @myvar %>

This wouldn't actually work. /tmp/myfile kept ending up with the real hostname from /etc/hostname. After banging my head a long while, I hit on these changes;

in the vagrant file;

chef.json = { :myhostname => "foofoo" }

in the recipe file;

myvar = node[:myhostname]

So, it seems ':hostname' is special somehow, but try as I might, I can't find any documentation that explains why, or how to override it, or what other 'special' values there might be available.

Most of the documentation seems to assume some level of competence I haven't reached yet. So, I might have seen the answer and not recognized it. Can anyone point me to the 'absolute beginner' docs that would explain this?

1

1 Answers

1
votes

First: this were exactly my first experiments with chef-solo too and: changing the hostname ist not the simplest thing one can do (because for the rest of the Chef run the old hostname remains so you have to do ugly things to restart Chef etc...) ;-)

Second: node['hostname'] is special indeed (it's a so-called automatic attribute), it gets updated from the target host via ohai so you can use it in your templates e.g..

Edit: You may use this cookbook for applying the hostname.