How to have a cron script that should call chef-databag to get username and password every time it runs?

161 Views Asked by At

I have a cronjob which executes a python script. python script takes two parameter username and pass.

for example : execute.py vijay hTbY87

Requirement is to take this username and pass from dataBag i have in chef. My instance where i need to run this cronjob is in AWS.

Is there a way to have such a cronjob ?

2

There are 2 best solutions below

2
rodcoelho On

Create a cronrun.sh file that contains the following:

#!/usr/bin/env bash

crontab <<EOF
*/30 * * * * /path/to/execute.py vijay hTbY87 > /path/to/logs/output.log 2>&1
EOF

This would execute every 30 minutes and will pipe the output (or Error message) to a logs directory. To search for another time interval, Google crontab.guru every ___ minutes or hours or days or whatever you want.

Make sure that you type chmod +x cronrush.sh so that the file becomes executable.

0
coderanger On

So assuming you're using the built-in cron resource (you might want to use the cron_d resource instead, there are some subtle differences), it's all just writing the Ruby code you want:

params = data_bag_item('bagname', 'itemname')

cron 'myscript' do
  command "python /path/to/execute.py #{params['user']} #{params['password']}"
  # Other properties here to set the schedule.
  # ...
end