Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chef: Read variable from file and use it in one converge

I have the following code which downloads a file and then reads the contents of the file into a variable. Using that variable, it executes a command. This recipe will not converge because /root/foo does not exist during the compile phase.

I can workaround the issue with multiple converges and an

if File.exist

but I would like to do it with one converge. Any ideas on how to do it?

execute 'download_joiner' do
  command "aws s3 cp s3://bucket/foo /root/foo"
  not_if { ::File.exist?('/root/foo') }
end

password = ::File.read('/root/foo').chomp

execute 'join_domain' do
  command "net ads join -U joiner%#{password}"
end
like image 976
jsmickey Avatar asked Feb 26 '16 17:02

jsmickey


1 Answers

The correct solution is to use a lazy property:

execute 'download_joiner' do
  command "aws s3 cp s3://bucket/foo /root/foo"
  creates '/root/foo'
  sensitive true
end


execute 'join_domain' do
  command lazy {
    password = IO.read('/root/foo').strip
    "net ads join -U joiner%#{password}"
  }
  sensitive true
end

That delays the file read until after it is written. Also I included the sensitive property so the password is not displayed.

like image 196
coderanger Avatar answered Sep 28 '22 04:09

coderanger