Skip to content

Instantly share code, notes, and snippets.

@iamgini
Last active May 13, 2025 09:42
Show Gist options
  • Save iamgini/cb3283b6cb8a1c2c3d892dff3d57e5c2 to your computer and use it in GitHub Desktop.
Save iamgini/cb3283b6cb8a1c2c3d892dff3d57e5c2 to your computer and use it in GitHub Desktop.
temp
- name: Execute command
block:
- name: Get date time before execution
ansible.builtin.set_fact:
sg_current_date_time_before: "{{ lookup('pipe','date -d \"now + 8 hours\" +%Y%m%d%H%M%SZ') | string }}"
- name: Run health check commands on {{ target_jump_server }}
ansible.builtin.shell: |
{{ hc_command[0] | replace('SITE_NAME', site_name) }} << EOF
{% for cmd in hc_command[1:] %}
'{{ cmd }}'
{% endfor %}
EOF
register: hc_command_output
delegate_to: "{{ target_jump_server }}"
- name: Show output if any
ansible.builtin.debug:
msg:
- "Stdout lines below:"
- "{{ hc_command_output | default('NA') }}"
- name: Get date time after success
ansible.builtin.set_fact:
sg_current_date_time_after: "{{ lookup('pipe', 'date -d "now + 8 hours" +%Y%m%d%H%M%SZ') | string }}"
- name: Get Time taken for success
ansible.builtin.set_fact:
total_time_taken: "{{ (sg_current_date_time_after | to_datetime('%Y%m%d%H%M%SZ') - sg_current_date_time_before | to_datetime('%Y%m%d%H%M%SZ')).total_seconds() | int }}"
- name: Update log
ansible.builtin.include_role:
name: db_operation
vars:
db_operation_description: 'Insert or Update HC and Log after health check'
db_operation_query: |
UPDATE {{ db_hc_table }}
SET
mae_bsm_ssh_connection_status = 'SUCCESS',
mae_bsm_health_check_command = {{ hc_command | join(", ")| replace('SITE_NAME', site_name) | quote }},
mae_bsm_health_check_status = 'SUCCESS',
mae_bsm_health_check_command_return = {{ hc_command_output.stdout | default("NOT AVAILABLE") | quote }},
ansible_process_status = 'New'
WHERE fld_ticketid = '{{ cr_number }}'
AND fld_ciname LIKE '%{{ site_name }}%';
INSERT INTO {{ db_log_table }}
(fld_ticketid, fld_ciname, fld_mobilenetworktype__c, fld_impactdatefrom, fld_impactdateto,
mae_bsm_ssh_connection_status, mae_bsm_health_check_command, mae_bsm_health_check_status,
mae_bsm_ssh_connection_time, mae_bsm_health_check_command_return, ansible_process_status, remarks, user_email)
VALUES (
'{{ cr_number }}',
'{{ site_name }}',
'{{ network_technology }}',
'{{ workflow_data.cr_date_from | default("NA") }}',
'{{ workflow_data.cr_date_to | default("NA") }}',
'SUCCESS',
{{ hc_command | join(", ")| replace('SITE_NAME', site_name) | quote }},
'SUCCESS',
'{{ total_time_taken }}',
{{ hc_command_output.stdout | default("NOT AVAILABLE") | quote }},
'New',
'Health Check Success',
'{{ executor_email }}'
);
rescue:
- name: Get date time after
ansible.builtin.set_fact:
sg_current_date_time_after: "{{ lookup('pipe','date -d \"now + 8 hours\" +%Y%m%d%H%M%SZ') | string }}"
- name: Get Time taken (for failed execution)
ansible.builtin.set_fact:
total_time_taken: "{{ (sg_current_date_time_after | to_datetime('%Y%m%d%H%M%SZ') - sg_current_date_time_before | to_datetime('%Y%m%d%H%M%SZ')).total_seconds() | int }}"
- name: Update log for fail
ansible.builtin.include_role:
name: db_operation
vars:
db_operation_description: 'Insert or Update HC and Log after health check'
db_operation_query: |
UPDATE {{ db_hc_table }}
SET
mae_bsm_ssh_connection_status = 'SUCCESS',
mae_bsm_health_check_command = {{ hc_command | join(", ") | replace('SITE_NAME', site_name) | quote }},
mae_bsm_health_check_status = 'FAIL',
mae_bsm_health_check_command_return = {{ hc_command_output.stdout | default("NOT AVAILABLE") | quote }},
ansible_process_status = 'New'
WHERE fld_ticketid = '{{ cr_number }}'
AND fld_ciname LIKE '%{{ site_name }}%';
INSERT INTO {{ db_log_table }}
(fld_ticketid, fld_ciname, fld_mobilenetworktype__c, fld_impactdatefrom, fld_impactdateto,
mae_bsm_ssh_connection_status, mae_bsm_health_check_command, mae_bsm_health_check_status,
mae_bsm_ssh_connection_time, mae_bsm_health_check_command_return, ansible_process_status, remarks, user_email)
VALUES (
'{{ cr_number }}',
'{{ site_name }}',
'{{ network_technology }}',
'{{ workflow_data.cr_date_from | default("NA") }}',
'{{ workflow_data.cr_date_to | default("NA") }}',
'SUCCESS',
{{ hc_command | join(", ") | replace('SITE_NAME', site_name) | quote }},
'FAIL',
'{{ total_time_taken }}',
{{ hc_command_output.stdout | default("NOT AVAILABLE") | quote }},
'New',
'Health Check Success',
'{{ executor_email }}'
);
- name: Show output with error if any
ansible.builtin.debug:
msg:
- "stdout lines below:"
- "{{ hc_command_output | default('NA') }}"
- "stderror lines below:"
- "{{ hc_command_output.stderr_lines | default('NA') }}"
- name: Check if connection issue to site
ansible.builtin.debug:
msg:
- "Unable to connect to the site: {{ site_name }}"
when: >
('Unable to connect to' in hc_command_output.stdout_lines | join(' ')) or
('Cannot connect to MO service, exiting' in hc_command_output.stdout | join(' ')) or
('Unable to connect to' in hc_command_output.stderr_lines | join(' ')) or
('Cannot connect to MO service, exiting' in hc_command_output.stderr_lines | join(' '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment