简体   繁体   中英

Passing the value from a ruby block to a resource in chef

I am writing a chef resource, which will generate a password, and I am calling that resource in a recipe with the list of inputs.

Following is my scenario: Once my resource got executed, a new set of passwords will be generated in a folder, and I want to retrieve that password which is newly generated. But I am unable to retrieve that password because the value I am trying to retrieve is executing at the convergence phase.

Simple code block to explain my scenario:

Chef::Log.info("Creating new keys")
create_password 'New Password is being generated' do
   action :change_passwords
   password_util_dir node[:password][:passwd_util_dir]
   rgbu_chef node[:password][:rgbu_chef]
   old_data_bags node[:password][:old_data_bags]
   new_data_bags node[:password][:new_data_bags]
end

The code above will create new passwords in a folder.

Later, I am trying to take the passwords through a JSON Parser:

text =::File.read("#{new_password_dir}")
data_hash = JSON.parse(text)
new_wls_password = data_hash['rase_wlsadmin_pwd']

The #{new_password_dir} is the directory location of the newly created password.json file.

I am trying to use the value of "new_wls_password" in the another resource like below:

Chef::Log.info("Updating WLSADMIN Password")
passwd_backup 'Updating wlsadmin password' do
  action :update_wlsadmin
  osuser node[:password][:wls_config_user]
  usergroup node[:password][:wls_install_group]
  new_wls_password "#{new_wls_password}"
end

Here, the new password which I am trying to retrieve is empty, since the following three lines are executed in the first place:

text =::File.read("#{new_password_dir}")
    data_hash = JSON.parse(text)
    new_wls_password = data_hash['rase_wlsadmin_pwd']

So, by that time, the new passwords resource has not been run.

I tried many stack overflow suggestions, like:

  1. putting those three lines in a ruby_block like this
ruby_block "new_password" do
    block do
        text =::File.read("#{new_password_dir}")
        data_hash = JSON.parse(text)
        node.set[:new_wls_password] = data_hash['rase_wlsadmin_pwd']
    end
end

Then I tried fetching the value into the resource as below

Chef::Log.info("Updating WLSADMIN Password")
    passwd_backup 'Updating wlsadmin password' do
      action :update_wlsadmin
      osuser node[:password][:wls_config_user]
      usergroup node[:password][:wls_install_group]
      new_wls_password "#{node[:new_wls_password]"
    end

With the above approach still the value is empty

  1. Trying the value with lazy and calling that value.

  2. Passing the value from one ruby block to another ruby block, which I can do, but not with the resources.

Please, can you help?

EDIT #1 : I need to pass the value from the resource to the template. Something like this, after running the following resource:

Chef::Log.info("Creating new keys")
create_password 'New Password is being generated' do
   action :change_passwords
   password_util_dir node[:password][:passwd_util_dir]
   rgbu_chef node[:password][:rgbu_chef]
   old_data_bags node[:password][:old_data_bags]
   new_data_bags node[:password][:new_data_bags]
end

A new set of passwords will be generated in a folder, like the /tmp/password.json file.

After the resource execution above I am writing a template like:

template "#{Chef::Config[:file_cache_path]}/domain.properties" do 
source 'domain_properties.erb' 
variables 
({ :domain_name => "#{domain_name}", 
   :admin_url => "#{admin_url}", 
   :new_wls_password => "#{new_wls_password}" })
end

Here, how can I parse the newly created value of "new_wls_password" ?

You can use lazy attribute like below:-

Chef::Log.info("Updating WLSADMIN Password")
passwd_backup 'Updating wlsadmin password' do
  action :update_wlsadmin
  osuser node[:password][:wls_config_user]
  usergroup node[:password][:wls_install_group]
  new_wls_password lazy { JSON.parse(File.read("/tmp/password.json"))['rase_wlsadmin_pwd'] }
end

Template resource can be written as:-

template "#{Chef::Config[:file_cache_path]}/domain.properties" do 
   source 'domain_properties.erb' 
   variables (lazy{{ :domain_name => "#{domain_name}", 
       :admin_url => "#{admin_url}", 
       :new_wls_password => JSON.parse(File.read("/tmp/password.json"))['rase_wlsadmin_pwd'] }})
end

Output:-

     * template[/tmp/kitchen/cache/domain.properties] action create
       - create new file /tmp/kitchen/cache/domain.properties
       - update content in file /tmp/kitchen/cache/domain.properties from none to fa22e0
       --- /tmp/kitchen/cache/domain.properties 2017-01-12 03:30:13.002968715 +0000
       +++ /tmp/kitchen/cache/.chef-domain20170112-11387-1ytkyk2.properties 2017-01-12 03:30:13.002968715 +0000
       @@ -1 +1,4 @@
       +domain_name= mmm
       +admin_url= nnn
       +new_wls_password= xH#3zIS9Q4Hc#B

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM