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

Idea of managing errors while creating the objects in controller #862

Merged
merged 14 commits into from
Aug 22, 2024
30 changes: 30 additions & 0 deletions roles/job_templates/tasks/async.yml
Tompage1994 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
- 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: "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']
}
]
}}"
...
15 changes: 8 additions & 7 deletions roles/job_templates/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
djdanielsson marked this conversation as resolved.
Show resolved Hide resolved
loop: "{{ __job_templates_job_async.results }}"
loop_control:
loop_var: __job_templates_job_async_result_item
Expand All @@ -100,4 +95,10 @@
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 templates error artifact
when: __templates_error_list is defined
ansible.builtin.set_stats:
data:
templates_errors: "{{ __templates_error_list }}"
...
23 changes: 23 additions & 0 deletions tests/configs/templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Name> --user admin --format yaml
# If you need to add Survey, Survey spec would look like given below:
Expand Down
16 changes: 16 additions & 0 deletions tests/configure_controller.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@

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 templates_errors is defined and has items
ansible.builtin.assert:
that:
- templates_errors is defined
- templates_errors | length > 0
fail_msg: "No errors found, validate test example"
success_msg: "Errors found, proceeding"

- name: Add Controller Settings Individually
ansible.builtin.include_role:
name: settings
Expand Down
Loading