【问题标题】:How to use the python interpreter in chef如何在 Chef 中使用 python 解释器
【发布时间】:2012-04-02 23:29:47
【问题描述】:

我正在尝试在 chef 中使用 python 解释器。以下是我的幼稚尝试,但没有奏效。在 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

【问题讨论】:

    标签: python chef-infra


    【解决方案1】:

    内容

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

    逐字传递给解释器,也就是说包括前导缩进。因为缩进是 Python 语法的一部分,所以你的脚本(在 &lt;&lt;-EOH/EOH 之间)不是有效的 Python。

    这种情况下的解决方案是删除&lt;&lt;-EOH/EOH块内的缩进。

    【讨论】:

      猜你喜欢
      • 2019-12-08
      • 1970-01-01
      • 2014-03-15
      • 2019-01-14
      • 2018-07-29
      • 2012-05-01
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多