Search code examples
cronjinja2salt-stack

Saltstack passing character Escaping backslash and doublequotes for cronjobs Variables


I am writing a sls template in saltstack and tried to correctly write a cronjob entry but i am not able to do so cause compiler always cries about syntax problem cause it seems the escaping is not working as i thought. i need the escaping backslashes inside the cron command too cause otherwise cronjob does not use the format charackters correctly.

this is the code in my template

{{instance}} cron-entry-for-backup-daily:
 cron.present:
   - user: root
   - name: "{{ online_basedir }}/online_tools/db-tools.py -b {{ settings.port }} -s {{ settings.online_master }} -x {{ instance + '.domain.name' }} backup -d {{ instance }} -f {{ '/opt/onlinebackupdir/' + instance + '/' + instance + '-odoo_db-' + '`date' + '\"' + '+\%Y_\%m_\%d-\%H_\%M' + '\"' + '`' + '_daily.zip'}}"
   - minute: '*'
   - hour: '22'
   - daymonth: '*'
   - month: '*'
   - dayweek: '1-6'
   - identifier: {{ instance + '_dailybackup' }}
   - comment: 'Installed via Salt'

the cronjob entry should look like this

 * 22 * * 1-6 /opt/online/online_tools/db-tools.py -b 63000 -s xyz123 -x sub.domain.name backup -d dbname -f /opt/onlinebackupdir/dbname/dbname-odoo_db-`date "+\%Y_\%m_\%d-\%H_\%M"`_daily.zip

If i execute this on command line it work if i execute it via cron it works ....

Error message:

i cannot compile my code from the sls file expected , but found '' in "", line 124, column 154: ... ckupdir/dbname/dbname-odoo_db-date"+\%Y_\%m_\%d-\%H_\%M"_daily.zip"

i already tried some ways but i think i am unable to solve it at the moment to much brain dead already :-( maybe someone knows a solution i also checked the jinja docs for escaping aso ... but i am helpless at the moment

tries:

'`date +\%Y_\%m_\%d-\%H_\%M`' --> error
'`date "+\%Y_\%m_\%d-\%H_\%M"`' --> error
'`date \"+\%Y_\%m_\%d-\%H_\%M\"`' --> error
'`date '+'\%Y_\%m_\%d-\%H_\%M`' --> error 
'`date '+\%Y_\%m_\%d-\%H_\%M'`' --> error
'`date \"+"\%Y_\%m_\%d-\%H_\%M"\"'`' --> error 
'`date' + '"' + '+\%Y_\%m_\%d-\%H_\%M' + '"' + '`' --> error 
'`date' + '"' + '+\%Y_\%m_\%d-\%H_\%M' + '"' + '`' --> error 
'`date +%Y_%m_%d-%H%M`' --> compiled and worked in command line but not inside crontab % needs to be escaped 

in the meantime i tried this too

"{{ online_basedir }}/online_tools/db-tools.py -b {{ settings.port }} -s {{ settings.online_master_pw }} -x {{ instance + '.domainname.net' }} backup -d {{ instance }} -f {{ '/opt/onlinebackupdir/' + instance + '/' + instance + '-odoo_db-' }} + {% raw %} `date "+\%Y_\%m_\%d-\%H_\%M"` {% endraw %} + {{'_daily.zip'}}"
"{{ online_basedir }}/online_tools/db-tools.py -b {{ settings.port }} -s {{ settings.online_master_pw }} -x {{ instance + '.domainname.net' }} backup -d {{ instance }} -f {{ '/opt/onlinebackupdir/' + instance + '/' + instance + '-odoo_db-' + {% raw %} `date "+\%Y_\%m_\%d-\%H_\%M"` {% endraw %} + '_daily.zip'}}"

This does not work either


Solution

  • OK i solved the Problem with this solution, maybe this is not the best way but it solves my problem.... If someone reads this i would be interested in why {% raw %} .... {% endraw % } does not work

    SOLUTION:

    "{{ online_basedir }}/online_tools/db-tools.py -b {{ settings.port }} -s {{ settings.online_master_pw }} -x {{ instance + '.domainname.net' }} backup -d {{ instance }} -f {{ '/opt/onlinebackupdir/' + instance + '/'  + instance + '-odoo_db-' + '`date \\\"+\\\\%Y_\\\\%m_\\\\%d-\\\\%H_\\\\%M\\\"`' + '_weekly.zip'}}"
    

    now the line in the crontab looks exactly like i need it:

    .......`date "+\%Y_\%m_\%d-\%H_\%M"`..... .zip
    

    RESULT in my BACKUP Folder: instance-odoo_db-2016_10_19-22_00_daily.zip