2015-04-02 131 views
0

我對廚師和Powershell很新穎,所以我希望這是有道理的。我需要添加服務器(Chef節點)到一個域,並且Powershell似乎是要走的路。我找到了一個我已經修改了一下的函數,這樣我就可以用一種冪等的方式來做這件事。廚師PowerShell功能

我的問題是我不知道如何把這個在廚師食譜。

我看了看周圍,但沒有運氣,我發現大多數廚師的例子都非常簡單(安裝IIS或tomcat類型)食譜。我包括「配方」我想在這裏創造:

# 
# set this up for reboot should we join domain successfully 
# 
windows_reboot 5 do 
    reason 'Reboot after joining AD' 
    action :nothing 
end 

# 
# import mixin powershellout here 
# also, make sure that Powershell cookbook is on active runlist for node 
# 
::Chef::Recipe.send(:include, Chef::Mixin::PowershellOut) 

powershell_script "addToDomain" do 

######################################################################################## 
# put the powershell script in here between the ruby heredoc string thingies 
# ref: http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#here_doc 
# 
script =<<-'EOF' 
    $exitVal=0; 
    function addComputer { param([string]$username, [string]$password, [string]$domain) 
    try { 
     if ((gwmi win32_computersystem).partofdomain -eq $true) { 
      # arguably here, I would check if it is the RIGHT domain... next rev... 
      $oldDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() 
      $domainName = $oldDomain.name 
      if($domain -eq $oldDomain) 
      { 
      $message = \"The system is joined to the domain\"; 
      $exitVal=2; 
      } 
      else 
      { 
      Remove-Computer -UnjoinDomainCredential (New-Object System.Management.Automation.PSCredential ($username, (ConvertTo-SecureString $password -AsPlainText -Force))) -Force -PassThru -verbose 
      $message = \"computer leaved domain\"; 
      $exitVal=4; 
      } 
     } 
     else { 
      add-computer -domain $domain -credential (New-Object System.Management.Automation.PSCredential ($username, (ConvertTo-SecureString $password -AsPlainText -Force))) -passthru -verbose 
      $message = \"computer joined to domain\"; 
      $exitVal=3; 
     } 
    } 
    catch 
    { 
    $message = \"Join Error - \"; 
    $message += $_; 
    $exitVal=1; 
    } 
    write-host $message; 
    exit $exitVal; 
} 
# this next line uses ruby 
addComputer #{node['ad']['user']} #{node['ad']['pwd']} #{node['ad']['domain']} 
EOF 
######################################################################################## 
# 
# 
result = powershell_out(script) 

Chef::Log.debug("powershell exit #{result.exitstatus}") 
Chef::Log.debug("powershell error #{result.stderr}") 
Chef::Log.debug("powershell stdout #{result.stdout}") 

# same as shell_out 
if result.exitstatus == 2 
    Chef::Log.debug("Already part of domain: #{result.stdout}") 
elsif result.exitstatus == 3 or result.exitstatus == 4 
    Chef::Log.debug("Joined domain: #{result.stdout}") 
    # reboot if joining or leaving domain 
    notifies :request, 'windows_reboot[5]', :delayed 
else 
    Chef::Log.error("Domain join fail: #{result.stdout}") 
    # any other actions here? maybe flag the node? 
end 
end 

回答

0

在我看來,你的Powershell的做法是不實現這一目標的最簡單方法。在那裏有烹飪書,可以爲你做這個 - 看看window_ad cookbook,看起來我喜歡它會做你想做的。請注意,您需要使用它的LWRP部分,而不是默認配方。

+0

謝謝,我以前見過那本食譜,但是由於描述說它在服務器上安裝了AD,所以我不認爲這是我需要的。我現在意識到它也可以用來加入一個域。 – lexugax 2015-04-06 19:53:34

+0

不客氣 - 不少食譜似乎將LWRP功能作爲事後考慮 - 不知道爲什麼! – IBam 2015-04-07 12:57:21