From 269c66fbdb83429436c3a3717d7b683cc43c15ae Mon Sep 17 00:00:00 2001 From: Baptiste Mille-Mathias Date: Sun, 18 Oct 2020 12:01:12 +0200 Subject: [PATCH 01/12] Add a test for removal of at command --- tests/integration/targets/at/aliases | 1 - tests/integration/targets/at/tasks/main.yml | 53 +++++++++++++++------ 2 files changed, 39 insertions(+), 15 deletions(-) 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..9538ece677 100644 --- a/tests/integration/targets/at/tasks/main.yml +++ b/tests/integration/targets/at/tasks/main.yml @@ -28,16 +28,16 @@ ## at ## -- name: define distros to attempt installing at on +- name: define distros to attempt installing at on set_fact: package_distros: - - RedHat - - CentOS - - ScientificLinux - - Fedora - - Ubuntu - - Debian - - openSUSE Leap + - RedHat + - CentOS + - ScientificLinux + - Fedora + - Ubuntu + - Debian + - openSUSE Leap - name: ensure at is installed package: @@ -54,9 +54,34 @@ - debug: var=at_test0 - name: validate results assert: - that: - - 'at_test0.changed is defined' - - 'at_test0.count is defined' - - 'at_test0.script_file is defined' - - 'at_test0.state is defined' - - 'at_test0.units is defined' + that: + - 'at_test0.changed is defined' + - 'at_test0.count is defined' + - 'at_test0.script_file is defined' + - 'at_test0.state is defined' + - 'at_test0.units is defined' + +- name: add a at useless command + at: + command: /bin/logger 'AT task ran from Ansible' + count: 1 + units: minutes + unique: yes + register: at_add + +- debug var=at_add + +- name: pause, giving the time the at runs once + pause: + minutes: 1 + +- name: add a at useless command + at: + command: /bin/logger 'AT task ran from Ansible' + count: 1 + units: minutes + unique: yes + state: absent + register: at_removal + +- debug var=at_removal From a699771c873926154f1ca0440238b56c06aac6d7 Mon Sep 17 00:00:00 2001 From: Baptiste Mille-Mathias Date: Sat, 24 Oct 2020 20:33:55 +0200 Subject: [PATCH 02/12] add chdir option to define a directory from where to run the at command --- .../102_module_at_add_chdir_option.yml | 2 ++ plugins/modules/at.py | 29 ++++++++++++------- tests/integration/targets/at/tasks/main.yml | 27 +++++++++++++++-- 3 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 changelogs/fragments/102_module_at_add_chdir_option.yml diff --git a/changelogs/fragments/102_module_at_add_chdir_option.yml b/changelogs/fragments/102_module_at_add_chdir_option.yml new file mode 100644 index 0000000000..4e67a9e73c --- /dev/null +++ b/changelogs/fragments/102_module_at_add_chdir_option.yml @@ -0,0 +1,2 @@ +minor_changes: + - module at - add option ``chdir`` to permit to launch the at command from a specific directory (https://github.com/ansible-collections/ansible.posix/pull/102). diff --git a/plugins/modules/at.py b/plugins/modules/at.py index a35ec4e98e..f428bfc6b8 100644 --- a/plugins/modules/at.py +++ b/plugins/modules/at.py @@ -17,6 +17,12 @@ - All jobs are executed in the 'a' queue. version_added: "1.0.0" options: + chdir: + description: + - An optional location from where to run the command at. Useful for intance + when running a playbook using ansible-pull with C(purge) option. + type: path + version_added: 1.1.2 command: description: - A command to be executed in the future. @@ -38,7 +44,7 @@ description: - The state dictates if the command or script file should be evaluated as present(added) or absent(deleted). type: str - choices: [ absent, present ] + choices: [absent, present] default: present unique: description: @@ -78,32 +84,32 @@ from ansible.module_utils.basic import AnsibleModule -def add_job(module, result, at_cmd, count, units, command, script_file): +def add_job(module, result, at_cmd, count, units, command, script_file, chdir=None): at_command = "%s -f %s now + %s %s" % (at_cmd, script_file, count, units) - rc, out, err = module.run_command(at_command, check_rc=True) + rc, out, err = module.run_command(at_command, cwd=chdir, check_rc=True) if command: os.unlink(script_file) result['changed'] = True -def delete_job(module, result, at_cmd, command, script_file): +def delete_job(module, result, at_cmd, command, script_file, chdir=None): for matching_job in get_matching_jobs(module, at_cmd, script_file): at_command = "%s -r %s" % (at_cmd, matching_job) - rc, out, err = module.run_command(at_command, check_rc=True) + rc, out, err = module.run_command(at_command, cwd=chdir, check_rc=True) result['changed'] = True if command: os.unlink(script_file) module.exit_json(**result) -def get_matching_jobs(module, at_cmd, script_file): +def get_matching_jobs(module, at_cmd, script_file, chdir=None): matching_jobs = [] 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) + rc, out, err = module.run_command(atq_command, cwd=chdir, check_rc=True) current_jobs = out.splitlines() if len(current_jobs) == 0: return matching_jobs @@ -118,7 +124,7 @@ def get_matching_jobs(module, at_cmd, script_file): 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]) - rc, out, err = module.run_command(at_command, check_rc=True) + rc, out, err = module.run_command(at_command, cwd=chdir, check_rc=True) if script_file_string in out: matching_jobs.append(split_current_job[0]) @@ -139,6 +145,7 @@ def main(): module = AnsibleModule( argument_spec=dict( command=dict(type='str'), + chdir=dict(type='path'), script_file=dict(type='str'), count=dict(type='int'), units=dict(type='str', choices=['minutes', 'hours', 'days', 'weeks']), @@ -152,6 +159,7 @@ def main(): at_cmd = module.get_bin_path('at', True) + chdir = module.params['chdir'] command = module.params['command'] script_file = module.params['script_file'] count = module.params['count'] @@ -173,7 +181,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, command, script_file, chdir=chdir) # if unique if existing return unchanged if unique: @@ -186,7 +194,8 @@ def main(): result['count'] = count result['units'] = units - add_job(module, result, at_cmd, count, units, command, script_file) + add_job(module, result, at_cmd, count, units, command, script_file, + chdir=chdir) module.exit_json(**result) diff --git a/tests/integration/targets/at/tasks/main.yml b/tests/integration/targets/at/tasks/main.yml index 9538ece677..1cd3f1d92e 100644 --- a/tests/integration/targets/at/tasks/main.yml +++ b/tests/integration/targets/at/tasks/main.yml @@ -69,7 +69,7 @@ unique: yes register: at_add -- debug var=at_add +- debug: var=at_add - name: pause, giving the time the at runs once pause: @@ -80,8 +80,29 @@ command: /bin/logger 'AT task ran from Ansible' count: 1 units: minutes - unique: yes + unique: true state: absent register: at_removal + ignore_errors: true + +- debug: var=at_removal + +- name: create an at command with chdir with valid value + at: + command: /bin/logger 'AT task ran from Ansible with chdir' + count: 1 + units: minutes + chdir: /tmp + register: at_chdir_valid +- debug: var=at_chdir_valid + +- name: create an at command with chdir with invalid value + at: + command: /bin/logger 'AT task ran from Ansible with chdir' + count: 1 + units: minutes + chdir: /invalid + register: at_chdir_invalid + ignore_errors: true -- debug var=at_removal +- debug: var=at_chdir_invalid From 09299f1b9a0a8fdc8cc671aa676155816fc5d8a9 Mon Sep 17 00:00:00 2001 From: Baptiste Mille-Mathias Date: Sun, 30 May 2021 18:18:21 +0200 Subject: [PATCH 03/12] Update version_added Co-authored-by: quidame --- plugins/modules/at.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/at.py b/plugins/modules/at.py index f428bfc6b8..ae987c930d 100644 --- a/plugins/modules/at.py +++ b/plugins/modules/at.py @@ -22,7 +22,7 @@ - An optional location from where to run the command at. Useful for intance when running a playbook using ansible-pull with C(purge) option. type: path - version_added: 1.1.2 + version_added: 1.3.0 command: description: - A command to be executed in the future. From ceaee68c85079c7251d73e3eefb3aa378d8c6aac Mon Sep 17 00:00:00 2001 From: Baptiste Mille-Mathias Date: Sun, 30 May 2021 18:18:47 +0200 Subject: [PATCH 04/12] Update plugins/modules/at.py Co-authored-by: quidame --- plugins/modules/at.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/at.py b/plugins/modules/at.py index ae987c930d..75dfcec106 100644 --- a/plugins/modules/at.py +++ b/plugins/modules/at.py @@ -19,7 +19,7 @@ options: chdir: description: - - An optional location from where to run the command at. Useful for intance + - An optional location from where to run the command C(at). Useful for intance when running a playbook using ansible-pull with C(purge) option. type: path version_added: 1.3.0 From 17f8c023616c9cb52c0cf836a8c91aedcf81e292 Mon Sep 17 00:00:00 2001 From: Baptiste Mille-Mathias Date: Sun, 30 May 2021 18:18:57 +0200 Subject: [PATCH 05/12] Update changelogs/fragments/102_module_at_add_chdir_option.yml Co-authored-by: quidame --- .../102_module_at_add_chdir_option.yml | 2 +- plugins/modules/at.py | 7 +++--- tests/integration/targets/at/tasks/main.yml | 23 ++++++++++++++----- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/changelogs/fragments/102_module_at_add_chdir_option.yml b/changelogs/fragments/102_module_at_add_chdir_option.yml index 4e67a9e73c..e4724ed092 100644 --- a/changelogs/fragments/102_module_at_add_chdir_option.yml +++ b/changelogs/fragments/102_module_at_add_chdir_option.yml @@ -1,2 +1,2 @@ minor_changes: - - module at - add option ``chdir`` to permit to launch the at command from a specific directory (https://github.com/ansible-collections/ansible.posix/pull/102). + - at - add option ``chdir`` to permit to launch the ``at`` command from a specific directory (https://github.com/ansible-collections/ansible.posix/issues/13). diff --git a/plugins/modules/at.py b/plugins/modules/at.py index 75dfcec106..97213c6586 100644 --- a/plugins/modules/at.py +++ b/plugins/modules/at.py @@ -19,8 +19,8 @@ options: chdir: description: - - An optional location from where to run the command C(at). Useful for intance - when running a playbook using ansible-pull with C(purge) option. + - An optional location from where to run the command C(at). + - Useful for intance when running a playbook using ansible-pull with C(purge) option. type: path version_added: 1.3.0 command: @@ -194,8 +194,7 @@ def main(): result['count'] = count result['units'] = units - add_job(module, result, at_cmd, count, units, command, script_file, - chdir=chdir) + add_job(module, result, at_cmd, count, units, command, script_file, chdir=chdir) module.exit_json(**result) diff --git a/tests/integration/targets/at/tasks/main.yml b/tests/integration/targets/at/tasks/main.yml index 1cd3f1d92e..335153226f 100644 --- a/tests/integration/targets/at/tasks/main.yml +++ b/tests/integration/targets/at/tasks/main.yml @@ -61,7 +61,7 @@ - 'at_test0.state is defined' - 'at_test0.units is defined' -- name: add a at useless command +- name: Add a useless command using at at: command: /bin/logger 'AT task ran from Ansible' count: 1 @@ -71,11 +71,11 @@ - debug: var=at_add -- name: pause, giving the time the at runs once +- name: Wait for at to run the previous command pause: minutes: 1 -- name: add a at useless command +- name: Add a useless command at: command: /bin/logger 'AT task ran from Ansible' count: 1 @@ -83,11 +83,16 @@ unique: true state: absent register: at_removal - ignore_errors: true - debug: var=at_removal -- name: create an at command with chdir with valid value +- name: Validate results + assert: + that: + - at_add is changed + - at_removal is changed + +- name: Create an at command with chdir with valid value at: command: /bin/logger 'AT task ran from Ansible with chdir' count: 1 @@ -96,7 +101,7 @@ register: at_chdir_valid - debug: var=at_chdir_valid -- name: create an at command with chdir with invalid value +- name: Create an at command with chdir with invalid value at: command: /bin/logger 'AT task ran from Ansible with chdir' count: 1 @@ -106,3 +111,9 @@ ignore_errors: true - debug: var=at_chdir_invalid + +- name: Validate results + assert: + that: + - at_chdir_valid is changed + - not at_chdir_valid is changed From faf1ea4132bc12c418ce729cb3622aa9cf579019 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Thu, 1 Jul 2021 17:43:16 -0700 Subject: [PATCH 06/12] Fix assertion --- tests/integration/targets/at/tasks/main.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/integration/targets/at/tasks/main.yml b/tests/integration/targets/at/tasks/main.yml index 335153226f..158ba4b546 100644 --- a/tests/integration/targets/at/tasks/main.yml +++ b/tests/integration/targets/at/tasks/main.yml @@ -51,7 +51,9 @@ count: 20 units: minutes register: at_test0 + - debug: var=at_test0 + - name: validate results assert: that: @@ -116,4 +118,4 @@ assert: that: - at_chdir_valid is changed - - not at_chdir_valid is changed + - at_chdir_invalid is changed \ No newline at end of file From b8fcfdd24711c8e262069988493ac6e504ab70ea Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Fri, 2 Jul 2021 08:58:21 -0700 Subject: [PATCH 07/12] Update main.yml --- tests/integration/targets/at/tasks/main.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/integration/targets/at/tasks/main.yml b/tests/integration/targets/at/tasks/main.yml index 158ba4b546..2990609165 100644 --- a/tests/integration/targets/at/tasks/main.yml +++ b/tests/integration/targets/at/tasks/main.yml @@ -88,6 +88,10 @@ - debug: var=at_removal +- name: Wait for at to run the previous command + pause: + minutes: 1 + - name: Validate results assert: that: From a18fce2f3a0055fc119e92554c3eb316673c0a5a Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Fri, 2 Jul 2021 15:11:08 -0700 Subject: [PATCH 08/12] Update main.yml --- tests/integration/targets/at/tasks/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/targets/at/tasks/main.yml b/tests/integration/targets/at/tasks/main.yml index 2990609165..7bae5500eb 100644 --- a/tests/integration/targets/at/tasks/main.yml +++ b/tests/integration/targets/at/tasks/main.yml @@ -97,6 +97,7 @@ that: - at_add is changed - at_removal is changed + when: ansible_distribution not in ('Ubuntu', 'FreeBSD') - name: Create an at command with chdir with valid value at: From 12d3db3414c5ef26a7df9c8547d957244759a4e0 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Fri, 2 Jul 2021 15:54:30 -0700 Subject: [PATCH 09/12] Update main.yml --- tests/integration/targets/at/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/at/tasks/main.yml b/tests/integration/targets/at/tasks/main.yml index 7bae5500eb..c4366c036e 100644 --- a/tests/integration/targets/at/tasks/main.yml +++ b/tests/integration/targets/at/tasks/main.yml @@ -97,7 +97,7 @@ that: - at_add is changed - at_removal is changed - when: ansible_distribution not in ('Ubuntu', 'FreeBSD') + when: ansible_distribution not in ('Ubuntu', 'FreeBSD') or ansible_distribution == 'CentOS' and distributed_major_version > 6 - name: Create an at command with chdir with valid value at: From 8788d4d103e7061e9b5df349cdfb343545ff757c Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Mon, 5 Jul 2021 15:49:49 -0700 Subject: [PATCH 10/12] Updating condition for a test --- tests/integration/targets/at/tasks/main.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/integration/targets/at/tasks/main.yml b/tests/integration/targets/at/tasks/main.yml index c4366c036e..69c3efce57 100644 --- a/tests/integration/targets/at/tasks/main.yml +++ b/tests/integration/targets/at/tasks/main.yml @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . + - set_fact: output_dir_test={{output_dir}}/at - name: make sure our testing sub-directory does not exist @@ -97,7 +98,7 @@ that: - at_add is changed - at_removal is changed - when: ansible_distribution not in ('Ubuntu', 'FreeBSD') or ansible_distribution == 'CentOS' and distributed_major_version > 6 + when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] != "6") or (ansible_facts['distribution'] not in ('Ubuntu','FreeBSD')) - name: Create an at command with chdir with valid value at: From c7d610b66279ab7e9eb018680da3a0c786513999 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 6 Jul 2021 11:53:09 -0700 Subject: [PATCH 11/12] Skipping tests for CentOS 6 --- tests/integration/targets/at/tasks/main.yml | 196 ++++++++++---------- 1 file changed, 100 insertions(+), 96 deletions(-) diff --git a/tests/integration/targets/at/tasks/main.yml b/tests/integration/targets/at/tasks/main.yml index 69c3efce57..436b787a7f 100644 --- a/tests/integration/targets/at/tasks/main.yml +++ b/tests/integration/targets/at/tasks/main.yml @@ -29,99 +29,103 @@ ## at ## -- name: define distros to attempt installing at on - set_fact: - package_distros: - - RedHat - - CentOS - - ScientificLinux - - Fedora - - Ubuntu - - Debian - - openSUSE Leap - -- name: ensure at is installed - package: - name: at - state: present - when: ansible_distribution in package_distros - -- name: run the first example - at: - command: "ls -d / > /dev/null" - count: 20 - units: minutes - register: at_test0 - -- debug: var=at_test0 - -- name: validate results - assert: - that: - - 'at_test0.changed is defined' - - 'at_test0.count is defined' - - 'at_test0.script_file is defined' - - 'at_test0.state is defined' - - 'at_test0.units is defined' - -- name: Add a useless command using at - at: - command: /bin/logger 'AT task ran from Ansible' - count: 1 - units: minutes - unique: yes - register: at_add - -- debug: var=at_add - -- name: Wait for at to run the previous command - pause: - minutes: 1 - -- name: Add a useless command - at: - command: /bin/logger 'AT task ran from Ansible' - count: 1 - units: minutes - unique: true - state: absent - register: at_removal - -- debug: var=at_removal - -- name: Wait for at to run the previous command - pause: - minutes: 1 - -- name: Validate results - assert: - that: - - at_add is changed - - at_removal is changed - when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] != "6") or (ansible_facts['distribution'] not in ('Ubuntu','FreeBSD')) - -- name: Create an at command with chdir with valid value - at: - command: /bin/logger 'AT task ran from Ansible with chdir' - count: 1 - units: minutes - chdir: /tmp - register: at_chdir_valid -- debug: var=at_chdir_valid - -- name: Create an at command with chdir with invalid value - at: - command: /bin/logger 'AT task ran from Ansible with chdir' - count: 1 - units: minutes - chdir: /invalid - register: at_chdir_invalid - ignore_errors: true - -- debug: var=at_chdir_invalid - -- name: Validate results - assert: - that: - - at_chdir_valid is changed - - at_chdir_invalid is changed \ No newline at end of file +- name: Run At tests + block: + - name: define distros to attempt installing at on + set_fact: + package_distros: + - RedHat + - CentOS + - ScientificLinux + - Fedora + - Ubuntu + - Debian + - openSUSE Leap + + - name: ensure at is installed + package: + name: at + state: present + when: ansible_distribution in package_distros + + - name: run the first example + at: + command: "ls -d / > /dev/null" + count: 20 + units: minutes + register: at_test0 + + - debug: var=at_test0 + + - name: validate results + assert: + that: + - 'at_test0.changed is defined' + - 'at_test0.count is defined' + - 'at_test0.script_file is defined' + - 'at_test0.state is defined' + - 'at_test0.units is defined' + + - name: Add a useless command using at + at: + command: /bin/logger 'AT task ran from Ansible' + count: 1 + units: minutes + unique: yes + register: at_add + + - debug: var=at_add + + - name: Wait for at to run the previous command + pause: + minutes: 1 + + - name: Add a useless command + at: + command: /bin/logger 'AT task ran from Ansible' + count: 1 + units: minutes + unique: true + state: absent + register: at_removal + + - debug: var=at_removal + + - name: Wait for at to run the previous command + pause: + minutes: 1 + + - name: Validate results + assert: + that: + - at_add is changed + - at_removal is changed + + - name: Create an at command with chdir with valid value + at: + command: /bin/logger 'AT task ran from Ansible with chdir' + count: 1 + units: minutes + chdir: /tmp + register: at_chdir_valid + - debug: var=at_chdir_valid + + - name: Create an at command with chdir with invalid value + at: + command: /bin/logger 'AT task ran from Ansible with chdir' + count: 1 + units: minutes + chdir: /invalid + register: at_chdir_invalid + ignore_errors: true + + - debug: var=at_chdir_invalid + + - name: Validate results + assert: + that: + - at_chdir_valid is changed + - at_chdir_invalid is changed + when: + - not (ansible_facts['distribution'] in ('Ubuntu','FreeBSD')) + - not (ansible_distribution == "CentOS" and ansible_distribution_version is version('6', '==')) \ No newline at end of file From 99585a9696f390dc48cf64d863dad8ccdc78cb7d Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Wed, 4 Aug 2021 23:46:54 -0700 Subject: [PATCH 12/12] Update at.py --- plugins/modules/at.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/at.py b/plugins/modules/at.py index 97213c6586..9c7b02e109 100644 --- a/plugins/modules/at.py +++ b/plugins/modules/at.py @@ -20,7 +20,7 @@ chdir: description: - An optional location from where to run the command C(at). - - Useful for intance when running a playbook using ansible-pull with C(purge) option. + - Useful for instance when running a playbook using ansible-pull with C(purge) option. type: path version_added: 1.3.0 command: @@ -44,7 +44,7 @@ description: - The state dictates if the command or script file should be evaluated as present(added) or absent(deleted). type: str - choices: [absent, present] + choices: [ absent, present ] default: present unique: description: