jsm*_*key 5 ruby chef-infra chef-recipe
我有以下代码下载文件,然后将文件的内容读入变量.使用该变量,它执行一个命令.此配方不会收敛,因为在编译阶段/ root/foo不存在.
我可以解决多个收敛和if File.exist的问题,但我想用一个收敛来做.关于如何做的任何想法?
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
Run Code Online (Sandbox Code Playgroud)
正确的解决方案是使用惰性属性:
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
Run Code Online (Sandbox Code Playgroud)
这会将文件读取延迟到写入之后.此外,我还包括该sensitive属性,因此不显示密码.
您可以在编译时使用 run_action 下载文件,并将第二部分包装在将在运行时执行的条件块中。
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
Run Code Online (Sandbox Code Playgroud)