LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-28-2020, 10:22 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Rep: Reputation: 43
ansible - adding crontab entry


Im having a problem with ansible while adding entry to crontab for user: user1.
Entry is added, however for some reason between execution time and a command, user1 is added.
Please see following result:
Code:
#Ansible: script1.sh
0 1 * * 1 user1 /mnt/sync/script1.sh
Does anyone understand what am I doing wrong?
I tried to kick out line: user: user1 but then execution fails.


This is the code of: tasks/script1.yml
Code:
---

- name: create file and add crontab entry
  file:
    path: "/mnt/sync/script1.sh"
    state: directory
    mode: '0755'

- name: upload script1.sh
  copy:
    src: "script1.sh"
    dest: "/mnt/sync/script1.sh"
    mode: 755
    owner: user1
    group: group1
    backup: yes

- name: cron job to for script1.sh
  cron:
    name: script1
    weekday: "1"
    hour: "1"
    cron_file: "/var/spool/cron/user1"
    user: user1
    job: "/mnt/sync/script1.sh"
 
Old 12-28-2020, 11:03 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Could you solve this without ansible?
 
Old 12-28-2020, 11:36 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
is it a problem at all?
see man crontab:
Quote:
The jobs in cron.d and /etc/crontab are system jobs, which are used usually for more than one user, thus, additionally the username is needed.
 
Old 12-28-2020, 01:27 PM   #4
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
Hi guys, thank you for your replies.

@NevemTeve - it must be done via ansible.
@pan64 - if I keep username (between time and command), cron job execution will fail. Once i manually remove username, it works.

It puzzles me from where ansible takes this user1???

Last edited by czezz; 12-28-2020 at 03:45 PM.
 
Old 12-28-2020, 05:55 PM   #5
marav
LQ Sage
 
Registered: Sep 2018
Location: Gironde
Distribution: Slackware
Posts: 5,356

Rep: Reputation: 4067Reputation: 4067Reputation: 4067Reputation: 4067Reputation: 4067Reputation: 4067Reputation: 4067Reputation: 4067Reputation: 4067Reputation: 4067Reputation: 4067
Hi,

My suggestions are :
you should use double quote only for variable values
Ex/
Code:
dest: /mnt/sync
or 
dest: "{{ my_dest }}"
Why is your path pointing to a script and not a directory (/mnt/sync) ?
This is similar to touch a file, in your playbook this is useless because of the state
Code:
- name: create file and add crontab entry
  file:
    path: "/mnt/sync/script1.sh"
    state: directory
    ...
You'd better use 'template' module (jinja2) compared to 'copy'
Code:
template:
  src: script1.sh.j2
  dest: /mnt/sync/script1.sh
  ...

Last edited by marav; 12-28-2020 at 06:04 PM.
 
Old 12-29-2020, 03:20 AM   #6
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
Thank you Marav for your reply and suggestions but the part of ansible code you mentioned works OK.
The problem is with the ansible code that inserts crontab entry (here):
Code:
- name: cron job to for script1.sh
  cron:
    name: script1
    weekday: "1"
    hour: "1"
    cron_file: "/var/spool/cron/user1"
    user: user1
    job: "/mnt/sync/script1.sh"
Result in crontab:
Code:
#Ansible: script1.sh
0 1 * * 1 user1 /mnt/sync/script1.sh

Last edited by czezz; 12-29-2020 at 03:24 AM.
 
Old 12-29-2020, 03:31 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
you specified user: user1. What will happen if you remove that line?
 
Old 12-29-2020, 03:48 AM   #8
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
I tried that already (see opening post):
Quote:
I tried to kick out line: user: user1 but then execution fails.
It gives error like this:
Code:
fatal: [host.local.net]: FAILED! => {"changed": false, "msg": "To use cron_file=... parameter you must specify user=... as well"}
 
Old 12-29-2020, 03:54 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by czezz View Post
I tried that already (see opening post):
Sorry, that was misleading for me. Never mind.
As I posted there are system jobs and user jobs. Now your playbook creates system jobs (with user info), but you need to create user job. Actually I think you need to omit cron_file too.
 
1 members found this post helpful.
Old 12-29-2020, 06:20 AM   #10
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
That is actually a surprise to me but this way (by removing cron_file: "/var/spool/cron/ecm_admin") it added correct entry.
Thank you!
Code:
#Ansible: script1.sh
0 1 * * 1 /mnt/sync/script1.sh
 
Old 12-29-2020, 09:20 AM   #11
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
https://docs.ansible.com/ansible/lat...on_module.html

Code:
cron_file
string
		
If specified, uses this file instead of an individual user's crontab.
If this is a relative path, it is interpreted with respect to /etc/cron.d.
If it is absolute, it will typically be /etc/crontab.
Many linux distros expect (and some require) the filename portion to consist solely of upper- and lower-case letters, digits, underscores, and hyphens.
To use the cron_file parameter you must specify the user as well.
It expects any named cron file to either to be relative to /etc/cron.d or be /etc/crontab, both of which require a user.
You can see this with their examples

Code:
- name: Modifies the users crontab and doesn't need a username
  cron:
    name: "Auto update apt"
    job: "apt-get update"
    state: present
- name: Creates a cron file as /etc/cron.d/apt-update and needs a username
  cron:
    name: Auto update apt
    user: root
    job: "apt-get update"
    cron_file: apt-update
    state: present

Last edited by Sefyir; 12-29-2020 at 09:34 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Ansible Guide: Manage Files using Ansible LXer Syndicated Linux News 0 04-26-2019 10:32 AM
LXer: Ansible Guide: Create Ansible Playbook for LEMP Stack LXer Syndicated Linux News 0 04-13-2019 05:03 AM
LXer: Ansible Tutorial: Introduction to simple Ansible commands LXer Syndicated Linux News 0 05-21-2018 10:28 AM
LXer: Ansible Tutorial: Intorduction to simple Ansible commands LXer Syndicated Linux News 0 01-14-2018 05:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration