Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CRON module] add support for random minutes/hours. #6896

Closed
kiranos opened this issue Apr 8, 2014 · 10 comments
Closed

[CRON module] add support for random minutes/hours. #6896

kiranos opened this issue Apr 8, 2014 · 10 comments
Labels
feature This issue/PR relates to a feature request.

Comments

@kiranos
Copy link

kiranos commented Apr 8, 2014

Issue Type:

Feature Idea

Ansible Version:

ansible 1.5.4

Environment:

Debian 7

Summary:

Today I use bash to automate simple tasks and would like to go over to ansible

I use this below to get a random minute for my cronjob (so not all mysqldumps begins at the same exact time on my vps servers which share hardware, it would create a pretty big load)

$MINUTE=$[ ( $RANDOM % 59 )  + 1 ]

echo "$MINUTE 23 * * * /usr/local/bin/mysqlbackup.sh > /dev/null" > /tmp/mycron
crontab /tmp/mycron

II would love to be able to have this functionality in the CRON module. and not first create a random number variable in bash and import it as a variable to my playbook. Its a few extra steps.

Steps To Reproduce:

random time is not added to CRON module

Expected Results:

add random generator to the module

Actual Results:

random time is not added to CRON module

@mpdehaan
Copy link
Contributor

mpdehaan commented Apr 9, 2014

The proposal that has been made in the past is something like {{ foo | random_int }} which I think would be great as a filter to add to core.py.

Is this something you would like to work on? Previously there was an implementation attempt but it was way too complicated in terms of syntax, and I'd definitely accept a core.py filter looking something like the above.

@kiranos
Copy link
Author

kiranos commented Apr 9, 2014

sorry I'm not a python coder, so cant help with official implementations. but would love to test proposed solution for this above issue.

@bcoca
Copy link
Member

bcoca commented Apr 9, 2014

filter added in #6917 should give you that

@sgpinkus
Copy link
Contributor

@bcoca Idempotence is an issue here? If you use the random filter, your going to get a new random number every time you run the play.

I think the capability has to be bulit into the cron module. Something like minute=random or random_minute=yes and so forth for hours,day,...

@bcoca
Copy link
Member

bcoca commented Apr 12, 2014

@sam yes, that is why i use random numbers to assign them to a host var,
not in the cron template itself.

The random filter has other uses, I use it to choose a random node in a
cluster on which to execute per cluster tasks and avoid always using the
first/last one.

@sgpinkus
Copy link
Contributor

@bcoca OK. In fact a decent way to handle this is to derive an integer from the cron job's name and get the appropriate modulus. There seems to be lack of inbuilt filters to do this though. Adding an ord built in filter would be helpful.

@mpdehaan mpdehaan added P5 and removed P4 labels Apr 17, 2014
@AnneTheAgile
Copy link

Summary;
1.Cron modules do now support using a random number of minutes or hours, as of 1.6.
2.Additionally, the referenced syntax desired by mpdehaan, " {{ foo | random_int }} ", is present in v.1.6.
3.The Docs are here;
http://docs.ansible.com/playbooks_variables.html#random-number-filter
3b.Docs source code is here;
https://github.com/ansible/ansible/blob/devel/docsite/rst/playbooks_variables.rst#random-number-filter
4.The utilized code commit was a merge of #6917 , written by bcoca , plus a tweak to avoid collisions, #7113 .
5.The issue of idempotence is not addressed directly yet.
5a.Docs for python ord(char);
https://docs.python.org/2/library/functions.html
ord(c): Given a string of length one, return an integer representing the Unicode code point of the character when the argument is a unicode object, or the value of the byte when the argument is an 8-bit string.

@photoninger
Copy link
Contributor

It will be great when number 5 "The issue of idempotence is not addressed directly yet." is done.
Perhaps we need an extra option "ignore_time" to compare only the parameters "name" and "job" with the crontab-entries starting at lines 484ff in library/system/cron ?
Or is this a bad idea?

@mpdehaan
Copy link
Contributor

Hi!

Thanks very much for your interest in Ansible. It sincerely means a lot to us.

On September 26, 2014, due to enormous levels of contribution to the project Ansible decided to reorganize module repos, making it easier
for developers to work on the project and for us to more easily manage new contributions and tickets.

We split modules from the main project off into two repos, http://github.com/ansible/ansible-modules-core and http://github.com/ansible/ansible-modules-extras

If you would still like this ticket attended to, and believe this problem or idea is still present in the latest version of Ansible (1.7.2) or the development branch, we will need your help in having it reopened in one of the two new repos, and instructions are provided below.

We apologize that we are not able to make this transition happen seamlessly, though this is a one-time change and your help is greatly appreciated --
this will greatly improve velocity going forward.

Both sets of modules will ship with Ansible, though they'll receive slightly different ticket handling.

To locate where a module lives between 'core' and 'extras'

Additionally, should you need more help with this, you can ask questions on:

Thank you very much!

@AnneTheAgile
Copy link

@photoninger , per my investigation, the idempotency was actually neatly solved with this July 8 , 2013 code, viewing blame on;
https://github.com/ansible/ansible-modules-core/blame/devel/system/cron.py

# L483, 484 
job = crontab.get_cron_job(minute, hour, day, month, weekday, job, special_time)
old_job = crontab.find_job(name)

Thus this ticket does not need to be ported to Core. I noted as such in ;
ansible/ansible-modules-core#47

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature This issue/PR relates to a feature request.
Projects
None yet
Development

No branches or pull requests

9 participants