Files
ansible-worker/files/ansible-playbooks/connstats-remove.yml
2026-08-11 10:34:28 +03:00

180 lines
6.8 KiB
YAML

---
- name: Remove connstats (single device, linear)
hosts: all
gather_facts: no
vars:
ssh_user: "{{ ansible_user | default('root') }}"
ssh_pass: "{{ ansible_password | default(ansible_ssh_pass) }}"
# RabbitMQ (use controls exchange + queue_controls like the reference)
rmq_host: "{{ lookup('env','RMQ_HOST') | default('10.210.12.2', true) }}"
rmq_port: "{{ lookup('env','RMQ_PORT') | default('15672', true) }}"
rmq_user: "{{ lookup('env','RMQ_USER') | default('admin', true) }}"
rmq_pass: "{{ lookup('env','RMQ_PASS') | default('change_me', true) }}"
rmq_vhost: "{{ lookup('env','RMQ_VHOST') | default('app', true) }}"
rmq_exchange: "{{ lookup('env','RMQ_EXCHANGE') | default('controls', true) }}"
control_queue: "{{ lookup('env','CONTROL_QUEUE') | default('queue_controls', true) }}"
tasks:
- block:
# --- SSH reachability check ---
- name: Check SSH connectivity (raw ping)
raw: "echo ping"
register: ping_result
ignore_errors: true
- block:
############ step 2
- name: Check if connstats cron line is present
raw: |
if [ -f /etc/crontabs/root ]; then
grep -Eq '/root/connstats\.sh([[:space:]]|$)' /etc/crontabs/root
else
exit 1
fi
register: cron_grep
failed_when: false
changed_when: false
- name: Remove connstats cron line (preserve any other lines)
when: cron_grep.rc == 0
raw: |
sed -i '\|/root/connstats\.sh|d' /etc/crontabs/root
register: cron_remove
changed_when: true
- name: Check if /root/connstats.log exists
raw: "test -e /root/connstats.log"
register: connstats_log_grep
failed_when: false
changed_when: false
- name: Remove /root/connstats.log
when: connstats_log_grep.rc == 0
raw: "rm -f /root/connstats.log"
register: connstats_log_remove
changed_when: true
- name: Check if /root/connstats.sh exists
raw: "test -e /root/connstats.sh"
register: connstats_script_grep
failed_when: false
changed_when: false
- name: Remove /root/connstats.sh
when: connstats_script_grep.rc == 0
raw: "rm -f /root/connstats.sh"
register: connstats_script_remove
changed_when: true
- name: Set result status (success removed or no change)
set_fact:
result_status: >-
{{ 'SUCCESS_REMOVED'
if ((cron_grep.rc == 0)
or (connstats_log_grep.rc == 0)
or (connstats_script_grep.rc == 0))
else 'SUCCESS_NO_CHANGE' }}
when: ping_result is succeeded
- name: Set status fact (no ssh)
when: ping_result is failed
set_fact:
result_status: "NO_SSH"
rescue:
- name: Mark result as failed
set_fact:
result_status: "FAILED during {{ ansible_failed_task.name }}"
always:
- name: Compute inscope device
set_fact:
inscope_device_name: "{{ ansible_hostname | default(inventory_hostname) }}"
# custom field update (per your sample)
- name: Publish custom-field update connstats removed to control queue
when: result_status == 'SUCCESS_REMOVED' or result_status == 'SUCCESS_NO_CHANGE'
delegate_to: localhost
uri:
url: "http://{{ rmq_host }}:{{ rmq_port }}/api/exchanges/{{ rmq_vhost | urlencode }}/{{ rmq_exchange | urlencode }}/publish"
method: POST
user: "{{ rmq_user }}"
password: "{{ rmq_pass }}"
force_basic_auth: true
status_code: 200
headers: { content-type: "application/json" }
body_format: json
body:
properties: { content_type: "application/json" }
routing_key: "{{ control_queue }}"
payload: "{{ {
'inscope_device': inscope_device_name,
'task_name': 'custom_field_set',
'task_add1': 'connstats',
'task_result': 'removed'
} | to_json }}"
payload_encoding: "string"
register: rmq_cf
changed_when: false
# final wrap-up journal "connstats: ..." with actions performed
- name: Build actions list
set_fact:
_actions_list: >-
{{
[]
+ (((cron_grep is defined) and ((cron_grep.rc | default(1)) == 0)) | ternary(['removed connstats crontab entry'], []))
+ (((connstats_log_grep is defined) and ((connstats_log_grep.rc | default(1)) == 0)) | ternary(['removed connstats.log'], []))
+ (((connstats_script_grep is defined) and ((connstats_script_grep.rc | default(1)) == 0)) | ternary(['removed connstats.sh'], []))
}}
- name: Build actions string
set_fact:
_actions_str: "{{ ((_actions_list | default([])) | length > 0) | ternary((_actions_list | join(', ')), 'no changes needed') }}"
- name: Build wrap-up journal payload
delegate_to: localhost
set_fact:
wrap_payload:
inscope_device: "{{ inscope_device_name }}"
task_name: "journal_add"
task_result: >-
connstats: {{ 'success' if (result_status == 'SUCCESS_REMOVED' or result_status == 'SUCCESS_NO_CHANGE') else result_status | lower }}
— actions: {{ _actions_str }}
- name: Publish wrap-up journal to control queue
delegate_to: localhost
uri:
url: "http://{{ rmq_host }}:{{ rmq_port }}/api/exchanges/{{ rmq_vhost | urlencode }}/{{ rmq_exchange | urlencode }}/publish"
method: POST
user: "{{ rmq_user }}"
password: "{{ rmq_pass }}"
force_basic_auth: true
status_code: 200
headers:
content-type: "application/json"
body_format: json
body:
properties:
content_type: "application/json"
routing_key: "{{ control_queue }}"
payload: "{{ wrap_payload | to_json }}"
payload_encoding: "string"
register: rmq_wrap
changed_when: (rmq_wrap.json is defined) and (rmq_wrap.json.routed | default(false) | bool)
# Local summary (kept for operator visibility)
- name: Summary
debug:
msg:
- "result_status: {{ result_status }}"
- "we're good"