What are cron jobs?
- Ansible Playbook Run Python Script
- Python For Loop
- Ansible Python Module Examples
- Python Ansible Playbook
- Ansible and Zowe CLI support many platforms including Linux on Z. Zowe CLI can also run on z/OS. In this example, we will use both of them on a control node on a Linux system.
- This is a task runner that serves as a higher-level automation layer to ansible. The script expects an ansible-playbook file as the task manifest. By default, this is a file named 'Taskfile.yaml' in the current working directory. The inspiration for the tool comes from the gnu make command, which operates in similar fashion, i.e.
Ansible Playbook Run Python Script
In many UNIX/LINUX type operating systems, the Cron is one of the very valuable and productive utility. It is used to run commands and scripts at pre-defined time.
Example: The Cobbler External Inventory Script. Example: AWS EC2 External Inventory Script. Can I run Python modules? I have an ansible server that I find works fine, but when I try to run a python script on the deployed servers I get no such file or directory, even though I visually confirm they are there.
These pre-scheduled jobs are called 'cron jobs'.
Some Common Cron Jobs Use Cases
- Deleting Old/Unused Files
- Regular Checking of Disk Space
- Regular Pre-Scheduled Backups
- Operating System Maintenance Jobs
- etc.
How to create a simple cron job? (Without Ansible)
Run the follwoing command at linux shell prompt:
- linux-prompt$ crontab -l # To list the crontab enteries
- linux-prompt$ crontab -e # To edit/add crontab enteries
Following is the syntax of cron job entry in the crontab file:
For Example:
- To run /path/command ten minutes every day, after midnight, following will be the entry:
10 0 * * * /path/command
- To run /path/myscript.sh at 3:30 PM on the fifth day of every month, following will be the entry:
30 15 5 * * /path/myscript.sh
- To run /path/myscript.sh at 6 PM on selected working weekdays (Monday, Tuesday and Wednesday), following will be the entry:
0 18 * * 1-3 /path/myscript.sh
How Ansible is useful to manage cron jobs?
The Ansible cron module is used to manage the crontab entries via play books. Consider, If you have many servers and you want to configure some common cron jobs on all servers, then configuring the same job manually on all servers will be tedious and error-prone task. Ansible can do this job via a simple playbook to configure the said cron jobs on all servers automatically (in one go).
Step by Step Example of using Ansible CRON module
Pre-Requisites:
1- One Ansible Control Node
2- Two Ansible managed hosts (You may use as many as you want)
3- Network access between control node and managed nodes
4- Host names of all three nodes should be registered with DNS server or appropriate entries should be present in the /etc/hosts files (on all three nodes).
5- User SSH keys should have already been generated at control node and shared with managed nodes (see this article to configure SSH Keys: http://tiny.cc/ro75fz
Note: In this article, we have used one user 'fakhar' on all three nodes. Its SSH keys have been generated at control node and shared on both managed hosts.
Step-1: Create an inventory file at control node (i.e. CentOS-Ctrl-Node), containing the host names of both managed servers, as shown in below image:
Step-2: Create an ansible playbook, to schedule cron jobs as shown below:
Step-3 (Optional): Run the 'tree' command at control node, where your playbook is placed (this is just to show you as how the files are placed/organized). See image below:
Step-4: Run the 'crontab -l' and 'ls -l /home/fakhar/temp.txt' on both managed hosts to confirm the status before the execution of playbook. See image below:
Step-5: Run the playbook using command 'ansible-playbook -i inventory cronPlayBook.yml' at control node. See image below. The playbook has been executed successfully.
Step-6: Check the result on the target managed hosts after the cron job scheduled time. For this purpose, run the 'crontab -l' and 'ls -l /home/fakhar/temp/txt' commands on both managed hosts. See image below, the cron job entries have been created and executed at specified time. The cron jobs have collected the system information and placed it in the target file (i.e. /home/fakhar/temp.txt) successfully.
Thank you.
Note
This module is part of ansible-core
and included in all Ansibleinstallations. In most cases, you can use the short module namescript even without specifying the collections:
keyword.However, we recommend you use the FQCN for easy linking to the moduledocumentation and to avoid conflicting with other collections that may havethe same module name.
The
script
module takes the script name followed by a list of space-delimited arguments.Either a free form command or
cmd
parameter is required, see the examples.The local script at path will be transferred to the remote node and then executed.
The given script will be processed through the shell environment on the remote node.
This module does not require python on the remote system, much like the ansible.builtin.raw module.
This module is also supported for Windows targets.
Note
This module has a corresponding action plugin.
Parameter | Choices/Defaults | Comments |
---|---|---|
chdir string | Change into this directory on the remote node before running the script. | |
cmd string | Path to the local script to run followed by optional arguments. | |
creates string | A filename on the remote node, when it already exists, this step will not be run. | |
decrypt boolean |
| This option controls the autodecryption of source files using vault. |
executable string | Name or path of a executable to invoke the script with. | |
free_form string | Path to the local script file followed by optional arguments. | |
removes string | A filename on the remote node, when it does not exist, this step will not be run. |
Note
Python For Loop
It is usually preferable to write Ansible modules rather than pushing scripts. Convert your script to an Ansible module for bonus points!
The
ssh
connection plugin will force pseudo-tty allocation via-tt
when scripts are executed. Pseudo-ttys do not have a stderr channel and all stderr is sent to stdout. If you depend on separated stdout and stderr result keys, please switch to a copy+command set of tasks instead of using script.If the path to the local script contains spaces, it needs to be quoted.
This module is also supported for Windows targets.
Does not support
check_mode
.
See also
Ansible Python Module Examples
The official documentation on the ansible.builtin.shell module.
The official documentation on the ansible.windows.win_shell module.
Authors
Python Ansible Playbook
Ansible Core Team
Michael DeHaan