Test Kitchen - How can I pass the local user login in as a Chef attribute?

348 Views Asked by At

Our development environment includes local VMs running queries to larger, shared resources in the cloud.

It was set up in such a way that the local user's login would be passed into a Chef role as an attribute, which is used to define SSH and DB connections.

We currently use Ruby in the Vagrantfile to pluck out the login and vm.provision to create a /tmp file on the guest VM's filesystem. Our Chef role for the local VMs reads the contents of that file and passes the value into an attribute.

Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

(snip)

require 'etc'
user = Etc.getlogin

(snip)

analytics.vm.provision :shell, inline: "echo #{user} > /tmp/remote_user"

During the Chef run, The contents of /tmp/remote_user are read by the following:

remote_user = File.open("/tmp/remote_user", "r").read.strip
sandbox = "_#{remote_user}"

default_attributes "remote_user" => remote_user,
    "sandbox" => sandbox,

Is there a better way to add the current user's login as an attribute to a local Chef run?

If not, how can I replicate the functionality I had in our old Vagrantfile in a .kitchen.yml?

1

There are 1 best solutions below

1
On BEST ANSWER

Test Kitchen processes .kitchen.yml via ERB, so you could just simply put something like this in your ERB file:

attributes:
  foo: <%=  ENV['STUFF'] %>

And then node['foo'] would contain $STUFF. Switch STUFF to USER in your case.