{"id":5979,"date":"2025-01-23T12:53:29","date_gmt":"2025-01-23T12:53:29","guid":{"rendered":"https:\/\/linuxbuz.com\/?p=5979"},"modified":"2025-07-11T04:50:56","modified_gmt":"2025-07-11T04:50:56","slug":"ansible-cron-module-examples","status":"publish","type":"post","link":"https:\/\/linuxbuz.com\/devops\/ansible-cron-module-examples","title":{"rendered":"How to Use Ansible Cron Module + Examples"},"content":{"rendered":"<p style=\"text-align: justify;\">Automating repetitive tasks is one of the essential steps in large-scale environments. The <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/ansible\/builtin\/cron_module.html\" target=\"_blank\" rel=\"noopener\"><strong>Ansible cron module<\/strong><\/a> simplifies this by allowing administrators to manage cron jobs on remote systems. Cron jobs are scheduled tasks that run periodically based on time intervals. Using Ansible, you can easily create, modify, or remove cron jobs on multiple systems without logging into each server.<\/p>\n<p style=\"text-align: justify;\"><em>This guide will explore the <strong>Ansible cron module<\/strong> in-depth, including practical examples.<\/em><\/p>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#001a66;border-radius:3px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#004d99;color:#FFFFFF;border-top-left-radius:1px;border-top-right-radius:1px\">\ud83d\udca1 Recommended Reading<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:1px;border-bottom-right-radius:1px\">\n\ud83d\udc49 <a href=\"https:\/\/linuxbuz.com\/linuxhowto\/how-to-get-free-cloud-vps-server-step-by-step-guide\" target=\"_blank\" rel=\"noopener\">How to Get a Free Cloud Server for Testing<\/a><br \/>\n<\/div><\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-cron-module-examples\/#What_is_the_Ansible_Cron_Module\" >What is the Ansible Cron Module?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-cron-module-examples\/#Basic_Syntax\" >Basic Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-cron-module-examples\/#Example_1_Adding_a_Cron_Job\" >Example 1. Adding a Cron Job<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-cron-module-examples\/#Example_2_Removing_a_Cron_Job\" >Example 2. Removing a Cron Job<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-cron-module-examples\/#3_Updating_an_Existing_Cron_Job\" >3. Updating an Existing Cron Job<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-cron-module-examples\/#Example_4_Adding_Cron_Jobs_with_Environment_Variables\" >Example 4. Adding Cron Jobs with Environment Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-cron-module-examples\/#Example_5_Disabling_a_Cron_Job\" >Example 5. Disabling a Cron Job<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-cron-module-examples\/#Example_6_Adding_a_Cron_Job_for_a_Specific_User\" >Example 6. Adding a Cron Job for a Specific User<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-cron-module-examples\/#Example_7_Managing_Multiple_Cron_Jobs_in_a_Playbook\" >Example 7. Managing Multiple Cron Jobs in a Playbook<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-cron-module-examples\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-cron-module-examples\/#FAQs\" >FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n<h2><span class=\"ez-toc-section\" id=\"What_is_the_Ansible_Cron_Module\"><\/span>What is the Ansible Cron Module?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The cron module in Ansible is used to manage the crontab entries of users. It allows you to:<\/p>\n<ul>\n<li>Add new cron jobs<\/li>\n<li>Remove existing cron jobs<\/li>\n<li>Disable or enable specific jobs<\/li>\n<li>Specify commands, times, and additional options<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">The cron module is a part of Ansible&#8217;s core modules, making it readily available for most installations.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Basic_Syntax\"><\/span>Basic Syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The basic syntax of the <strong>Ansible cron module<\/strong> is as follows:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Manage a cron job\r\n  ansible.builtin.cron:\r\n    name: \"Job Name\"\r\n    minute: \"*\"\r\n    hour: \"*\"\r\n    day: \"*\"\r\n    month: \"*\"\r\n    weekday: \"*\"\r\n    job: \"\/path\/to\/command\"\r\n    state: \"present\"<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Example_1_Adding_a_Cron_Job\"><\/span>Example 1. Adding a Cron Job<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">This example demonstrates how to add a cron job to run a script daily at midnight.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Add a cron job to run a backup script\r\n  hosts: all\r\n  tasks:\r\n    - name: Add a cron job for daily backups\r\n      ansible.builtin.cron:\r\n        name: \"Daily Backup Job\"\r\n        minute: \"0\"\r\n        hour: \"0\"\r\n        job: \"\/usr\/local\/bin\/backup.sh\"\r\n        state: present<\/code><\/pre>\n<p style=\"text-align: justify;\">This playbook adds a <strong>daily<\/strong> cron job on all hosts to run a backup script at midnight.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Example_2_Removing_a_Cron_Job\"><\/span>Example 2. Removing a Cron Job<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To <strong>delete<\/strong> an existing cron job, <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\" target=\"_blank\" rel=\"noopener\"><strong>create a playbook<\/strong><\/a> with the following content:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Remove a specific cron job\r\n  hosts: all\r\n  tasks:\r\n    - name: Remove the daily backup job\r\n      ansible.builtin.cron:\r\n        name: \"Daily Backup Job\"\r\n        state: absent<\/code><\/pre>\n<p style=\"text-align: justify;\">This will remove a specific cron job named <strong>&#8220;Daily Backup Job&#8221;<\/strong> from all hosts.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"3_Updating_an_Existing_Cron_Job\"><\/span>3. Updating an Existing Cron Job<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">You can <strong>update<\/strong> a cron job by specifying the same name and modifying its properties.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Update an existing cron job\r\n  hosts: all\r\n  tasks:\r\n    - name: Change the time for the backup job\r\n      ansible.builtin.cron:\r\n        name: \"Daily Backup Job\"\r\n        minute: \"30\"\r\n        hour: \"1\"\r\n        job: \"\/usr\/local\/bin\/backup.sh\"\r\n        state: present<\/code><\/pre>\n<p>The above example updates the <strong>&#8220;Daily Backup Job&#8221;<\/strong> cron job on all hosts to run at <strong>1:30 AM.<\/strong><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Example_4_Adding_Cron_Jobs_with_Environment_Variables\"><\/span>Example 4. Adding Cron Jobs with Environment Variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">You can <strong>add environment variables<\/strong> to a cron job.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Add a cron job with environment variables\r\n  hosts: all\r\n  tasks:\r\n    - name: Add a job with environment variables\r\n      ansible.builtin.cron:\r\n        name: \"Job with Environment Variables\"\r\n        minute: \"0\"\r\n        hour: \"2\"\r\n        job: \"\/usr\/local\/bin\/report.sh\"\r\n        env:\r\n          PATH: \"\/usr\/local\/bin:\/usr\/bin:\/bin\"\r\n          LANG: \"en_US.UTF-8\"\r\n        state: present<\/code><\/pre>\n<p style=\"text-align: justify;\">This playbook adds a cron job named <strong>&#8220;Job with Environment Variables&#8221;<\/strong> on all hosts to run at <strong>2:00 AM,<\/strong> including custom environment variables like <strong>PATH<\/strong> and <strong>LANG.<\/strong><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Example_5_Disabling_a_Cron_Job\"><\/span>Example 5. Disabling a Cron Job<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Sometimes, you may want to <strong>disable<\/strong> a cron job temporarily.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Disable a specific cron job\r\n  hosts: all\r\n  tasks:\r\n    - name: Disable the daily backup job\r\n      ansible.builtin.cron:\r\n        name: \"Daily Backup Job\"\r\n        state: disabled<\/code><\/pre>\n<p style=\"text-align: justify;\">You can use <strong>state: disabled<\/strong> to <strong>disable<\/strong> the <strong>&#8220;Daily Backup Job&#8221;<\/strong> cron job on all hosts without removing it.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Example_6_Adding_a_Cron_Job_for_a_Specific_User\"><\/span>Example 6. Adding a Cron Job for a Specific User<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can specify a different user for the cron job.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Add a cron job for a specific user\r\n  hosts: all\r\n  tasks:\r\n    - name: Add a cron job for user 'john'\r\n      ansible.builtin.cron:\r\n        name: \"User-Specific Job\"\r\n        minute: \"15\"\r\n        hour: \"3\"\r\n        job: \"\/home\/john\/scripts\/cleanup.sh\"\r\n        user: \"john\"\r\n        state: present<\/code><\/pre>\n<p>This playbook adds a cron job named <strong>&#8220;User-Specific Job&#8221;<\/strong> for the user <strong>&#8220;john&#8221;<\/strong> to run a cleanup script at <strong>3:15<\/strong> AM on all hosts.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Example_7_Managing_Multiple_Cron_Jobs_in_a_Playbook\"><\/span>Example 7. Managing Multiple Cron Jobs in a Playbook<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can manage <strong>multiple<\/strong> cron jobs in a single playbook.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Manage multiple cron jobs\r\n  hosts: all\r\n  tasks:\r\n    - name: Add a daily cleanup job\r\n      ansible.builtin.cron:\r\n        name: \"Daily Cleanup\"\r\n        minute: \"0\"\r\n        hour: \"4\"\r\n        job: \"\/usr\/local\/bin\/cleanup.sh\"\r\n        state: present\r\n\r\n    - name: Add a weekly report generation job\r\n      ansible.builtin.cron:\r\n        name: \"Weekly Report\"\r\n        minute: \"30\"\r\n        hour: \"5\"\r\n        weekday: \"7\"\r\n        job: \"\/usr\/local\/bin\/report.sh\"\r\n        state: present<\/code><\/pre>\n<p>This playbook adds two cron jobs on all hosts: a daily cleanup job at <strong>4:00 AM<\/strong> and a weekly report generation job at <strong>5:30 AM<\/strong> on Sundays.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The Ansible cron module is a powerful tool for managing scheduled tasks across multiple systems. With its flexibility and ease of use, it ensures consistency and reduces manual effort in cron job management.<\/p>\n<p style=\"text-align: justify;\">You can explore the <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-debug-module-examples\" target=\"_blank\" rel=\"noopener\"><strong>debug module<\/strong><\/a> to validate cron job tasks and the <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-when-conditionals\" target=\"_blank\" rel=\"noopener\"><strong>when statement<\/strong><\/a> to add conditional logic for dynamic scheduling.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>1. How do I verify if a cron job was successfully created?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tUse the cron command on the managed node or the debug module in Ansible to check the task output.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>2. What happens if I set overlapping cron jobs with the same name?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tAnsible will overwrite the existing job with the new parameters, as jobs are identified by their name.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>3. How do I create a cron job that runs multiple commands?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tUse ; or &amp;&amp; in the job parameter to include multiple commands in a single cron job.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t\n<script type=\"application\/ld+json\">\n    {\n\t\t\"@context\": \"https:\/\/schema.org\",\n\t\t\"@type\": \"FAQPage\",\n\t\t\"mainEntity\": [\n\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"1. How do I verify if a cron job was successfully created?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Use the cron command on the managed node or the debug module in Ansible to check the task output.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"2. What happens if I set overlapping cron jobs with the same name?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Ansible will overwrite the existing job with the new parameters, as jobs are identified by their name.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"3. How do I create a cron job that runs multiple commands?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Use ; or &amp;&amp; in the job parameter to include multiple commands in a single cron job.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t\t    ]\n}\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>Automating repetitive tasks is one of the essential steps in large-scale environments. The Ansible cron module simplifies this by allowing administrators to manage cron jobs on remote systems. Cron jobs &hellip; <\/p>\n","protected":false},"author":1,"featured_media":5980,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"two_page_speed":[],"footnotes":""},"categories":[828],"tags":[],"class_list":["post-5979","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/5979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/comments?post=5979"}],"version-history":[{"count":5,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/5979\/revisions"}],"predecessor-version":[{"id":6671,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/5979\/revisions\/6671"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media\/5980"}],"wp:attachment":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media?parent=5979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/categories?post=5979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/tags?post=5979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}