Search code examples
serverspec

serverspec using environment variables in Rakefile


Serverspec is used to check on several servers. Therefore the recommend structure of roles is used:

|-- Rakefile |-- spec |-- app | -- ruby_spec.rb |-- base | -- users_and_groups_spec.rb |-- db | -- mysql_spec.rb |-- proxy | -- nginx_spec.rb |-- spec_helper.rb

To read the data and structure I use a yaml-file.

On the serverspec website is in the Rakefile inside the Raketask the following:
ENV['TARGET_HOST'] = host

Why should I set the host as an environment variable? Wouldn't a local one be enough?


Solution

  • The default spec helper uses it to target hosts for the net-ssh gem. You can refactor the host targeting code in the spec_helper to not even use it if you want and then just use host_inventory for the hostname.

    Note the following:

    https://github.com/mizzy/serverspec/blob/master/lib/serverspec/setup.rb#L276 https://github.com/mizzy/serverspec/blob/master/lib/serverspec/setup.rb#L292

    Despite the anonymous downvote, this is absolutely the correct answer.