#1

Hi,

How to prompt in chef attributes ?
I need to ask from a user whether they need to keep the default value
which i described in attribute file or they need to override with their
new value and get and process in that value itself.
I used get.chomp in ruby_block
#!/usr/bin/ruby
require 'rubygems’
ruby_block “Transfer bits” do
puts node[‘trans’][‘host’]
block do
HOST = node[‘trans’][‘host’]
puts HOST

puts "User need to override"
ip1 = gets.chomp
if ip1 == “y” || ip1 == "Y"
puts "Enter ur ip"
ip2 = gets.chomp
puts ip2
else
puts HOST
end
end
end

Like this but it shows:
Errno::ENOENT
172.16.1.121
172.16.1.121 -------------
172.16.1.121
172.16.1.121 No such file or directory - -o
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121 Cookbook Trace:
172.16.1.121
172.16.1.121 ---------------
172.16.1.121
172.16.1.121 /var/chef/cache/cookbooks/trans/recipes/default.rb:166:in gets' 172.16.1.121 172.16.1.121 /var/chef/cache/cookbooks/trans/recipes/default.rb:166:ingets’
172.16.1.121
172.16.1.121 /var/chef/cache/cookbooks/trans/recipes/default.rb:166:in `block (2 levels) in from_file’
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121

Thanks,
Indra K


#2

When you say “user” are you talking about somebody sitting at the client computer, or are you talking about the administrator who manages the cookbooks etc. on the chef server?

On the chef server, there are several ways to do what you want: you can override the attribute using roles, or put it in a data bag, or set the attributes using knife.

If you want a user on the client machine to do this – chef is probably not the right tool for you. The whole point of chef is that it runs unattended (and many people run it automatically every 30 minutes, so even if you could prompt a user, that would happen twice every hour).

You could probably rig something using a “flag” file – the user creates a particular file in a particular location, and then your chef recipe checks if that file exists, and adjusts the attributes as needed.

A better option is probably for the user to talk to an administrator, and update the attributes on the chef server.

From: Indra k [mailto:indra.k@cloudenablers.com]
Sent: Wednesday, August 6, 2014 12:02 AM
To: chef@lists.opscode.com
Subject: [chef]

Hi,

How to prompt in chef attributes ?

I need to ask from a user whether they need to keep the default value which i described in attribute file or they need to override with their new value and get and process in that value itself.

I used get.chomp in ruby_block
#!/usr/bin/ruby
require 'rubygems’
ruby_block “Transfer bits” do
puts node[‘trans’][‘host’]
block do
HOST = node[‘trans’][‘host’]
puts HOST

puts "User need to override"
ip1 = gets.chomp
if ip1 == “y” || ip1 == "Y"
puts "Enter ur ip"
ip2 = gets.chomp
puts ip2
else
puts HOST
end
end
end

Like this but it shows:
Errno::ENOENT
172.16.1.121
172.16.1.121 -------------
172.16.1.121
172.16.1.121 No such file or directory - -o
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121 Cookbook Trace:
172.16.1.121
172.16.1.121 ---------------
172.16.1.121
172.16.1.121 /var/chef/cache/cookbooks/trans/recipes/default.rb:166:in gets' 172.16.1.121 172.16.1.121 /var/chef/cache/cookbooks/trans/recipes/default.rb:166:ingets’
172.16.1.121
172.16.1.121 /var/chef/cache/cookbooks/trans/recipes/default.rb:166:in `block (2 levels) in from_file’
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121

Thanks,

Indra K


#3

On Wed, Aug 6, 2014 at 3:02 AM, Indra k indra.k@cloudenablers.com wrote:

Hi,

How to prompt in chef attributes ?
I need to ask from a user whether they need to keep the default value
which i described in attribute file or they need to override with their new
value and get and process in that value itself.
I used get.chomp in ruby_block

Kevin is correct that the chef client is not meant to take user input to
its recipes from the command line. You could pass attributes in JSON format
in a file with “chef-client -J ./my_attributes_file.json” but this isn’t a
normal pattern.

Normally you would want to write your recipe so it can determine the
correct attribute on its own. Using search in a recipe to find another node
and get it’s IP address is common.

http://docs.getchef.com/essentials_search.html#nodes

Bryan