Monday, December 30, 2013

Using Powershell_out mixin in Chef


Premise:

I have been evaluating Chef 11.8.2 for last couple of weeks for my company to automate and manage the deployment of our enterprise application across a large customer base. Our product runs on Windows platform, hence I started writing test Powershell scripts to run with Chef client.

Environment Setup:

I have been using hosted Chef server for this evaluation. I have setup a VM as Chef workstation and another VM as Chef Node.

Problem:

I have been trying to use Chef powershell_script resource to return a value and based on the returned value, execute/notify other Chef resources. After going through several blogs and articles, I found that Chef powershell_script resource does not return a value even if you have a return statement in the powershell script. 

I found people talking on different blogs about using powershell_out mixin for this purpose. 

Following is a sample cookbook that uses powershell_out mixin to return a value after checking the status of a service. The returned value controls the execution of another resource (powershell_script).

Steps:

1. On workstation, use  command to install powershell cookbook.

2. On workstation, use  command to upload the cookbook to Chef Server

3. Add this cookbook to Chef Node's runlist.

4. Create a new cookbook. Add "::Chef::Recipe.send(:include, Chef::Mixin::PowershellOut)" to import PowershellOut mixin.


Sample Cookbook

Following is a sample cookbook, mentioned above in step 4.



The above snippet checks if MSSQLServer windows service exists. If it is "Running", it returns 0. By default, powershell_out adds CR/LF at the end of the string, hence I use "chop" method to get rid of it.



Finally, I describe Chef powershell_scrip resource to "run" only if the value of returned variable is '0'




Note: You can only return strings that appear as status code, such as 0,1, "Running" etc.

Reference: http://pdalinis.blogspot.com/2013/09/chef-using-powershell-as-conditional.html

No comments:

Post a Comment