Error when using gsub method in chef recipe

183 views Asked by At

I am new to chef and have little understanding of Ruby.

I am getting below value in 'storage_conn_str'

SAS Token: #Chef::DelayedEvaluator:0x0000000006e28c80@c:/chef/cache/cookbooks/*****/recipes/*****.rb:20

In my recipe, I am repalcing '&' with "&". I have used lazy to delay the execution of variable in ruby block and resource.

Below is my recipe

key_vault_name node['key_vault_names']['Test']
end

ruby_block 'modify_token' do
  block do
    sastoken = lazy { node.run_state['SAS_Token'] }
    Chef::Log.info("SAS Token: #{sastoken}")
    modified_token = lazy { sastoken.gsub(/[&]/, '&') }
    Chef::Log.info("SAS Token after replacement: #{modified_token}")
    storage_conn_str = lazy { File.join(storage_conn_str , modified_token)}
    Chef::Log.info("storage connection string: #{storage_conn_str}")
  end
  action :run
end

webapp 'TableStorageAPI' do
source URI.join(node['binary_storage_uri'], app_node['source']).to_s
version app_version
appPoolName 'TableStorageAPI'
path '/V1/TableStorageAPI'
siteName 'SSL'
enable32Bit false
pipeline_mode :Integrated
use_servicebroker false
transform_variables(
storage_conn: lazy {storage_conn_str},
mail_to: app_node['mail_to'],
mail_from: app_node['mail_from'],
smtp_host: node['tps']['smtp']['server'],
log_location: app_node['log_location'],
env_name: app_node['env_name']
)
end```
----------------------------------------------
I am not sure why webapp resource is executing before the computation of 'storage_conn_str'.
1

There are 1 answers

1
Abhishek Prusty On

"I am not sure why webapp resource is executing before the computation of 'storage_conn_str'."

That's because storage_conn_str is defined within ruby_block 'modify_token'. It is not visible to webapp resource.

What you can do is, create a attribute instead.

eg: node["mycb"]["storage_conn_str"] = ""

In ruby_block

ruby_block 'modify_token' do
  block do
    ...
    storage_conn_str = lazy { File.join(storage_conn_str , modified_token)}
    Chef::Log.info("storage connection string: #{storage_conn_str}")
    node.default["mycb"]["storage_conn_str"] = storage_conn_str 
  end
  action :run
end

In webapp resource

webapp 'TableStorageAPI' do
...
   storage_conn: node["mycb"]["storage_conn_str"]
...
end

This should solve the problem you are having