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

问题描述

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

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 没有启动。

解决方法

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

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

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

,

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

带空格键 没有空格键 "" 作为外部引号和 ' 作为内部 '' 作为外部引号和 " 作为内部 还有一些 但是所有这些都无法以正确的方式转义引号 - 我总是出错! 所以我找到的唯一一个解决方案:

  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

这按预期工作