2016-01-19 72 views
0

我有一些需要用戶交互的命令,以便我可以在Enter的默認設置下輸入時間,或者從廚師本身提供一些輸入,這樣我的實例在運行時就不會失敗。例如...如何在Chef中的命令上進行用戶交互?

./build-ca  # The command I am executing 
If you enter '.', the field will be left blank. 
----- 
Country Name (2 letter code) [IN]: 
State or Province Name (full name) [KA]: 

這裏按enter會做這項工作(使用默認值),而手動doint,但我怎麼能使用自動廚師這個東西,有時候我需要按y或其它值。任何幫助將不勝感激。

+0

您的命令是否可以接受參數? – Carlo

+0

使用[template](https://docs.chef.io/resource_template.html)渲染你的'openssl.cnf',然後用'-config openssl.cnf'調用'openssl'可執行文件。 – StephenKing

+0

https://supermarket.chef.io/cookbooks/ssl_certificate –

回答

0

我想你正在使用Easy RSA腳本來生成OpenVPN證書。

一個解決方案是生成./vars文件,並調用pkitoolwhich is called by the old build-ca script):

easy_rsa_path = '/usr/share/easy-rsa' # or whatever 

template "#{easy_rsa_path}/vars" # [...] 

bash 'build-ca' do 
    cwd easy_rsa_path 
    code <<-EOH 
    source ./vars && \ 
    ./clean-all && \ 
    ./pkitool --batch --initca 
    EOH 
    creates "#{easy_rsa_path}/keys/ca.crt" 
end 

一個更好的辦法是使用OpenSSL生成證書。你可以看到在openvpn cookbook實現例如:

bash 'openvpn-initca' do 
    environment('KEY_CN' => "#{node['openvpn']['key']['org']} CA") 
    code <<-EOF 
    openssl req -batch -days #{node["openvpn"]["key"]["ca_expire"]} \ 
     -nodes -new -newkey rsa:#{key_size} -sha1 -x509 \ 
     -keyout #{node["openvpn"]["signing_ca_key"]} \ 
     -out #{node["openvpn"]["signing_ca_cert"]} \ 
     -config #{key_dir}/openssl.cnf 
    EOF 
    not_if { ::File.exists?(node['openvpn']['signing_ca_cert']) } 
end 
0

如果您有沒有替代品(如傳遞參數或輸入文件的命令或腳本,如已被別人建議),你可以編寫腳本與expect的交互和使用bash資源從您的廚師食譜中運行它。

請參閱How to automate user interactive command in chef recipe

+0

謝謝.. !!這有助於解決我的問題! –

相關問題