Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the python interpreter in chef

I am trying to use the python interpreter in chef. Below is my naive attempt that did not work. What is the proper way to accomplish the below in python?

script "install_something" do
  interpreter "python"
  user "root"
  cwd "/tmp"
  code <<-EOH
  import boto
  f = open('test.txt','r')
  f.write('adfadf')
  f.close()
  EOH
  not_if {File.exists?("/tmp/test.txt")}
end


[Mon, 02 Apr 2012 15:20:35 +0000] ERROR: Chef::Exceptions::ShellCommandFailed: script[install_something] (rtb_server::default line 101) had an error: Chef::Exceptions::ShellCommandFailed: Expected process to exit with [0], but received '1'
---- Begin output of "python"  "/tmp/chef-script20120402-26069-3d6hob-0" ----
STDOUT: 
STDERR: File "/tmp/chef-script20120402-26069-3d6hob-0", line 1
    import boto
    ^
IndentationError: unexpected indent
---- End output of "python"  "/tmp/chef-script20120402-26069-3d6hob-0" ----
Ran "python"  "/tmp/chef-script20120402-26069-3d6hob-0" returned 1
like image 818
Tampa Avatar asked May 23 '26 04:05

Tampa


1 Answers

The contents of

  code <<-EOH
  import boto
  f = open('test.txt','r')
  f.write('adfadf')
  f.close()
  EOH

are passed to the interpreter verbatim, which is to say including the leading indent. Because indentation forms a part of the python syntax, your script (between the <<-EOH/EOH) is not valid python.

The solution in this case is to remove the indentation within the <<-EOH/EOH block.

like image 140
Marcin Avatar answered May 25 '26 16:05

Marcin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!