diff --git a/roles/job_templates/tasks/async.yml b/roles/job_templates/tasks/async.yml new file mode 100644 index 000000000..6ef4e50e9 --- /dev/null +++ b/roles/job_templates/tasks/async.yml @@ -0,0 +1,36 @@ +--- +- name: Async block + block: + - name: "Managing Controller Job Templates | Wait for finish the Job Templates management" + ansible.builtin.async_status: + jid: "{{ __job_templates_job_async_result_item.ansible_job_id }}" + register: __job_templates_job_async_result + until: __job_templates_job_async_result.finished + retries: "{{ controller_configuration_job_templates_async_retries }}" + delay: "{{ controller_configuration_job_templates_async_delay }}" + + rescue: + - name: Removing variables existance from result_file + ansible.builtin.replace: + path: "{{ __job_templates_job_async_result.results_file }}" + regexp: '\{\{.*?\}\}' + replace: '' + + - name: "Load error details" + ansible.builtin.include_vars: + file: "{{ __job_templates_job_async_result.results_file }}" + name: __error_data + + - name: "Building list of not processed templates" + ansible.builtin.set_fact: + __templates_error_list: "{{ __templates_error_list | default([]) + [ { + 'name': __error_data['invocation']['module_args']['name'], + 'project_name': __error_data['invocation']['module_args']['project'], + 'playbook': __error_data['invocation']['module_args']['playbook'], + 'inventory': __error_data['invocation']['module_args']['inventory'], + 'credentials': __error_data['invocation']['module_args']['credentials'], + 'error': __error_data['msg'] + } + ] + }}" +... diff --git a/roles/job_templates/tasks/main.yml b/roles/job_templates/tasks/main.yml index 03154cd15..7dc8ed990 100644 --- a/roles/job_templates/tasks/main.yml +++ b/roles/job_templates/tasks/main.yml @@ -84,13 +84,8 @@ error_flag: true when: ansible_check_mode and __job_templates_job_async.failed is defined and __job_templates_job_async.failed -- name: "Managing Controller Job Templates | Wait for finish the Job Templates management" - ansible.builtin.async_status: - jid: "{{ __job_templates_job_async_result_item.ansible_job_id }}" - register: __job_templates_job_async_result - until: __job_templates_job_async_result.finished - retries: "{{ controller_configuration_job_templates_async_retries }}" - delay: "{{ controller_configuration_job_templates_async_delay }}" +- name: "Managing Controller Job Templates | include async file" + ansible.builtin.include_tasks: "{{ role_path }}/tasks/async.yml" loop: "{{ __job_templates_job_async.results }}" loop_control: loop_var: __job_templates_job_async_result_item @@ -100,4 +95,11 @@ vars: __operation: "{{ operation_translate[__job_templates_job_asycn_result_item.__controller_template_item.state | default(controller_state) | default('present')] }}" ansible_async_dir: '{{ controller_configuration_async_dir }}' + +- name: Set Job Templates error artifact + when: __templates_error_list is defined + ansible.builtin.set_stats: + data: + job_templates_errors: "{{ __templates_error_list }}" + register: __job_templates_errors_set_stats ... diff --git a/tests/configs/templates.yml b/tests/configs/templates.yml index 20a36a495..fe6c127f8 100644 --- a/tests/configs/templates.yml +++ b/tests/configs/templates.yml @@ -58,6 +58,29 @@ controller_templates: choices: '' new_question: true +controller_templates_invalid: + - name: test-template-1_with_missing_project + description: created by Ansible Playbook + job_type: run + inventory: RHVM-01 + labels: + - Prod + credentials: admin@internal-RHVM-01 + project: NonExistingProject + playbook: helloworld.yml + verbosity: 2 + extra_vars: + target_hosts: infra-ansible-tower-01.example.com + - name: Demo Job Template with missing credentials + project: Test Project + job_type: run + playbook: helloworld.yml + ask_extra_vars: false + concurrent_jobs_enabled: false + credentials: Non exisiting Credential + verbosity: 0 + inventory: localhost + # You can retrieve a job along with its survey spec using tower cli as follows: # tower-cli receive --job_template --user admin --format yaml # If you need to add Survey, Survey spec would look like given below: diff --git a/tests/configure_controller.yml b/tests/configure_controller.yml index 4ef5853b1..9682edea9 100644 --- a/tests/configure_controller.yml +++ b/tests/configure_controller.yml @@ -86,6 +86,24 @@ tasks: + - name: Validate error handling + block: + - name: Add Invalid Job Templates to Controller + ansible.builtin.include_role: + name: job_templates + vars: + controller_templates: "{{ controller_templates_invalid }}" + + - name: Ensure the job_templates_errors is defined and has items + ansible.builtin.assert: + that: + - job_templates_errors is defined + - job_templates_errors | length > 0 + fail_msg: "No errors found, validate test example" + success_msg: "Errors found, proceeding" + vars: + job_templates_errors: __job_templates_errors_set_stats.ansible_stats.data.job_templates_errors + - name: Add Controller Settings Individually ansible.builtin.include_role: name: settings