diff --git a/changelogs/fragments/228-at_fix_delete_logic.yml b/changelogs/fragments/228-at_fix_delete_logic.yml new file mode 100644 index 0000000000..4c13ad621a --- /dev/null +++ b/changelogs/fragments/228-at_fix_delete_logic.yml @@ -0,0 +1,4 @@ +--- +bugfixes: + - at - use ``atrm`` command instead of ``at -r`` to avoid ``invalid option`` error on ``RHEL6`` and ``CentOS6``. + - at - fixed search logic for job_id to perform correctly on ``*BSD`` and Solaris. diff --git a/plugins/modules/at.py b/plugins/modules/at.py index a35ec4e98e..eb35c5c4ec 100644 --- a/plugins/modules/at.py +++ b/plugins/modules/at.py @@ -86,9 +86,9 @@ def add_job(module, result, at_cmd, count, units, command, script_file): result['changed'] = True -def delete_job(module, result, at_cmd, command, script_file): +def delete_job(module, result, at_cmd, atrm_cmd, command, script_file): for matching_job in get_matching_jobs(module, at_cmd, script_file): - at_command = "%s -r %s" % (at_cmd, matching_job) + at_command = "%s %s" % (atrm_cmd, matching_job) rc, out, err = module.run_command(at_command, check_rc=True) result['changed'] = True if command: @@ -98,13 +98,17 @@ def delete_job(module, result, at_cmd, command, script_file): def get_matching_jobs(module, at_cmd, script_file): matching_jobs = [] - + os_type = platform.system().lower() atq_cmd = module.get_bin_path('atq', True) # Get list of job numbers for the user. atq_command = "%s" % atq_cmd rc, out, err = module.run_command(atq_command, check_rc=True) - current_jobs = out.splitlines() + if os_type in ['sunos', 'openbsd']: + # Skip header in the command-line output + current_jobs = out.splitlines()[1:] + else: + current_jobs = out.splitlines() if len(current_jobs) == 0: return matching_jobs @@ -115,12 +119,20 @@ def get_matching_jobs(module, at_cmd, script_file): # Loop through the jobs. # If the script text is contained in a job add job number to list. for current_job in current_jobs: - split_current_job = current_job.split() - at_opt = '-c' if platform.system() != 'AIX' else '-lv' - at_command = "%s %s %s" % (at_cmd, at_opt, split_current_job[0]) + job_id = get_id_from_jobqueue(os_type, current_job) + at_opt = '-c' if os_type != 'AIX' else '-lv' + + if os_type == 'sunos': + # at -c option is different purpose in Solaris. + # So it needs to read job spool file(/var/spool/cron/atjobs/) directly. + at_cmd = 'cat' + at_dir = '/var/spool/cron/atjobs/' + at_command = '%s %s/%s' % (at_cmd, at_dir, job_id) + else: + at_command = "%s %s %s" % (at_cmd, at_opt, job_id) rc, out, err = module.run_command(at_command, check_rc=True) if script_file_string in out: - matching_jobs.append(split_current_job[0]) + matching_jobs.append(job_id) # Return the list. return matching_jobs @@ -134,6 +146,20 @@ def create_tempfile(command): return script_file +def get_id_from_jobqueue(os_type, current_job): + # Linux: job_id is located at the beginning of the atq output. + # FreeBSD and NetBSD: job_id is located at the end of the atq output, + # OpenBSD and Solaris: job_id is located in middle of the atq output. + split_current_job = current_job.split() + if os_type in ['freebsd', 'netbsd']: + job_id = split_current_job[-1] + elif os_type in ['openbsd', 'sunos']: + job_id = split_current_job[6] + else: + job_id = split_current_job[0] + return job_id + + def main(): module = AnsibleModule( @@ -151,6 +177,7 @@ def main(): ) at_cmd = module.get_bin_path('at', True) + atrm_cmd = module.get_bin_path('atrm', True) command = module.params['command'] script_file = module.params['script_file'] @@ -173,7 +200,7 @@ def main(): # if absent remove existing and return if state == 'absent': - delete_job(module, result, at_cmd, command, script_file) + delete_job(module, result, at_cmd, atrm_cmd, command, script_file) # if unique if existing return unchanged if unique: diff --git a/tests/integration/targets/at/aliases b/tests/integration/targets/at/aliases index 85f744a596..6eae8bd8dd 100644 --- a/tests/integration/targets/at/aliases +++ b/tests/integration/targets/at/aliases @@ -1,3 +1,2 @@ shippable/posix/group1 destructive -disabled # fixme package diff --git a/tests/integration/targets/at/tasks/main.yml b/tests/integration/targets/at/tasks/main.yml index cd09e11850..4fce83f183 100644 --- a/tests/integration/targets/at/tasks/main.yml +++ b/tests/integration/targets/at/tasks/main.yml @@ -28,7 +28,7 @@ ## at ## -- name: define distros to attempt installing at on +- name: define distros to attempt installing at on set_fact: package_distros: - RedHat @@ -51,8 +51,8 @@ count: 20 units: minutes register: at_test0 -- debug: var=at_test0 -- name: validate results + +- name: validate results for schedule creation assert: that: - 'at_test0.changed is defined' @@ -60,3 +60,15 @@ - 'at_test0.script_file is defined' - 'at_test0.state is defined' - 'at_test0.units is defined' + +- name: remove first example schedule + at: + command: "ls -d / > /dev/null" + state: absent + register: at_test1 + +- name: validate results for schedule deletion + assert: + that: + - 'at_test1.changed' + - 'at_test1.state == "absent"'