--------
- Added new method to call the webhook via handler
- Added inventory_hostname to webhook calls to see who is doing what
- Updated messages for better readability
- Added comment markers for clarity
This commit is contained in:
Chris Hammer 2023-06-21 22:28:38 -04:00
parent bc812e3bc9
commit e726576548
4 changed files with 70 additions and 29 deletions

View File

@ -2,3 +2,4 @@ skip_list:
- yaml[colons] - yaml[colons]
- yaml[empty-lines] - yaml[empty-lines]
- yaml[line-length] - yaml[line-length]
- no-changed-when

4
handlers/main.yml Normal file
View File

@ -0,0 +1,4 @@
- name: Bob
ansible.builtin.debug:
msg: "{{ r_check_commands }}"
when: r_check_commands is defined

View File

@ -3,12 +3,16 @@
rcount: "{{ 1 if rcount is undefined else rcount | int + 1 }}" rcount: "{{ 1 if rcount is undefined else rcount | int + 1 }}"
########################
# FAILURE BLOCK: START #
########################
- name: Abort run and notify if retries exceed defined limits - name: Abort run and notify if retries exceed defined limits
when: rcount|int >= fail_count when: rcount|int >= fail_count
block: block:
- name: Show rcount and num_retries - name: Show rcount and num_retries
ansible.builtin.debug: ansible.builtin.debug:
msg: "Failed on retry {{ rcount }} with a max of {{ num_retries }}" msg: "{{ inventory_hostname }}: Failed on retry {{ rcount }} \
with a max of {{ num_retries }}"
- name: Log failure to API - name: Log failure to API
ansible.builtin.uri: ansible.builtin.uri:
@ -21,20 +25,28 @@
validate_certs : false validate_certs : false
return_content : false return_content : false
body_format : json body_format : json
body: body : |
Cloudy: "With a chance of meatballs." "{{ inventory_hostname }}": "Job FAIL."
- name: End condition met. Aborting... - name: End condition met.
ansible.builtin.fail: ansible.builtin.fail:
msg: "Bye" msg: "Bye"
######################
# FAILURE BLOCK: END #
######################
###########################
# SUCCESSFUL BLOCK: START #
###########################
- name: We were successful - name: We were successful
when: rcount|int == num_retries when: rcount|int == num_retries
block: block:
- name: Show rcount and num_retries - name: Show rcount and num_retries
ansible.builtin.debug: ansible.builtin.debug:
msg: "Success on retry {{ rcount }} with a max of {{ num_retries }}" msg: "{{ inventory_hostname }}: Success on retry {{ rcount }} with \
a max of {{ num_retries }}"
- name: Log success to API - name: Log success to API
ansible.builtin.uri: ansible.builtin.uri:
@ -47,22 +59,32 @@
validate_certs : false validate_certs : false
return_content : false return_content : false
body_format : json body_format : json
body: body : |
Somewhere: "Over the rainbow." "{{ inventory_hostname }}": "Job success."
- name: End condition met. Exiting gracefully... - name: Flush handlers.
ansible.builtin.meta: flush_handlers
- name: End condition met.
ansible.builtin.meta: end_play ansible.builtin.meta: end_play
#########################
# SUCCESSFUL BLOCK: END #
#########################
########################
# GENERAL TASKS: START #
########################
- name: Run a series of test commands - name: Run a series of test commands
ansible.builtin.command: "{{ item }}" ansible.builtin.command: "{{ item }}"
changed_when : false
loop : "{{ check_commands }}" loop : "{{ check_commands }}"
register : r_check_commands register : r_check_commands
notify : Bob
- name: Current status - name: Current status
ansible.builtin.debug: ansible.builtin.debug:
msg: "Currently on run {{ rcount }} of {{ num_retries }}." msg: "{{ inventory_hostname }}: Finished run {{ rcount }} of {{ num_retries }}."
run_once: true run_once: true
- name: Log status update to API - name: Log status update to API
@ -76,8 +98,8 @@
validate_certs : false validate_certs : false
return_content : false return_content : false
body_format : json body_format : json
body: body: |
Itsa: "me! Mario! - Finished run {{ rcount }} of {{ num_retries }}." "{{ inventory_hostname }}": "Finished run {{ rcount }} of {{ num_retries }}."
- name: Pause for effect - name: Pause for effect
ansible.builtin.pause: ansible.builtin.pause:
@ -86,3 +108,7 @@
- name: Include ourself for recursion - name: Include ourself for recursion
ansible.builtin.include_tasks: tasks/long_running_tasks.yml ansible.builtin.include_tasks: tasks/long_running_tasks.yml
when: rcount|int <= num_retries when: rcount|int <= num_retries
########################
# GENERAL TASKS: END #
########################

View File

@ -26,5 +26,15 @@
handlers: handlers:
- name: Bob - name: Bob
ansible.builtin.debug: ansible.builtin.uri:
msg: "We've reached equilibrium" url : "{{ webhook }}"
headers:
Authorization : "Token {{ webhook_key }}"
Content-type : 'application/json'
method : POST
status_code : 200
validate_certs : false
return_content : false
body_format : json
body : |
"{{ inventory_hostname }}": "We've reached equilibrium."