Chef 12 dns recipe error

Chef::Log.info("****** DNS reconfig ******")
execute ‘Configure hostname2’ do
command “echo #{node[“opsworks”][“instance”][“hostname”]}.#{node[“opsworks”][“stack”][“name”]}.#{node[“domain_name”]} > /etc/hostname”
ignore_failure true
end

Receiving `block in from_file’ error. What am doing wrong. This is on opsworks chef 12. Appreciate any kind of help.

It would help if you give the full stacktrace.
From what I see, your code looks correct. Is it failing elsewhere ?

For your use-case, please see chef documentation about the file resource https://docs.chef.io/resource_file.html

Hi Joris, thanks for your reply..here is the output, it says recipe compile error.

================================================================================
Recipe Compile Error in /var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb
================================================================================
 
SyntaxError
-----------
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: syntax error, unexpected '[', expecting '('
...rks"]["instance"]["hostname"].["opsworks"]["stack"]["name"]....
...                               ^
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: syntax error, unexpected ']', expecting tSTRING_DEND
...ance"]["hostname"].["opsworks"]["stack"]["name"].["domain_na...
...                               ^
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: syntax error, unexpected ']', expecting tSTRING_DEND
...ostname"].["opsworks"]["stack"]["name"].["domain_name"]} > /...
...                               ^
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: syntax error, unexpected ']', expecting tSTRING_DEND
...].["opsworks"]["stack"]["name"].["domain_name"]} > /etc/host...
...                               ^
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: syntax error, unexpected ']', expecting tSTRING_DEND
...stack"]["name"].["domain_name"]} > /etc/hostname" 
...                               ^
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: unknown regexp options - hta
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: syntax error, unexpected tSTRING_BEG, expecting tSTRING_DEND
...domain_name"]} > /etc/hostname" 
...                               ^
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: unterminated string meets end of file
 
Platform:
---------
x86_64-linux
 
[2018-07-25T21:24:18+00:00] ERROR: Running exception handlers
[2018-07-25T21:24:18+00:00] ERROR: Exception handlers complete
[2018-07-25T21:24:18+00:00] FATAL: Stacktrace dumped to /var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/chef-stacktrace.out
[2018-07-25T21:24:18+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2018-07-25T21:24:18+00:00] ERROR: /var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: syntax error, unexpected '[', expecting '('
...rks"]["instance"]["hostname"].["opsworks"]["stack"]["name"]....
...                               ^
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: syntax error, unexpected ']', expecting tSTRING_DEND
...ance"]["hostname"].["opsworks"]["stack"]["name"].["domain_na...
...                               ^
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: syntax error, unexpected ']', expecting tSTRING_DEND
...ostname"].["opsworks"]["stack"]["name"].["domain_name"]} > /...
...                               ^
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: syntax error, unexpected ']', expecting tSTRING_DEND
...].["opsworks"]["stack"]["name"].["domain_name"]} > /etc/host...
...                               ^
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: syntax error, unexpected ']', expecting tSTRING_DEND
...stack"]["name"].["domain_name"]} > /etc/hostname" 
...                               ^
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: unknown regexp options - hta
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: syntax error, unexpected tSTRING_BEG, expecting tSTRING_DEND
...domain_name"]} > /etc/hostname" 
...                               ^
/var/chef/runs/1383b7e6-95cc-41d5-8177-efa2d2a3b14e/local-mode-cache/cache/cookbooks/corenew/recipes/dns.rb:24: unterminated string meets end of file
[2018-07-25T21:24:18+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

@rajesh.apt could you snippet or gist corenew/recipes/dns.rb - there is a Ruby syntax error on line 24 and we can probably spot it pretty easy.

I have updated the gist. Your help will be appreciated

I don't see a gist linked anywhere but the likely problem is one of quote escaping so try:

  command "echo #{node['opsworks']['instance']['hostname']}.#{node['opsworks']['stack']['name']}.#{node['domain_name']} > /etc/hostname"

Anytime you have a double quoted string then any double quotes inside that string need to be escaped. In this case, Chef style would say "use single quoted node attributes" so that should also sidestep the issue.