Reputation: 811
Part of a Chef cookbook I'm writing is configuring perforce, which requires the user to enter their password (lest they save this in plaintext in an "attributes" file). Is it possible to interrupt the provisioning with an interactive prompt?
Upvotes: 4
Views: 1609
Reputation: 2586
We would prompt the user from the Vagrantfile and then set that value as a Chef attribute. Prompts really only makes sense on dev boxes, so they really shouldn't be part of the Chef recipe:
Vagrant.configure('2') do |config|
config.vm.provision :chef_client do |chef|
chef.add_role 'dev'
chef.chef_server_url = 'https://api.opscode.com/organizations/myorg'
chef.node_name = "vagrant-#{ENV['USER']}-dev.example.com"
chef.validation_key_path = 'example-validator'
chef.json = {
'mysvc' => {
'password' => MySvc.password()
}
}
end
end
module MySvc
def self.password
begin
system 'stty -echo'
print 'MySvc Password: '
; pass = $stdin.gets.chomp; puts "\n"
ensure
system 'stty echo'
end
pass
end
end
Upvotes: 4
Reputation: 612
Correct me if i misunderstood your problem, if you want to read user input :
You can use built-in SHELL command "read".
example:
[myprompt]$ read -p "Insert text : " IN_TEXT
Insert text : user input
[myprompt]$ echo $IN_TEXT
user input
ps: if you use read command for password you can use "-s" option to hide input coming from terminal.
example2 :
[myprompt]$ read -sp "Insert text : " IN_TEXT
Insert text : //stdin <<"user input"
[myprompt]$ echo $IN_TEXT
user input
Upvotes: 3