Using a variable in a chef recipe


#1

Hi,
I want to pass a parameter at the time of knife winrm and use it in the recipe cookbook.
Is it possible?

For e.g.

knife winrm ‘name:klchefwintest01’ chef-client --winrm-user Administrator --winrm-password ‘xxxxxx’ --attribute cloud.local_ipv4 --variable dbname

I want to use variable dbname to do a db backup or use it in the powershell command.


#2

This is not directly possible but you can pass a JSON attrbiutes file to the chef client which sets node attributes for the chef run. The file has to be something like

{
  "my_cookbook": {
    "my_attribute": "my_value"
  }
}

You can then access this attribute in your recipe like any other with node['my_cookbook']['my_attribute'] when calling chef-client <your options here> --json-attributes /tmp/chef-attributes.json. Looks quite bumpy but you could write a small helper script which writes the JSON and calls chef.


#3

it is not acceptable as an option for knife winrm chef-client

Error: invalid option: --json-attributes
USAGE: knife winrm QUERY COMMAND (options)


#4

The option is one of the chef-client, not knife winrm.


#5

I am assuming the target node is bootstrapped in a chef-server since you want to invoke chef-client on the node. So why don’t you just assign the node to a chef-server environment, update the environment with the desired json attributes by using knife and then invoke the chef-client through winrm