hiera (Puppet) yaml 中 cronjob 的一行中的几个命令

Several commands in one line for cronjob in hiera (Puppet) yaml

我有 hieradata yaml 文件,其中有很多选项和一些 cronjobs 我想再添加一个包含多个 bash 命令的 cronjob,其中包含引号:

hbase_test:
      cron: '*/5 * * * *'
      user: 'root'
      command: '(date && echo "describe \'my_table\'; exit" | hbase shell) >> hbase_test.log 2>&1;'
      flock: true

但这不起作用 - puppet 无法加载此类 yaml。 我也尝试了下一种方式:

command: "(date && echo \"describe 'my_table'; exit\" | hbase shell) >> hbase_test.log 2>&1;"

puppet没问题,hbase启动成功,但是失败:

describe my_table; exit
NameError: undefined local variable or method `my_table' for #<Object:0xf9f041c>

正确的命令必须是:

describe 'my_table'; exit

我丢了引号!而且我不知道如何以正确的方式逃避他们。 我也试过了

command:>
  (date && echo "describe 'my_table'; exit" | hbase shell) >> hbase_test.log 2>&1;

但是 cron 没有启动。

您的 :> 之间需要一个 space。这就是你想要的:

command: >
  (date && echo "describe 'my_table'; exit" | hbase shell) >> hbase_test.log 2>&1;

您可以看到 YAML 如何解析为 JSON here

最后我尝试了很多不同的方法:

with spacebar without spacebar "" as outer quotes and ' as inner '' as outer quotes and " as inner and some more But all of them cannot escape quotes in right way - I always get error! So the only one solution I find:

  1. 使用您需要的所有命令创建 bash 文件
  2. 运行 bash 一个命令中的脚本

Hiera 命令:

command: 'sh my_bash_script >> hbase_test.log 2>&1 && rm my_bash_script'

Bash 脚本:

date
echo "describe 'my_table'; exit" | hbase shell

这按预期工作