Files
tower-tests/inmem-split-host.yml

184 lines
6.5 KiB
YAML

---
- name: Run a series of tasks, with exception handling, on a given host
hosts: all
gather_facts: no
become: no
vars_files:
- vars/defaults.yml
vars:
provision_host: "{{ tower_provision_host | default(ansible_play_hosts | random) }}"
tasks:
- name : "Create in-memory inventory to run against"
add_host :
name : "{{ item }}"
groups :
- remdiation
delegate_to: localhost
loop: "{{ provision_host.split(',') }}" # Please take note of the split
# differences between Ansible versions
# str.split() - Ansible <= 2.9
# str | split - Ansible >= 2.10
# =========================================================================== #
- name: Verify file existence and handle errors if needed
hosts: remdiation
gather_facts: yes
become: no
vars_files:
- vars/defaults.yml
tasks:
- name : Run file test
###############
# BLOCK TASKS #
###############
block :
- name : "Test #1: register file contents"
command :
cmd : "cat {{ check_file }}"
changed_when : no
register : r_check_file
- name : "Test #2: Launch the `file_removal_test` job template"
no_log : "{{ __no_log | default('yes') }}"
uri :
url : "{{ tower_file_remove_url }}"
force_basic_auth : yes
user : "{{ tower_user }}"
password : "{{ tower_password }}"
body_format : json
method : POST
status_code : 201
body:
extra_vars:
tower_provision_host: "{{ inventory_hostname }}"
- name : Return job data to originating source
no_log : "{{ __no_log | default('yes') }}"
uri :
url : "{{ gitlab_commit_url }}"
headers :
PRIVATE-TOKEN : "{{ gitlab_pat }}"
method : POST
status_code : 201
body_format : json
return_content : no
validate_certs : no
body:
id : "{{ gitlab_proj_id }}"
branch : "{{ git_commit_branch }}"
commit_message : "Job {{ lookup('env', 'JOB_ID') | default('N/A', true) }} - {{ inventory_hostname }}; SUCCESS: {{ r_check_file.stdout }}"
author_name : "{{ git_commit_author }}"
author_email : "{{ git_commit_email }}"
actions:
- action : create
file_path : "successful_jobs/{{ lookup('env', 'JOB_ID') }}_\
{{ inventory_hostname }}_\
{{ ansible_date_time.iso8601 }}.txt"
content : |
tower_provision_host : {{ inventory_hostname }}
tower_job_id : {{ lookup('env', 'JOB_ID') | default('N/A', true) }}
tower_project_revision : {{ lookup('env', 'PROJECT_REVISION') | default('N/A', true) }}
tower_provision_rc : {{ r_check_file.rc | default('N/A', true) }}
tower_provision_stdout : {{ r_check_file.stdout | default('N/A', true) }}
###################
# END BLOCK TASKS #
###################
################
# RESCUE TASKS #
################
rescue:
- name : Exception caught
debug :
msg : "*** EXCEPTION CAUGHT; RUNNING RESCUE TASKS ***"
- name : "Create missing file on {{ inventory_hostname }}"
no_log : "{{ __no_log | default('yes') }}"
uri :
url : "{{ tower_file_creation_url }}"
force_basic_auth : yes
user : "{{ tower_user }}"
password : "{{ tower_password }}"
body_format : json
method : POST
status_code : 201
body:
extra_vars:
tower_provision_host: "{{ inventory_hostname }}"
- name : Return job data to originating source
no_log : "{{ __no_log | default('yes') }}"
uri :
url : "{{ gitlab_commit_url }}"
headers :
PRIVATE-TOKEN : "{{ gitlab_pat }}"
method : POST
status_code : 201
body_format : json
return_content : no
validate_certs : no
body:
id : "{{ gitlab_proj_id }}"
branch : "{{ git_commit_branch }}"
commit_message : "Job {{ lookup('env', 'JOB_ID') | default('N/A', true) }} - {{ inventory_hostname }}; FAILED: {{ r_check_file.msg }}"
author_name : "{{ git_commit_author }}"
author_email : "{{ git_commit_email }}"
actions:
- action : create
file_path : "failed_jobs/{{ lookup('env', 'JOB_ID') }}_\
{{ inventory_hostname }}_\
{{ ansible_date_time.iso8601 }}.txt"
content : |
tower_provision_host : {{ inventory_hostname }}
tower_job_id : {{ lookup('env', 'JOB_ID') | default('N/A', true) }}
tower_project_revision : {{ lookup('env', 'PROJECT_REVISION') | default('N/A', true) }}
tower_provision_rc : {{ r_check_file.rc | default('N/A', true) }}
tower_provision_stdout : {{ r_check_file.stdout | default('N/A', true) }}
tower_provision_stderr : {{ r_check_file.stderr | default('N/A', true) }}
tower_provision_msg : {{ r_check_file.msg | default('N/A', true) }}
####################
# END RESCUE TASKS #
####################
################
# ALWAYS TASKS #
################
always:
- name : Job complete
debug :
msg :
- "tower_provision_host : {{ inventory_hostname }}"
- "tower_provision_failed : {{ r_check_file.failed }}"
####################
# END ALWAYS TASKS #
####################
...