I have a task to configure the following Ansible playbook to ignore error code 422 (which should happen when it looks for a volume that is detached from the server).
Furthermore, I currently see that this situation can also display error code 400, so, I may also have to exclude this.
# ---
# # This script will print the received parameters only
- hosts: localhost
connection: local
# # Required parameters
# # for testing locally
vars:
server_id: xxx
backup_properties:
name: xxx
retention_period: x
volume_ids:
- xxx
gather_facts: no
tasks:
# Get moss_url variable
- include_tasks: ../vault_get_moss_environment.yml
# Request the backup from the endpoint
- name: Call MOS Windows Service Endpoint
uri:
url: "{{ moss_url }}/v1/service-instances/{{ server_id }}/backups"
method: POST
return_content: yes
status_code: 202
headers:
Content-Type: "application/json"
Authorization: "Bearer {{ getToken.json.access_token }}"
body_format: json
body: {
"name": "{{ backup_properties.name | default('Backup Before Update') }}",
"volumeIds": "{{ backup_properties.volume_ids | default(omit) }}",
"retentionPeriod": "{{ backup_properties.retention_period | default(14) }}"
}
register: trigger_backup_response
no_log: false
ignore_errors: yes
- fail:
msg: "Error code: {% if trigger_backup_response.msg %}{{ trigger_backup_response.msg }}{% else %}{{ trigger_backup_response }}{% endif %}"
when: trigger_backup_response.failed | default(false) and trigger_backup_response.json.status != "NOT_FOUND" # .failed is defined only when it fails
- name: Delete server's backup schedules if server is not found in Openstack
include_tasks: ../MOSS/call_moss_delete_backup_schedules.yml
vars:
id: "{{ server_id }}"
when: trigger_backup_response.status == 404 and trigger_backup_response.json.status == "NOT_FOUND"
- name: Get the id from the content
debug:
msg: "Started backup job with id: {{ trigger_backup_response.json.id }} for server with id: {{ server_id }}"
when: trigger_backup_response.status == 202
- name: Set backup_job_id as a fact
set_fact:
backup_job_id="{{ trigger_backup_response.json.id }}"
when: trigger_backup_response.status == 202
###### Please do not delete this debug as it is used by MOSS #####
- debug:
msg: "backup_job_id={{ backup_job_id }}"
when: trigger_backup_response.status == 202
I am aware that I probably have to use ignore_erros in some context, but, am not able to locate any way to exclude errors specifically by their code.
So, overall the question is how can I make this playbook ignore specifically errors 422 and 400 without sending any alerts or performing any actions?