Chef: Read variable from file and use it in one converge
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.
You can download the file at compile time by using run_action and wrap the second part in the conditional block which will be executed at run time.
execute 'download_joiner' do
command "aws s3 cp s3://bucket/foo /root/foo"
not_if { ::File.exist?('/root/foo') }
end.run_action(:run)
if File.exist?('/root/foo')
password = ::File.read('/root/foo').chomp
execute 'join_domain' do
command "net ads join -U joiner%#{password}"
end
end