How to configure a firewall to always have a cron job?

How to configure Vagrant so that when I provide a machine, its crontab is automatically configured? (tramp is provided according to the chef's files (?))

As an example, I wanted to install the following cron:

5 * * * * curl http://www.google.com 
+6
source share
1 answer

Basic provision of such things can be easily done without Chef / Puppet / Ansible and use a shell instead.

Vagrant docs describe this basic software quite well for their example of how to download Apache from boostrap.sh.

Similarly, you can follow the same steps when editing your Vagrantfile to call bootstrap.sh when provided:

 Vagrant.configure("2") do |config| ... config.vm.provision :shell, path: "bootstrap.sh" ... end 

You can then create the bootstrap.sh file in the same directory as your Vagrantfile, which will contain something like:

 #!/bin/bash # Adds a crontab entry to curl google.com every hour on the 5th minute # Cron expression cron="5 * * * * curl http://www.google.com" # β”‚ β”‚ β”‚ β”‚ β”‚ # β”‚ β”‚ β”‚ β”‚ β”‚ # β”‚ β”‚ β”‚ β”‚ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0) # β”‚ β”‚ β”‚ └────────── month (1 - 12) # β”‚ β”‚ └─────────────── day of month (1 - 31) # β”‚ └──────────────────── hour (0 - 23) # └───────────────────────── min (0 - 59) # Escape all the asterisks so we can grep for it cron_escaped=$(echo "$cron" | sed s/\*/\\\\*/g) # Check if cron job already in crontab crontab -l | grep "${cron_escaped}" if [[ $? -eq 0 ]] ; then echo "Crontab already exists. Exiting..." exit else # Write out current crontab into temp file crontab -l > mycron # Append new cron into cron file echo "$cron" >> mycron # Install new cron file crontab mycron # Remove temp file rm mycron fi 

By default, backup devices for vagrants are run as root, so this will add a cron job to the crontab root user, assuming that it does not exist yet. If you want to add it to the crontab firewall, then you will need to run the security tool with the privileged flag set to false :

 config.vm.provision :shell, path: "bootstrap.sh", privileged: false 
+7
source

Source: https://habr.com/ru/post/984537/


All Articles