Chef 13 Upgrade: Rubocop Changes for `lazy` Parameters

As part of an upgrade from Chef 12 to Chef 13, this is one of the posts in which I've been detailing the issues I've encountered, and how I've resolved them .

When running Rubocop from ChefDK 2 against the following code, we receive the error Parenthesize the param lazy { ... } to make sure that the block will be associated with the lazy method call twice:

template 'Chef deploy user\'s authorized_keys' do
  source 'authorized_keys.erb'
  path lazy { "#{node['etc']['passwd']['chef']['dir']}/.ssh/authorized_keys" }
  user 'chef'
  group 'chef'
  mode '0600'
  variables lazy {
    {
      public_keys: node['authorized_keys'] + [node.run_state['public_key/deploy']]
    }
  }
end

This error can be resolved by adding a parentheses around the whole lazy { } blocks:

 template 'Chef deploy user\'s authorized_keys' do
   source 'authorized_keys.erb'
-  path lazy { "#{node['etc']['passwd']['chef']['dir']}/.ssh/authorized_keys" }
+  path(lazy { "#{node['etc']['passwd']['chef']['dir']}/.ssh/authorized_keys" })
   user 'chef'
   group 'chef'
   mode '0600'
-  variables lazy {
+  variables(lazy do
     {
       public_keys: node['authorized_keys'] + [node.run_state['public_key/deploy']]
     }
-  }
+  end)
 end

Note that I've also converted the multi-line block to a do / end block, as that was another complaint of Rubocop.

*****

Written by Jamie Tanna on 06 March 2018, and last updated on 30 April 2018.

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under Apache License 2.0.

Tags

Categories