- From:
- To: chef <
>
- Subject: [chef] SOLVED: chef powershell cookbook won't update the hostname
- Date: Tue, 1 Nov 2011 14:15:24 -0700
solved .. more or less. if you're interested in the outcome:
http://help.opscode.com/discussions/problems/853-powershell-cookbook-wont-update-the-hostname
i still have open questions, but moving forward now.
kallen
On Tue, 18 Oct 2011,
wrote:
>
hello. this still isn't working for me: updating Windows hostname using
>
powershell via chef isn't working. can anyone lend a hand?
>
>
debug output of the initial chef-client run on a newly launched
>
windows ec2 instance can be seen here:
>
http://groknaut.net/chef-windows-rehostname.txt
>
>
in that output, i notice how it's invoked and that it ran successfully:
>
[Tue, 18 Oct 2011 21:22:54 -0700] INFO: powershell[update-hostname]
>
sh(c:/windows/system32/WindowsPowershell/v1.0/powers
>
hell.exe -ExecutionPolicy RemoteSigned -Command "& {
>
C:\DOCUME~1\ops\LOCALS~1\Temp\1\chef-script20111018-2940-1y3pd5y-0.ps1 }")
>
[Tue, 18 Oct 2011 21:22:55 -0700] INFO: powershell[update-hostname] ran
>
successfully
>
>
>
if it would help reveal what's going on, how could i run that by hand?
>
this file is no longer present:
>
C:\DOCUME~1\ops\LOCALS~1\Temp\1\chef-script20111018-2940-1y3pd5y-0.ps1
>
i wonder if i can tell chef-client to leave that temp file in place so
>
i can try to invoke it. if i had that file, how do i run it by hand? some
>
kind of ruby/chef-client/shell_out handwave handwave..?
>
>
>
>
details:
>
>
using the powershell cookbook, i'm trying to cause a newly launched
>
ec2 instance to update its hostname based on userdata, but it's not
>
working. (i *did* use ec2configservice to allow hostname updates.)
>
>
when i run the powershell script within powershell it works. i reboot
>
the host, and indeed it comes back up with the new name. but when i try
>
to run this powershell script via chef, the instance reboots, but it
>
doesn't come back with the new name.
>
>
below i show my recipe and the powershell script itself. any advice?
>
>
>
notice in the recipe i've tried to call the powershell script using
>
2 different methods (one is commented out in the recipe). neither
>
are working. are both invocations valid?
>
>
i also tried removing parts of the script that would write to STDOUT,
>
in case that was making chef unhappy. didn't seem to help.
>
>
>
>
node info:
>
###################################################################
>
[cheftain01]$ knife node show ip-DEADBEEF.ec2.internal
>
Node Name: ip-DEADBEEF.ec2.internal
>
Environment: _default
>
FQDN: ip-DEADBEEF.ec2.internal
>
IP: 50.17.113.175
>
Run List: role[winpower]
>
Roles: winpower
>
Recipes: windows::default, windows::reboot_handler,
>
powershell::default, powershell::rehostname
>
Platform: windows 5.2.3790
>
>
>
recipe:
>
###################################################################
>
# Cookbook Name:: powershell
>
# Recipe:: rehostname
>
#
>
>
windows_reboot 300 do
>
reason 'Because chef said so'
>
action :nothing
>
end
>
windows_reboot 30 do
>
action :cancel
>
end
>
>
powershell "update-hostname" do
>
powershell_script = <<'POWERSHELL_SCRIPT'
>
$webclient = new-object system.net.webclient
>
$awsurl = "http://169.254.169.254/latest/user-data"
>
$userdata ="c:\Local\Chef\userdata.txt"
>
$webClient.DownloadFile("$awsurl","$userdata")
>
# -r foo -h sous-chef-win -e trex -c trex
>
$namefromuserdata = Get-Content $userdata | %{ $_.Split(' ')[3]; }
>
$computername = Get-Content env:computername
>
Write-Host -ForegroundColor Yellow "userdata: $namefromuserdata...."
>
Write-Host -ForegroundColor Yellow "computername: $computername...."
>
if ( $namefromuserdata -ine $computername )
>
{
>
Write-Host -ForegroundColor Green "renaming $computername to
>
$namefromuserdata."
>
$sysInfo = Get-WmiObject -Class Win32_ComputerSystem
>
$result = $sysInfo.Rename($namefromuserdata)
>
switch($result.ReturnValue)
>
{
>
0 { Write-Host -ForegroundColor Green "Success: renamed
>
$computername to $namefromuserdata." }
>
5 { Write-Host -ForegroundColor Red "This cmdlet must be execute
>
with administrative privileges" }
>
default { Write-Host -ForegroundColor Red "Error" }
>
>
}
>
>
}
>
else { Write-Host -ForegroundColor Green "NOT renaming $computername to
>
$namefromuserdata." }
>
POWERSHELL_SCRIPT
>
notifies :request, 'windows_reboot[300]'
>
#notifies :create, "ruby_block[remove_rehostname]", :immediately
>
creates "C:\\Local\\Chef\\hostname-updated.txt"
>
end
>
>
=begin
>
powershell "update-hostname" do
>
command "C:\\Local\\bin\\update-hostname.ps1"
>
action :run
>
notifies :request, 'windows_reboot[30]'
>
end
>
=end
>
>
# not working yet:
>
ruby_block "remove_rehostname" do
>
block do
>
Chef::Log.info("Recipe rehostname completed, removing the destructive
>
recipe[powershell::rehostname]")
>
node.run_list.remove("recipe[powershell::rehostname]") if
>
node.run_list.include?("recipe[powershell::rehostname]")
>
end
>
action :nothing
>
end
>
>
>
>
update-hostname.ps1:
>
###################################################################
>
$webclient = new-object system.net.webclient
>
$awsurl = "http://169.254.169.254/latest/user-data"
>
$userdata ="c:\Local\Chef\userdata.txt"
>
$webClient.DownloadFile("$awsurl","$userdata")
>
# in our userdata, hostname is the 4th element in array:
>
$namefromuserdata = Get-Content $userdata | %{ $_.Split(' ')[3]; }
>
$computername = Get-Content env:computername
>
Write-Host -ForegroundColor Yellow "userdata: $namefromuserdata...."
>
Write-Host -ForegroundColor Yellow "computername: $computername...."
>
if ( $namefromuserdata -ine $computername )
>
{
>
Write-Host -ForegroundColor Green "renaming $computername to
>
$namefromuserdata."
>
$sysInfo = Get-WmiObject -Class Win32_ComputerSystem
>
$result = $sysInfo.Rename($namefromuserdata)
>
switch($result.ReturnValue)
>
{
>
0 { Write-Host -ForegroundColor Green "Success: renamed
>
$computername to $namefromuserdata." }
>
5 { Write-Host -ForegroundColor Red "This cmdlet must be
>
execute with administrative privileges" }
>
default { Write-Host -ForegroundColor Red "Error" }
>
}
>
>
else { Write-Host -ForegroundColor Green "NOT renaming $computername to
>
$namefromuserdata." }
>
>
>
>
thanks,
>
kallen
- [chef] SOLVED: chef powershell cookbook won't update the hostname, kallen, 11/01/2011
Archive powered by MHonArc 2.6.16.