Can't modify frozen String


#1

I have the following piece of code in one the recipes?

chef_client_scheduled_task 'Chef Client' do
       user node['chef_client']['task']['user']
       password node['chef_client']['task']['password']
       frequency node['chef_client']['task']['frequency']
end

and when I run I keep getting

* chef_client_scheduled_task[Chef Client] action add
  ==============================================
  Error executing action `add` on resource 'chef_client_scheduled_task[Chef Client]'
  ===============================================
  RuntimeError
  ------------
  can't modify frozen String

  Resource Declaration:
  ---------------------
  # In C:/chef/cache/cookbooks/chef-client/recipes/task.rb

  27: chef_client_scheduled_task 'Chef Client' do
  28:   user node['chef_client']['task']['user']
  29:   password node['chef_client']['task']['password']

I tried changing chef_client_scheduled_task to chef_client_scheduled_task.dup after reading a few blogs but this did not help. Any pointers on how to fix this?


#2

Hmm, I have not changed to Chef 13 yet, but I think you are already using it and this is because of resource cloning, or rather that resource cloning was removed from Chef. The chef-client cookbook already contains a chef_client_scheduled_task 'Chef Client' which means you have to call your resource anything other than Chef Client and it should work.
The error message here is quite misleading. Maybe this will be changed/fixed in future releases of the chef client.


#3

I tried changing C:/chef/cache/cookbooks/chef-client/recipes/task.rb

and chef_client_scheduled_task 'Chef Client' to chef_client_scheduled_task 'ChefC'

everytime I run the recipe this reverts to chef_client_scheduled_task 'Chef Client'. Anything I am missing? I have 13.6.4. Do you recommend downgrading to some 12.x version?


#4

The chef-client cookbook is an official one from the supermarket. You can not just change it and hope that everything works. The thing is that you stated that you have chef_client_scheduled_task 'Chef Client' in one of your recipes. In which recipe is that? Do you want to use this scheduled task from the chef-client cookbook or anything else?


#5

As you noticed in the logs there is task recipe which has this code. There is also a resource in the resources directory scheduled_task.rb

provides :chef_client_scheduled_task

property :user, String, required: true
property :password, String
property :frequency, String, default: 'minute'
property :frequency_modifier, Integer, default: 30
property :start_time, String

action :add do
  create_chef_directories

 # Build command line to pass to cmd.exe
 client_cmd = new_resource.chef_binary_path
 client_cmd << " -L #{::File.join(new_resource.log_directory, 'client.log')}"
 client_cmd << " -c #{::File.join(new_resource.config_directory, 'client.rb')}"
 client_cmd << " -s #{new_resource.splay}"

 # Add custom options
 client_cmd << " #{new_resource.daemon_options.join(' ')}" if 
new_resource.daemon_options.any?

start_time = new_resource.frequency == 'minute' ? (Time.now + 60 * 
new_resource.frequency_modifier).strftime('%H:%M') : nil
windows_task 'chef-client' do
run_level :highest
command "cmd /c \"#{client_cmd} ^> NUL 2^>^&1\""

user               new_resource.user
password           new_resource.password
frequency          new_resource.frequency.to_sym
frequency_modifier new_resource.frequency_modifier
start_time         new_resource.start_time || start_time
 end
end

#6

That is an excerpt from the chef-client cookbook and none of your recipes or cookbooks. If you are trying to use the chef-client cookbook then you need to stop trying to modify this cookbook in any way.
Using the cookbook is quite well documented in the README. For example, to use the task recipe of the chef-client cookbook you need to add chef-client::task to the run list for your node and optionally you can tweak it by setting the attributes mentioned in the README.
The preferred was is usually not to just add the cookbook to the run_list but to write a wrapper cookbook: https://blog.chef.io/2017/02/14/writing-wrapper-cookbooks/.
A good way to start learning all of this is https://learn.chef.io


#7

If I’m reading this conversation right, the error “can’t mosify frozen string” is directly related to the lack of resource cloning in Chef 13.

That’s awesome to know. One of my cookbooks has had that error for a few weeks. Any chance that we could improve that message, so that people like myself wouldn’t have to search too hard?