bashyamlpuppetquoteshiera

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


I have hieradata yaml file with a lot of options and some cronjobs I want to add one more cronjob that contain several bash commands in it with quotes:

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

But this doesn't work - puppet fails to load such yaml. I also tried next way:

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

Puppet is ok, hbase starts successfully, but fails:

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

The right command must be:

describe 'my_table'; exit

I lost quotes! And I don't know how to escape them in right way. I also tried

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

But cron doesn't start.


Solution

  • Finally I tried a lot of different approaches:

    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. create bash file with all command you need
    2. run bash script in hire in one command

    Hiera command:

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

    Bash script:

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

    This works as expected