How can i set node attribute from inside powershell_script resource

Hi All,

i have a boolean attribute. i want to set the attribute to true
conditionally inside a powershell_script.
if the attribute is true i would like to throw a fatal error message and
abort chef run.

i’m stuck on how to set the attribute value from inside the powershell.

in the below case
#{node.default[‘saas_wfa_prod_db_server’][‘fail_chef’]=true} is running
irrespective of the if condition. (may be its evaluated during compile)

below is the code. can some one please help.

–code–
node.default[‘saas_wfa_prod_db_server’][‘fail_chef’] = false

powershell_script “get available offline disks” do
code <<-EOH
$sysstoragelength =4
$drivestoragelength=5

if($sysstoragelength -lt $drivestoragelength)
{
#{node.default[‘saas_wfa_prod_db_server’][‘fail_chef’]=true}
}
EOH
end

if (node[‘saas_wfa_prod_db_server’][‘fail_chef’])
Chef::Application.fatal!(“sufficient number of disks not available in
offline state”, 42)
end


pradeep

You can’t update an attribute from inside a powershell_script that is executing. You could do something like the below (with one example where the PowerShell script evaluates to $true and one where it evals to $false:

node.default[‘test’] = false

ruby_block ‘Terminate Chef Client Run’ do
action :nothing
block do
Chef::Application.fatal! 'The attribute was true!'
end
only_if {node[‘test’] == true}
end

ruby_block ‘Fail PowerShell Test’ do
block do
node.default[‘test’] = true
end
guard_interpreter :powershell_script
only_if "test-path c:/windows2"
notifies :run, ‘ruby_block[Terminate Chef Client Run]’, :immediately
end

ruby_block ‘Pass PowerShell Test’ do
block do
node.default[‘test’] = true
end
guard_interpreter :powershell_script
only_if "test-path c:/windows"
notifies :run, ‘ruby_block[Terminate Chef Client Run]’, :immediately
end

Steven Murawski
Community Software Development Engineer @ Chef
Microsoft MVP - PowerShell
http://stevenmurawski.com [http://stevenmurawski.com/]
On 7/10/2015 9:07:23 AM, Pradeep Kumar pradeep.goldfields@gmail.com wrote:
Hi All,

i have a boolean attribute. i want to set the attribute to true conditionally inside a powershell_script.
if the attribute is true i would like to throw a fatal error message and abort chef run.

i’m stuck on how to set the attribute value from inside the powershell.

in the below case #{node.default[‘saas_wfa_prod_db_server’][‘fail_chef’]=true} is running irrespective of the if condition. (may be its evaluated during compile)

below is the code. can some one please help.

–code–
node.default[‘saas_wfa_prod_db_server’][‘fail_chef’] = false

powershell_script “get available offline disks” do
code <<-EOH
$sysstoragelength =4
$drivestoragelength=5

if($sysstoragelength -lt $drivestoragelength)
{
#{node.default[‘saas_wfa_prod_db_server’][‘fail_chef’]=true}
}
EOH
end

if (node[‘saas_wfa_prod_db_server’][‘fail_chef’])
Chef::Application.fatal!(“sufficient number of disks not available in offline state”, 42)
end

pradeep

Thank you! Steven

it worked for me. Quite intelligent stuff.

On Fri, Jul 10, 2015 at 9:12 PM, Steven Murawski steven.murawski@gmail.com
wrote:

You can’t update an attribute from inside a powershell_script that is
executing. You could do something like the below (with one example where
the PowerShell script evaluates to $true and one where it evals to $false:

node.default[‘test’] = false

ruby_block ‘Terminate Chef Client Run’ do
action :nothing
block do
Chef::Application.fatal! 'The attribute was true!'
end
only_if {node[‘test’] == true}
end

ruby_block ‘Fail PowerShell Test’ do
block do
node.default[‘test’] = true
end
guard_interpreter :powershell_script
only_if "test-path c:/windows2"
notifies :run, ‘ruby_block[Terminate Chef Client Run]’, :immediately
end

ruby_block ‘Pass PowerShell Test’ do
block do
node.default[‘test’] = true
end
guard_interpreter :powershell_script
only_if "test-path c:/windows"
notifies :run, ‘ruby_block[Terminate Chef Client Run]’, :immediately
end

Steven Murawski
Community Software Development Engineer @ Chef
Microsoft MVP - PowerShell
http://stevenmurawski.com

On 7/10/2015 9:07:23 AM, Pradeep Kumar pradeep.goldfields@gmail.com
wrote:
Hi All,

i have a boolean attribute. i want to set the attribute to true
conditionally inside a powershell_script.
if the attribute is true i would like to throw a fatal error message and
abort chef run.

i’m stuck on how to set the attribute value from inside the powershell.

in the below case
#{node.default[‘saas_wfa_prod_db_server’][‘fail_chef’]=true} is running
irrespective of the if condition. (may be its evaluated during compile)

below is the code. can some one please help.

–code–
node.default[‘saas_wfa_prod_db_server’][‘fail_chef’] = false

powershell_script “get available offline disks” do
code <<-EOH
$sysstoragelength =4
$drivestoragelength=5

if($sysstoragelength -lt $drivestoragelength)
{
#{node.default[‘saas_wfa_prod_db_server’][‘fail_chef’]=true}
}
EOH
end

if (node[‘saas_wfa_prod_db_server’][‘fail_chef’])
Chef::Application.fatal!(“sufficient number of disks not available in
offline state”, 42)
end


pradeep


pradeep