{"id":1403,"date":"2024-06-14T08:10:08","date_gmt":"2024-06-14T08:10:08","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1403"},"modified":"2025-05-23T19:12:24","modified_gmt":"2025-05-23T19:12:24","slug":"ansible-add-and-remove-crontab-files","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1403","title":{"rendered":"Ansible: Add and remove crontab files"},"content":{"rendered":"<p>Ansible can be used to remotely configure cron entries or even create a crontab files in managed nodes.<\/p>\n<p>The below example creates a crontab file named &#8220;\/etc\/cron.d\/the-date-time&#8221; in the managed nodes.<\/p>\n<p>First let us verify if the file already exists in the managed node.<\/p>\n<p><strong>[root@centosMYOBvm ~]#<\/strong> cat \/etc\/cron.d\/the-date-time<br \/>\n<em>cat: \/etc\/cron.d\/the-date-time: No such file or directory<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat create_crontab_file.yml<\/p>\n<p>= ======<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Playbook to create a cron file with a job<\/em><br \/>\n<em> \u00a0 \u00a0 hosts: testONE<\/em><br \/>\n<em> \u00a0 \u00a0 become: true<\/em><br \/>\n<em> \u00a0 \u00a0 tasks:<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 &#8211; name: Task to create a crontab file with entry<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ansible.builtin.cron:<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 name: Add a crontab file<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 cron_file: the-date-time<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 minute: &#8220;*\/1&#8221;<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 hour: &#8220;9-16&#8221;<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 weekday: &#8220;mon,tue,wed,thu,fri&#8221;<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 job: &#8220;ls \/var &gt;&gt; \/home\/user1\/the_date_time_cron_job&#8221;<\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 user: <span style=\"color: #ff6600;\">user1<\/span><\/em><br \/>\n<em> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 state: present<\/em><\/p>\n<p>==== ==<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout create_crontab_file.yml<\/p>\n<p>==== ==<\/p>\n<p><em>PLAY [Playbook to create a cron file with a job] *******************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Task to create a crontab file with entry] ********************************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><\/p>\n<p>==== ==<\/p>\n<p>Now let us verify the crontab file that got created<\/p>\n<p><strong>[root@centosMYOBvm ~]#<\/strong> cat \/etc\/cron.d\/the-date-time<br \/>\n<em>#Ansible: Add a crontab file<\/em><br \/>\n<em>*\/1 \u00a0 \u00a0 9-16 \u00a0 \u00a0 * \u00a0 \u00a0 * \u00a0 \u00a0 \u00a0 \u00a0 mon,tue,wed,thu,fri <span style=\"color: #ff6600;\">\u00a0 \u00a0 user1<\/span> \u00a0 \u00a0 ls \/var\u00a0&gt;&gt; \/home\/user1\/the_date_time_cron_job<\/em><\/p>\n<p>==== ==<\/p>\n<p><strong>[root@centosMYOBvm ~]#<\/strong> tail -f \/home\/user1\/the_date_time_cron_job<\/p>\n<p><em>adm<\/em><br \/>\n<em>cache<\/em><br \/>\n<em>crash<\/em><\/p>\n<p>==== ====<\/p>\n<p>In order to <strong>remove<\/strong> a crontab file a task similar to above needs to be written, but with the property <strong>status<\/strong> as <em>absent<\/em>.<\/p>\n<p>name: Add date and time to a file<br \/>\nuser: user1<br \/>\ncron_file: the-date-time<br \/>\nstate: absent<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Ansible can be used to remotely configure cron entries or even create a crontab files in managed nodes. The below example creates a crontab file <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1403\" title=\"Ansible: Add and remove crontab files\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":1792,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,21,3],"tags":[],"class_list":["post-1403","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1403"}],"version-history":[{"count":13,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1403\/revisions"}],"predecessor-version":[{"id":1603,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1403\/revisions\/1603"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/1792"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1403"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}