ruby - How does Chef recipe get/retrieve parameters from databag? -
i have been trying learn chef , trying test small chef cookbook dcpromo of windows 2008 r2 server.
i don't remember got 2 files originally, able working.
the original dcpromo_unattend.txt.erb file was:
[dcinstall] safemodeadminpassword=<%= @admin_password %> rebootoncompletion=yes replicaornewdomain=domain newdomain=forest newdomaindnsname=<%= @domain_name %> forestlevel=3 domainlevel=3 installdns=yes
and default.rb had part in it:
template dcpromo_file source "dcpromo_unattend.txt.erb" variables({ :admin_password => '', :domain_name => '' }) end
i wasn't quite sure how pass in admin_password , domain_name parameters, hard-coded both in dcpromo_unattend.txt.erb file, and, after tweaking, able make cookbook work.
now, i'd able put admin_password , domain_name values databag, tried adding:
begin dcpromote = chef::databagitem.load(:dcpromote, :main) rescue chef::log.fatal("could not find 'main' item in 'dcpromote' data bag - raising fatal error!!") raise end
and changed original template section to:
template dcpromo_file source "dcpromo_unattend.txt.erb" variables({ :admin_password => dcpromote['admin_password'], :domain_name => dcpromote['domain_name'] }) end
and created databag named "dcpromote", doesn't seem working.
can explain how original template code supposed work, i.e., suppose retrieving admin_password , domain_name parameters from?
also, can tell me wrong changes made read admin_password , domain_name "dcpromote" databag?
thanks, jim
edit: guess i've been staring @ few more hours, and, actually, don't understand how did working.
what mean erb file have has password , domain hard-coded:
[dcinstall] safemodeadminpassword=xxxxxxxxx rebootoncompletion=yes replicaornewdomain=domain newdomain=forest newdomaindnsname=whatever.com forestlevel=4 domainlevel=4 installdns=yes
notice there no reference admin_password or domain_name in file.
so, how part of recipe/default.rb working?
template dcpromo_file source "dcpromo_unattend.txt.erb" variables({ :admin_password => '', :domain_name => '' }) end
can explain part of recipe code doing:
variables({ :admin_password => '', :domain_name => '' })
??
thanks, jim
edit 2:
adding entire default.rb after changes suggested @draco ater:
# # cookbook name:: dcpromote # recipe:: default # # copyright (c) 2015 authors, rights reserved. # class serverhelper extend ::windows::helper class << self def dism @@dism ||= locate_sysnative_cmd("dism.exe") end def powershell @@powershell ||= locate_sysnative_cmd('windowspowershell\v1.0\powershell.exe') end def feature_installed?(feature) cmd = mixlib::shellout.new("#{dism} /online /get-features", {:returns => [0,42,127]}).run_command !!(cmd.stderr.empty? && (cmd.stdout =~ /^feature name : #{feature}.?$\n^state : enabled.?$/i)) end end end windows_reboot 60 action :nothing end # # following snippet from: https://supermarket.chef.io/cookbooks/ad # snippet checks presence of databag named "dcpromote" , presence # of item in databag named "main". if item not present, # snippet logs fatal error. begin dcpromote = chef::databagitem.load('dcpromote', 'main') rescue chef::log.fatal("could not find 'main' item in 'dcpromote' data bag - raising fatal error!!") raise end directory chef::config[:file_cache_path] dcpromo_file = file.join(chef::config[:file_cache_path], 'dcpromo_unattend.txt') #cert_script = file.join(chef::config[:file_cache_path], 'setupca.vbs') # available e.g. http://blogs.technet.com/b/pki/archive/2009/09/18/automated-ca-installs-using-vb-script-on-windows-server-2008-and-2008r2.aspx template dcpromo_file source "dcpromo_unattend.txt.erb" variables( :admin_password => dcpromote['admin_password'], :domain_name => dcpromote['domain_name'] ) end powershell_script "run_dcpromo" code "dcpromo /unattend:#{dcpromo_file}" #notifies :request, 'windows_reboot[60]' not_if { serverhelper.feature_installed? 'directoryservices-domaincontroller' } end windows_feature 'directoryservices-domaincontroller' action :install #notifies :request, 'windows_reboot[60]' end
this cookbook/recipe still not working databag.
to clarify: when run earlier code hard-coded setting of admin_password , domain_name, works.
however, if try code uses databag doesn't work. when run databag:
1) [this strange]: if @ "unattended" txt file during run, looks populated, @ end, password item set nothing, i.e., unattended text file changes during run.
2) in end when powershell run, looks gets error 32.
here's console output:
ps c:\users\administrator> chef-client -o dcpromote_usedatabag starting chef client, version 12.3.0 [2015-06-14t07:24:47-07:00] info: *** chef 12.3.0 *** [2015-06-14t07:24:47-07:00] info: chef-client pid: 260 [2015-06-14t07:25:04-07:00] warn: run list override has been provided. [2015-06-14t07:25:04-07:00] warn: original run list: [] [2015-06-14t07:25:04-07:00] warn: overridden run list: [recipe[dcpromote_usedatabag]] [2015-06-14t07:25:04-07:00] info: run list [recipe[dcpromote_usedatabag]] [2015-06-14t07:25:04-07:00] info: run list expands [dcpromote_usedatabag] [2015-06-14t07:25:04-07:00] info: starting chef run node8 [2015-06-14t07:25:04-07:00] info: running start handlers [2015-06-14t07:25:04-07:00] info: start handlers complete. [2015-06-14t07:25:04-07:00] info: http request returned 404 not found: resolving cookbooks run list: ["dcpromote_usedatabag"] [2015-06-14t07:25:04-07:00] info: loading cookbooks [dcpromote_usedatabag@0.1.1, windows@1.37.0, che [2015-06-14t07:25:04-07:00] info: skipping removal of obsoleted cookbooks cache synchronizing cookbooks: [2015-06-14t07:25:04-07:00] info: storing updated cookbooks/dcpromote_usedatabag/recipes/default.rb [2015-06-14t07:25:04-07:00] info: storing updated cookbooks/dcpromote_usedatabag/templates/default/d erb in cache. [2015-06-14t07:25:04-07:00] info: storing updated cookbooks/dcpromote_usedatabag/berksfile in ca [2015-06-14t07:25:04-07:00] info: storing updated cookbooks/dcpromote_usedatabag/.kitchen.yml in - windows - chef_handler [2015-06-14t07:25:04-07:00] info: storing updated cookbooks/dcpromote_usedatabag/chefignore in c [2015-06-14t07:25:04-07:00] info: storing updated cookbooks/dcpromote_usedatabag/metadata.rb in [2015-06-14t07:25:04-07:00] info: storing updated cookbooks/dcpromote_usedatabag/readme.md in ca - dcpromote_usedatabag compiling cookbooks... [2015-06-14t07:25:04-07:00] info: +++++++++++++++++++++++++++ hi ++++++++++++++++++++++++++++ [2015-06-14t07:25:04-07:00] info: +++++++++++++++++++++++++++ hi ++++++++++++++++++++++++++++ [2015-06-14t07:25:04-07:00] info: +++++++++++++++++++++++++++ in template +++++++++++++++++++++++++ [2015-06-14t07:25:04-07:00] info: +++++++++++++++++++++++++++ in template +++++++++++++++++++++++++ [2015-06-14t07:25:04-07:00] info: ++++ xoutput = [123] converging 5 resources recipe: dcpromote_usedatabag::default * windows_reboot[60] action nothing[2015-06-14t07:25:04-07:00] info: processing windows_reboot[60] romote_usedatabag::default line 28) (skipped due action :nothing) * directory[c:/chef/cache] action create[2015-06-14t07:25:04-07:00] info: processing directory[c:/ reate (dcpromote_usedatabag::default line 47) (up date) * template[c:/chef/cache/dcpromo_unattend.txt] action create[2015-06-14t07:25:04-07:00] info: proc hef/cache/dcpromo_unattend.txt] action create (dcpromote_usedatabag::default line 52) [2015-06-14t07:25:04-07:00] info: template[c:/chef/cache/dcpromo_unattend.txt] created file c:/chef/ nd.txt - create new file c:/chef/cache/dcpromo_unattend.txt[2015-06-14t07:25:04-07:00] info: template[c _unattend.txt] updated file contents c:/chef/cache/dcpromo_unattend.txt - update content in file c:/chef/cache/dcpromo_unattend.txt none 798057 --- c:/chef/cache/dcpromo_unattend.txt 2015-06-14 07:25:04.000000000 -0700 +++ c:/users/admini~1/appdata/local/temp/chef-rendered-template20150614-260-1cvaiw 2015-06-14 0 700 @@ -1 +1,10 @@ +[dcinstall] +safemodeadminpassword=p@ssw0rd$123 +rebootoncompletion=yes +replicaornewdomain=domain +newdomain=forest +newdomaindnsname=whateverisforever123.com +forestlevel=4 +domainlevel=4 +installdns=yes * powershell_script[run_dcpromo] action run[2015-06-14t07:25:04-07:00] info: processing powershell action run (dcpromote_usedatabag::default line 68) ================================================================================ error executing action `run` on resource 'powershell_script[run_dcpromo]' ================================================================================ mixlib::shellout::shellcommandfailed ------------------------------------ expected process exit [0], received '32' ---- begin output of "powershell.exe" -nologo -noninteractive -noprofile -executionpolicy unrest none -file "c:/users/admini~1/appdata/local/temp/chef-script20150614-260-dfo5yi.ps1" ---- stdout: stderr: ---- end output of "powershell.exe" -nologo -noninteractive -noprofile -executionpolicy unrestri ne -file "c:/users/admini~1/appdata/local/temp/chef-script20150614-260-dfo5yi.ps1" ---- ran "powershell.exe" -nologo -noninteractive -noprofile -executionpolicy unrestricted -inputform ers/admini~1/appdata/local/temp/chef-script20150614-260-dfo5yi.ps1" returned 32 resource declaration: --------------------- # in c:/chef/cache/cookbooks/dcpromote_usedatabag/recipes/default.rb 68: powershell_script "run_dcpromo" 69: code "dcpromo /unattend:#{dcpromo_file}" 70: #notifies :request, 'windows_reboot[60]' 71: not_if { serverhelper.feature_installed? 'directoryservices-domaincontroller' } 72: end 73: compiled resource: ------------------ # declared in c:/chef/cache/cookbooks/dcpromote_usedatabag/recipes/default.rb:68:in `from_file' powershell_script("run_dcpromo") action "run" retries 0 retry_delay 2 default_guard_interpreter :powershell_script command "run_dcpromo" backup 5 returns 0 code "dcpromo /unattend:c:/chef/cache/dcpromo_unattend.txt" interpreter "powershell.exe" declared_type :powershell_script cookbook_name "dcpromote_usedatabag" recipe_name "default" not_if { #code block } end [2015-06-14t07:26:22-07:00] info: running queued delayed notifications before re-raising exception running handlers: [2015-06-14t07:26:22-07:00] error: running exception handlers running handlers complete [2015-06-14t07:26:22-07:00] error: exception handlers complete [2015-06-14t07:26:22-07:00] fatal: stacktrace dumped c:/chef/cache/chef-stacktrace.out chef client failed. 1 resources updated in 98.15625 seconds [2015-06-14t07:26:22-07:00] fatal: mixlib::shellout::shellcommandfailed: powershell_script[run_dcpro tabag::default line 68) had error: mixlib::shellout::shellcommandfailed: expected process exit ved '32' ---- begin output of "powershell.exe" -nologo -noninteractive -noprofile -executionpolicy unrestrict -file "c:/users/admini~1/appdata/local/temp/chef-script20150614-260-dfo5yi.ps1" ---- stdout: stderr: ---- end output of "powershell.exe" -nologo -noninteractive -noprofile -executionpolicy unrestricted file "c:/users/admini~1/appdata/local/temp/chef-script20150614-260-dfo5yi.ps1" ---- ran "powershell.exe" -nologo -noninteractive -noprofile -executionpolicy unrestricted -inputformat n admini~1/appdata/local/temp/chef-script20150614-260-dfo5yi.ps1" returned 32 ps c:\users\administrator>
and here's unattended txt file @ end:
[dcinstall] safemodeadminpassword= rebootoncompletion=yes replicaornewdomain=domain newdomain=forest newdomaindnsname=whateverisforever123.com forestlevel=4 domainlevel=4 installdns=yes
why unattended txt file changing twice during run (and why password value disappearing)?
thanks, jim
edit 3:
for record, able working adding additional parameter template file setting netbios name:
[dcinstall] rebootoncompletion=yes replicaornewdomain=domain newdomain=forest safemodeadminpassword=<%= @admin_password %> newdomaindnsname=<%= @domain_name %> forestlevel=4 domainlevel=4 installdns=yes domainnetbiosname=<%= @domain_netbios_name %>
and modified default.rb set parameter:
template dcpromo_file source "dcpromo_unattend.txt.erb" variables( :admin_password => dcpromote['admin_password'], :domain_netbios_name => dcpromote['domain_netbios_name'], :domain_name => dcpromote['domain_name'] )
jim
let's start template file itself.
[dcinstall] safemodeadminpassword=<%= @admin_password %> rebootoncompletion=yes replicaornewdomain=domain newdomain=forest newdomaindnsname=<%= @domain_name %> forestlevel=3 domainlevel=3 installdns=yes
the code inside <% %>
ruby code. things start @
inside <% %>
variables. =
shorthand printing value. template uses 2 variables set values, printing them out.
where variables come from? code in recipe ({
, }
not necessary here):
variables( :admin_password => '', :domain_name => '' )
currently initialized empty strings, if put else there in recipe, changed in template too. not break, if pass variables not used in template, redundant code.
for can put password , domain name there , working (producing right configuration file on target machine)
variables( :admin_password => 'my_pass', :domain_name => 'localhost' )
now want move values data bag. create 'dcpromote' databag 'main' data bag item.
knife data bag create dcpromote main
and edit json file. in end should have that:
{ "id": "main", # <= data bag item name "admin_password": "my_pass", "domain_name": "localhost" }
then in recipe read data bag variable (try using strings, not symbols, data bag , item name):
begin dcpromote = chef::databagitem.load( 'dcpromote', 'main' ) rescue chef::log.fatal("could not find 'main' item in 'dcpromote' data bag - raising fatal error!!") raise end
and use when creating configuration file:
variables( :admin_password => dcpromote['admin_password'], :domain_name => dcpromote['domain_name'] )
Comments
Post a Comment