diff --git a/.ansible-lint b/.ansible-lint index 6bf43dc..b6d3809 100644 --- a/.ansible-lint +++ b/.ansible-lint @@ -2,3 +2,4 @@ skip_list: - yaml[colons] - yaml[empty-lines] - yaml[line-length] + - no-changed-when diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..16e1feb --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,4 @@ +- name: Bob + ansible.builtin.debug: + msg: "{{ r_check_commands }}" + when: r_check_commands is defined diff --git a/tasks/long_running_tasks.yml b/tasks/long_running_tasks.yml index be0435b..8f3c91f 100644 --- a/tasks/long_running_tasks.yml +++ b/tasks/long_running_tasks.yml @@ -3,66 +3,88 @@ rcount: "{{ 1 if rcount is undefined else rcount | int + 1 }}" +######################## +# FAILURE BLOCK: START # +######################## - name: Abort run and notify if retries exceed defined limits when: rcount|int >= fail_count block: - name: Show rcount and num_retries 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 ansible.builtin.uri: - url : "{{ webhook }}" + 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: - Cloudy: "With a chance of meatballs." + method : POST + status_code : 200 + validate_certs : false + return_content : false + body_format : json + body : | + "{{ inventory_hostname }}": "Job FAIL." - - name: End condition met. Aborting... + - name: End condition met. ansible.builtin.fail: msg: "Bye" +###################### +# FAILURE BLOCK: END # +###################### + +########################### +# SUCCESSFUL BLOCK: START # +########################### - name: We were successful when: rcount|int == num_retries block: - name: Show rcount and num_retries 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 ansible.builtin.uri: - url : "{{ webhook }}" + 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: - Somewhere: "Over the rainbow." + method : POST + status_code : 200 + validate_certs : false + return_content : false + body_format : json + body : | + "{{ 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 +######################### +# SUCCESSFUL BLOCK: END # +######################### + +######################## +# GENERAL TASKS: START # +######################## - name: Run a series of test commands - ansible.builtin.command : "{{ item }}" - changed_when : false - loop : "{{ check_commands }}" - register : r_check_commands + ansible.builtin.command: "{{ item }}" + loop : "{{ check_commands }}" + register : r_check_commands + notify : Bob - name: Current status ansible.builtin.debug: - msg: "Currently on run {{ rcount }} of {{ num_retries }}." + msg: "{{ inventory_hostname }}: Finished run {{ rcount }} of {{ num_retries }}." run_once: true - name: Log status update to API @@ -76,8 +98,8 @@ validate_certs : false return_content : false body_format : json - body: - Itsa: "me! Mario! - Finished run {{ rcount }} of {{ num_retries }}." + body: | + "{{ inventory_hostname }}": "Finished run {{ rcount }} of {{ num_retries }}." - name: Pause for effect ansible.builtin.pause: @@ -86,3 +108,7 @@ - name: Include ourself for recursion ansible.builtin.include_tasks: tasks/long_running_tasks.yml when: rcount|int <= num_retries + +######################## +# GENERAL TASKS: END # +######################## diff --git a/webhook_test_1.yml b/webhook_test_1.yml index 8c95900..c55d568 100644 --- a/webhook_test_1.yml +++ b/webhook_test_1.yml @@ -26,5 +26,15 @@ handlers: - name: Bob - ansible.builtin.debug: - msg: "We've reached equilibrium" + ansible.builtin.uri: + 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."