commit bc812e3bc9308eed36f6f22d5a4e6e32bf9b4aea Author: Chris Hammer Date: Wed Jun 21 19:18:42 2023 -0400 initial project commit diff --git a/.ansible-lint b/.ansible-lint new file mode 100644 index 0000000..6bf43dc --- /dev/null +++ b/.ansible-lint @@ -0,0 +1,4 @@ +skip_list: + - yaml[colons] + - yaml[empty-lines] + - yaml[line-length] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a156f6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +collections +facts.d +.vscode diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..35c69a6 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,9 @@ +[defaults] +inventory = hosts +roles_path = roles +collections_path = collections +fact_caching_timeout = 300 +host_key_checking = False +display_skipped_hosts = False +bin_ansible_callbacks = True +callbacks_enabled = ansible.posix.profile_tasks, ansible.posix.timer diff --git a/hosts b/hosts new file mode 100644 index 0000000..2302eda --- /dev/null +++ b/hosts @@ -0,0 +1 @@ +localhost ansible_connection=local diff --git a/requirements.yml b/requirements.yml new file mode 100644 index 0000000..a0cd255 --- /dev/null +++ b/requirements.yml @@ -0,0 +1,3 @@ +--- +collections: + - name: ansible.posix diff --git a/tasks/long_running_tasks.yml b/tasks/long_running_tasks.yml new file mode 100644 index 0000000..be0435b --- /dev/null +++ b/tasks/long_running_tasks.yml @@ -0,0 +1,88 @@ +- name: Increment counter + ansible.builtin.set_fact: + rcount: "{{ 1 if rcount is undefined else rcount | int + 1 }}" + + +- 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 }}" + + - name: Log failure to API + 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: + Cloudy: "With a chance of meatballs." + + - name: End condition met. Aborting... + ansible.builtin.fail: + msg: "Bye" + + +- 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 }}" + + - name: Log success to API + 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: + Somewhere: "Over the rainbow." + + - name: End condition met. Exiting gracefully... + ansible.builtin.meta: end_play + + +- name: Run a series of test commands + ansible.builtin.command : "{{ item }}" + changed_when : false + loop : "{{ check_commands }}" + register : r_check_commands + +- name: Current status + ansible.builtin.debug: + msg: "Currently on run {{ rcount }} of {{ num_retries }}." + run_once: true + +- name: Log status update to API + 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: + Itsa: "me! Mario! - Finished run {{ rcount }} of {{ num_retries }}." + +- name: Pause for effect + ansible.builtin.pause: + seconds: "{{ pause_time }}" + +- name: Include ourself for recursion + ansible.builtin.include_tasks: tasks/long_running_tasks.yml + when: rcount|int <= num_retries diff --git a/webhook_test_1.yml b/webhook_test_1.yml new file mode 100644 index 0000000..8c95900 --- /dev/null +++ b/webhook_test_1.yml @@ -0,0 +1,30 @@ +- name: "Webhook Test 1: Long running job" + hosts: all + gather_facts: false + become: false + + + vars: + pause_time : 1 + num_retries : 5 + fail_count : 30 + poll_interval : 10 + + webhook : "https://webhooks.thezengarden.net/webhook-callback-poc" + webhook_key : "fYnWhoF42NFsN2p5tiB3Wd65HgB1oc-Qo2jXS6TTmZov" + + check_commands: + - uname -a + - uptime + - ping -c 1 127.0.0.1 + + + tasks: + - name: Include long_running_tasks.yml + ansible.builtin.include_tasks: tasks/long_running_tasks.yml + + + handlers: + - name: Bob + ansible.builtin.debug: + msg: "We've reached equilibrium"