Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

powershell_exec used in value of Chef recipe property does not populate on intitial run only. #9538

Closed
msaxbury opened this issue Mar 25, 2020 · 1 comment

Comments

@msaxbury
Copy link

@msaxbury msaxbury commented Mar 25, 2020

Describe the problem

I am trying to populate a property inside a template resource variables section, within a recipe that runs second in order. Upon initial chef run it never executes. IF I manually run the client again it always works. I am using a modified version of the Chef Sql Server cookbook from the marketplace. The template resource is basically the same as in that cookbook.

template config_file do
    agent_account_pwd: powershell_exec("& #{installRoot}#{secretsDecrypterRoot}\\#{secretsDecrypterEXE} decrypt -k #{installRoot}#{aesKeyFileRoot}\\#{aesKeyFileName} -s #{agent_account_pwd_encrypted}").result
end

passwords_options = {
  AGTSVCPASSWORD: "#{powershell_exec("& #{installRoot}#{secretsDecrypterRoot}\\#{secretsDecrypterEXE} decrypt -k #{installRoot}#{aesKeyFileRoot}\\#{aesKeyFileName} -s #{agent_account_pwd_encrypted}").result}",
  SQLSVCPASSWORD: "#{powershell_exec("& #{installRoot}#{secretsDecrypterRoot}\\#{secretsDecrypterEXE} decrypt -k #{installRoot}#{aesKeyFileRoot}\\#{aesKeyFileName} -s #{sql_account_pwd_encrypted}").result}",
}.map do |option, attribute|

I am also populating another remote_file resource's property in a different recipe(that runs first) and it works every time on the initial run:

remote_file file_name do
    checksum powershell_exec("((Invoke-WebRequest -URI #{artifactoryBaseUri}#{storageApiUri}#{dbaRepo}#{dbcPath}#{dbcPkgName}) | ConvertFrom-Json).checksums.sha256").result
end

I have tried using lazy {} in this way, and it populates the template correctly on the initial run.

template config_file do
    agent_account_pwd: lazy { powershell_exec("& #{installRoot}#{secretsDecrypterRoot}\\#{secretsDecrypterEXE} decrypt -k #{installRoot}#{aesKeyFileRoot}\\#{aesKeyFileName} -s #{agent_account_pwd_encrypted}").result }
end

However, the passwords section does not like it NOT wrapped #{""} for substitution, and throws undefined method when using lazy wrapper. This section is used on the command line for sql installation. Cannot seem to get it populated on the first run. These will populate on the second run, as indicated above.

passwords_options = {
  AGTSVCPASSWORD: lazy { "#{powershell_exec("& #{installRoot}#{secretsDecrypterRoot}\\#{secretsDecrypterEXE} decrypt -k #{installRoot}#{aesKeyFileRoot}\\#{aesKeyFileName} -s #{agent_account_pwd_encrypted}").result}" },
  SQLSVCPASSWORD: lazy { "#{powershell_exec("& #{installRoot}#{secretsDecrypterRoot}\\#{secretsDecrypterEXE} decrypt -k #{installRoot}#{aesKeyFileRoot}\\#{aesKeyFileName} -s #{sql_account_pwd_encrypted}").result}" },
}.map do |option, attribute|

Software Version

Windows Server 2016 enterprise
Chef Infra Client: 15.4.45
Chef Infra Server: 13.1.13

Replication Case

We use the Chef vRA plugin workflow to bootstrap the VM and execute the first Chef initial run.
The cookbook recipe fails.
I RDP to the server, login, and execute the "chef-client -l debug" command from an elevated PS window.
The values are successfully populated and cookbook/recipe complete successfully.

Stacktrace

``` Generated at 2020-03-25 13:49:43 -0400 Mixlib::ShellOut::ShellCommandFailed: windows_package[Microsoft SQL Server 2016 (64-bit)] (cs_sql_server::sql_server line 184) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0, -2068643838, -2147467259, 42, 127, 3010], but received '-2061893613' ---- Begin output of start "" /wait "c:\00.install\sql_installers\sql_server_enterprise_core_2016\setup.exe" /Q /ACTION=Install /AGTSVCPASSWORD="{}" /SQLSVCPASSWORD="{}" /ConfigurationFile=C:\chef\cache/ConfigurationFile.ini /IAcceptSQLServerLicenseTerms=true & exit %%%%ERRORLEVEL%%%% ---- STDOUT: STDERR: ---- End output of start "" /wait "c:\00.install\sql_installers\sql_server_enterprise_core_2016\setup.exe" /Q /ACTION=Install /AGTSVCPASSWORD="{}" /SQLSVCPASSWORD="{}" /ConfigurationFile=C:\chef\cache/ConfigurationFile.ini /IAcceptSQLServerLicenseTerms=true & exit %%%%ERRORLEVEL%%%% ---- Ran start "" /wait "c:\00.install\sql_installers\sql_server_enterprise_core_2016\setup.exe" /Q /ACTION=Install /AGTSVCPASSWORD="{}" /SQLSVCPASSWORD="{}" /ConfigurationFile=C:\chef\cache/ConfigurationFile.ini /IAcceptSQLServerLicenseTerms=true & exit %%%%ERRORLEVEL%%%% returned -2061893613 C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/mixlib-shellout-3.0.7-universal-mingw32/lib/mixlib/shellout.rb:300:in `invalid!' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/mixlib-shellout-3.0.7-universal-mingw32/lib/mixlib/shellout.rb:287:in `error!' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/mixin/shell_out.rb:132:in `shell_out_compacted!' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/mixin/shell_out.rb:65:in `shell_out!' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package/windows/exe.rb:58:in `install_package' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package/windows.rb:155:in `install_package' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package.rb:95:in `block (3 levels) in ' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package.rb:244:in `multipackage_api_adapter' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package.rb:94:in `block (2 levels) in ' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/mixin/why_run.rb:51:in `add_action' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider.rb:232:in `converge_by' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package.rb:93:in `block in ' (eval):2:in `block in action_install' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider.rb:243:in `instance_eval' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider.rb:243:in `compile_and_converge_action' (eval):2:in `action_install' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package/windows.rb:149:in `action_install' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider.rb:187:in `run_action' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource.rb:585:in `run_action' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:74:in `run_action' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:86:in `block in run_action' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:81:in `each' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:81:in `run_action' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:108:in `block in run_all_actions' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:108:in `each' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:108:in `run_all_actions' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:132:in `block in converge' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource_collection/resource_list.rb:96:in `block in execute_each_resource' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource_collection/stepable_iterator.rb:115:in `call_iterator_block' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource_collection/stepable_iterator.rb:86:in `step' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource_collection/stepable_iterator.rb:104:in `iterate' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource_collection/resource_list.rb:94:in `execute_each_resource' C:/opscode/chef/embedded/lib/ruby/2.6.0/forwardable.rb:230:in `execute_each_resource' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:130:in `converge' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/client.rb:704:in `block in converge' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/client.rb:699:in `catch' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/client.rb:699:in `converge' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/client.rb:723:in `converge_and_save' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/client.rb:289:in `run' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/application.rb:320:in `run_with_graceful_exit_option' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/application.rb:296:in `block in run_chef_client' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/local_mode.rb:42:in `with_server_connectivity' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/application.rb:279:in `run_chef_client' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/application/base.rb:330:in `run_application' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/application.rb:69:in `run' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-bin-15.4.45/bin/chef-client:25:in `' C:/opscode/chef/bin/chef-client:176:in `load' C:/opscode/chef/bin/chef-client:176:in `'

Caused by Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0, -2068643838, -2147467259, 42, 127, 3010], but received '-2061893613'
---- Begin output of start "" /wait "c:\00.install\sql_installers\sql_server_enterprise_core_2016\setup.exe" /Q /ACTION=Install /AGTSVCPASSWORD="{}" /SQLSVCPASSWORD="{}" /ConfigurationFile=C:\chef\cache/ConfigurationFile.ini /IAcceptSQLServerLicenseTerms=true & exit %%%%ERRORLEVEL%%%% ----
STDOUT:
STDERR:
---- End output of start "" /wait "c:\00.install\sql_installers\sql_server_enterprise_core_2016\setup.exe" /Q /ACTION=Install /AGTSVCPASSWORD="{}" /SQLSVCPASSWORD="{}" /ConfigurationFile=C:\chef\cache/ConfigurationFile.ini /IAcceptSQLServerLicenseTerms=true & exit %%%%ERRORLEVEL%%%% ----
Ran start "" /wait "c:\00.install\sql_installers\sql_server_enterprise_core_2016\setup.exe" /Q /ACTION=Install /AGTSVCPASSWORD="{}" /SQLSVCPASSWORD="{}" /ConfigurationFile=C:\chef\cache/ConfigurationFile.ini /IAcceptSQLServerLicenseTerms=true & exit %%%%ERRORLEVEL%%%% returned -2061893613
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/mixlib-shellout-3.0.7-universal-mingw32/lib/mixlib/shellout.rb:300:in invalid!' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/mixlib-shellout-3.0.7-universal-mingw32/lib/mixlib/shellout.rb:287:in error!'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/mixin/shell_out.rb:132:in shell_out_compacted!' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/mixin/shell_out.rb:65:in shell_out!'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package/windows/exe.rb:58:in install_package' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package/windows.rb:155:in install_package'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package.rb:95:in block (3 levels) in <class:Package>' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package.rb:244:in multipackage_api_adapter'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package.rb:94:in block (2 levels) in <class:Package>' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/mixin/why_run.rb:51:in add_action'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider.rb:232:in converge_by' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package.rb:93:in block in class:Package'
(eval):2:in block in action_install' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider.rb:243:in instance_eval'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider.rb:243:in compile_and_converge_action' (eval):2:in action_install'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider/package/windows.rb:149:in action_install' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/provider.rb:187:in run_action'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource.rb:585:in run_action' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:74:in run_action'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:86:in block in run_action' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:81:in each'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:81:in run_action' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:108:in block in run_all_actions'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:108:in each' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:108:in run_all_actions'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:132:in block in converge' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource_collection/resource_list.rb:96:in block in execute_each_resource'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource_collection/stepable_iterator.rb:115:in call_iterator_block' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource_collection/stepable_iterator.rb:86:in step'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource_collection/stepable_iterator.rb:104:in iterate' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource_collection/stepable_iterator.rb:55:in each_with_index'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/resource_collection/resource_list.rb:94:in execute_each_resource' C:/opscode/chef/embedded/lib/ruby/2.6.0/forwardable.rb:230:in execute_each_resource'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/runner.rb:130:in converge' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/client.rb:704:in block in converge'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/client.rb:699:in catch' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/client.rb:699:in converge'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/client.rb:723:in converge_and_save' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/client.rb:289:in run'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/application.rb:320:in run_with_graceful_exit_option' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/application.rb:296:in block in run_chef_client'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/local_mode.rb:42:in with_server_connectivity' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/application.rb:279:in run_chef_client'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/application/base.rb:330:in run_application' C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.4.45-universal-mingw32/lib/chef/application.rb:69:in run'
C:/opscode/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-bin-15.4.45/bin/chef-client:25:in <top (required)>' C:/opscode/chef/bin/chef-client:176:in load'
C:/opscode/chef/bin/chef-client:176:in `

'

</details>


## Possible Solution
<!--- If you already have ideas about how to solve the issue, add them here. -->
@lamont-granquist

This comment has been minimized.

Copy link
Contributor

@lamont-granquist lamont-granquist commented Mar 30, 2020

lazy {} isn't ruby and doesn't work outside of passing a value lazily to the property of a chef resource.

to lazy a block of ruby code you need to use ruby_block.

ruby_block "whatever" do
  block do
    passwords_options = {
      AGTSVCPASSWORD: "#{powershell_exec("& #{installRoot}#{secretsDecrypterRoot}\\#{secretsDecrypterEXE} decrypt -k #{installRoot}#{aesKeyFileRoot}\\#{aesKeyFileName} -s #{agent_account_pwd_encrypted}").result}",
      SQLSVCPASSWORD: "#{powershell_exec("& #{installRoot}#{secretsDecrypterRoot}\\#{secretsDecrypterEXE} decrypt -k #{installRoot}#{aesKeyFileRoot}\\#{aesKeyFileName} -s #{sql_account_pwd_encrypted}").result}",
    }.map do |option, attribute|
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.