diff --git a/files/ansible-playbooks/redirect_and_block_remove.yml b/files/ansible-playbooks/redirect_and_block_remove.yml new file mode 100644 index 0000000..27482fc --- /dev/null +++ b/files/ansible-playbooks/redirect_and_block_remove.yml @@ -0,0 +1,704 @@ +# redirect_and_block_removal.yml — connectivity + stage block scripts removal +# Phase 0: reliable DEV2 connectivity + wrapper build +# Phase 1: run twice and remove the scripts + +- name: "Redirect and block | Connectivity + stage scripts" + hosts: all + gather_facts: no + + vars: + pathprefix: "PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH; " + + 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) }}" + + + dev1_user: "root" + dev1_pass: "wavewave" + + dev2_host: "192.168.1.1" + dev2_port: 22 + + dev2_side_ip: "192.168.1.11/24" + dev2_side_ip_addr: "{{ dev2_side_ip.split('/')[0] }}" + dev1_iface: "br-wan" + + arping_iface: "eth0" + + dev2_ssh_user: "root" + dev2_passfiles: + - "basicpass" + - "basicpass2" + + ssh_opts_common: >- + -o PreferredAuthentications=password + -o PubkeyAuthentication=no + -o StrictHostKeyChecking=no + -o UserKnownHostsFile=/dev/null + -o NumberOfPasswordPrompts=1 + -o ConnectTimeout=30 + -o ConnectionAttempts=1 + -o LogLevel=ERROR + + debugging: true + ssh_timeout: 30 + + block_src_dir: "/opt/containers/ansible-worker/app" + block_dst_dir: "/root" + block_files: + - "startblock.sh" + - "stopblock.sh" + + tasks: + - name: Initialize passfile facts (avoid undefined vars in later templates) + delegate_to: localhost + ansible.builtin.set_fact: + dev2_passfile_used_direct: "NONE" + dev2_passfile_used_tunnel: "NONE" + dev2_passfile_used_lldp4: "NONE" + dev2_passfile_used_lldp6: "NONE" + changed_when: false + + # ============================ PHASE 0: LLDP DISCOVERY (IPv4 + IPv6) ============================ + - name: Compute hostname digits key for LLDP lookup (DEV2) + ansible.builtin.set_fact: + dev2_lldp_digits: "{{ (inventory_hostname | string) | regex_replace('[^0-9]', '') }}" + changed_when: false + + - name: Discover DEV2 candidate IPv4 via LLDP on DEV1 (best-effort) + ansible.builtin.raw: > + {{ pathprefix }} + DIGITS="{{ dev2_lldp_digits }}"; + cat /var/run/lldp_server.json 2>/dev/null \ + | grep "${DIGITS}" -A 10 \ + | grep address \ + | grep -vE 'subtype|ipv6' \ + | awk -F'"' '{ print $4 }' \ + | head -n1 + register: dev2_lldp_ip_raw + changed_when: false + failed_when: false + + - name: Discover DEV2 candidate IPv6 via LLDP on DEV1 (best-effort) + ansible.builtin.raw: > + {{ pathprefix }} + DIGITS="{{ dev2_lldp_digits }}"; + cat /var/run/lldp_server.json 2>/dev/null \ + | grep "${DIGITS}" -A 15 \ + | grep 'address_ipv6' \ + | awk -F'"' '{ print $4 }' \ + | head -n1 + register: dev2_lldp_ip6_raw + changed_when: false + failed_when: false + + - name: Capture LLDP-derived DEV2 IP facts + ansible.builtin.set_fact: + lldp_dev2_ip: "{{ (dev2_lldp_ip_raw.stdout | default('')) | trim }}" + lldp_dev2_ip6: "{{ (dev2_lldp_ip6_raw.stdout | default('')) | trim }}" + changed_when: false + + - name: Classify LLDP IPv4 candidate + delegate_to: localhost + ansible.builtin.set_fact: + lldp_ip_class: >- + {% set ip = (lldp_dev2_ip | default('')) %} + {% if ip == '' %}none + {% elif ip.startswith('10.') %}10 + {% elif ip.startswith('192.168.') %}192_168 + {% else %}other{% endif %} + changed_when: false + + - name: Debug LLDP candidates + when: debugging | bool + delegate_to: localhost + ansible.builtin.debug: + msg: + - "LLDP digits={{ dev2_lldp_digits | default('') }}" + - "LLDP IPv4 candidate={{ lldp_dev2_ip | default('') }}" + - "LLDP IPv4 class={{ lldp_ip_class | default('none') }}" + - "LLDP IPv6 candidate={{ lldp_dev2_ip6 | default('') }}" + + # ============================ PHASE 0: PRIMARY PATH DECISION ============================ + - name: Override dev2_host from LLDP when candidate is 192.168.x.x + when: (lldp_ip_class | trim) == "192_168" and (lldp_dev2_ip | trim | length > 0) + delegate_to: localhost + ansible.builtin.set_fact: + dev2_host: "{{ lldp_dev2_ip | trim }}" + changed_when: false + + - name: Set connection method initial (direct_lldp if 10.x else tunnel) + delegate_to: localhost + ansible.builtin.set_fact: + dev2_conn_method: "{{ 'direct_lldp' if ((lldp_ip_class | trim) == '10') else 'tunnel' }}" + changed_when: false + + # ============================ PHASE 0: DIRECT LLDP IPv4 AUTH (10.x) ============================ + - name: Try DEV2 login via direct LLDP IPv4 (10.x) with basicpass + when: dev2_conn_method == "direct_lldp" + delegate_to: localhost + ansible.builtin.shell: | + set -e + HOST="{{ lldp_dev2_ip }}" + sshpass -f basicpass ssh {{ ssh_opts_common }} \ + -o AddressFamily=inet \ + "{{ dev2_ssh_user }}@${HOST}" echo OK >/dev/null 2>&1 + args: { executable: /bin/bash } + register: dev2_lldp_try_basicpass + changed_when: false + ignore_errors: true + + - name: Select basicpass for direct LLDP if succeeded + when: dev2_conn_method == "direct_lldp" and dev2_lldp_try_basicpass.rc == 0 + delegate_to: localhost + ansible.builtin.set_fact: + dev2_passfile_used_direct: "basicpass" + changed_when: false + + - name: Try DEV2 login via direct LLDP IPv4 (10.x) with basicpass2 (only if first failed) + when: dev2_conn_method == "direct_lldp" and (dev2_passfile_used_direct == "NONE") + delegate_to: localhost + ansible.builtin.shell: | + set -e + HOST="{{ lldp_dev2_ip }}" + sshpass -f basicpass2 ssh {{ ssh_opts_common }} \ + -o AddressFamily=inet \ + "{{ dev2_ssh_user }}@${HOST}" echo OK >/dev/null 2>&1 + args: { executable: /bin/bash } + register: dev2_lldp_try_basicpass2 + changed_when: false + ignore_errors: true + + - name: Select basicpass2 for direct LLDP if succeeded + when: dev2_conn_method == "direct_lldp" and dev2_passfile_used_direct == "NONE" and dev2_lldp_try_basicpass2.rc == 0 + delegate_to: localhost + ansible.builtin.set_fact: + dev2_passfile_used_direct: "basicpass2" + changed_when: false + + # ============================ PHASE 0: TUNNEL PREP (DEV1 temp IP + ARP + tunnel) ============================ + - name: Add temporary IP on DEV1 (tolerate 'File exists') + when: dev2_conn_method == "tunnel" + ansible.builtin.raw: > + {{ pathprefix }} + ip a add {{ dev2_side_ip }} dev {{ dev1_iface }} + register: add_ip + changed_when: add_ip.rc == 0 + failed_when: > + add_ip.rc != 0 + and ('File exists' not in (add_ip.stdout | default(''))) + and ('File exists' not in (add_ip.stderr | default(''))) + + - name: Discover DEV2 MAC via bridge fdb on DEV1 (best-effort) + when: dev2_conn_method == "tunnel" + ansible.builtin.raw: > + {{ pathprefix }} + bridge fdb show {{ dev1_iface }} | grep eth0 | grep -v permanent | grep master | awk '{print $1}' | head -n1 + register: dev2_mac_scan + changed_when: false + failed_when: false + + - name: Capture discovered DEV2 MAC (if any) + when: dev2_conn_method == "tunnel" + ansible.builtin.set_fact: + dev2_mac: "{{ (dev2_mac_scan.stdout | default('') ) | trim }}" + changed_when: false + + - name: Clear existing ARP entry for DEV2 on DEV1 (best-effort) + when: dev2_conn_method == "tunnel" + ansible.builtin.raw: > + {{ pathprefix }} + ip neigh del {{ dev2_host }} dev {{ dev1_iface }} 2>/dev/null || true + changed_when: false + failed_when: false + + - name: Add static ARP entry on DEV1 (if MAC discovered; tolerate 'File exists') + when: dev2_conn_method == "tunnel" and (dev2_mac | default('') | length > 0) + ansible.builtin.raw: > + {{ pathprefix }} + ip neigh add {{ dev2_host }} lladdr {{ dev2_mac }} dev {{ dev1_iface }} nud permanent + register: dev2_arp_add + changed_when: dev2_arp_add.rc == 0 + failed_when: > + dev2_arp_add.rc != 0 + and ('File exists' not in (dev2_arp_add.stdout | default(''))) + and ('File exists' not in (dev2_arp_add.stderr | default(''))) + + - name: Refresh ARP (best-effort) + when: dev2_conn_method == "tunnel" + ansible.builtin.raw: > + {{ pathprefix }} + arping -U -I {{ arping_iface }} {{ dev2_side_ip_addr }} -c 3 + changed_when: false + failed_when: false + + - name: Pick a free local TCP port for the tunnel (controller side) + when: dev2_conn_method == "tunnel" + delegate_to: localhost + ansible.builtin.shell: | + set -e + pick() { + for i in $(seq 1 25); do + p="$(shuf -i 20000-39999 -n 1)" + if command -v ss >/dev/null 2>&1; then + if ! ss -ltn | awk '{print $4}' | grep -qE "(:|\.)${p}$"; then + echo "$p"; return 0 + fi + else + if ! nc -z 127.0.0.1 "$p" >/dev/null 2>&1; then + echo "$p"; return 0 + fi + fi + done + return 1 + } + pick + args: { executable: /bin/bash } + register: pick_port + changed_when: false + + - name: Stop if no free local port was found + when: dev2_conn_method == "tunnel" and (pick_port.stdout | trim | length) == 0 + ansible.builtin.meta: end_host + + - name: Create control dir for SSH ControlMaster + when: dev2_conn_method == "tunnel" + delegate_to: localhost + ansible.builtin.shell: "mktemp -d" + args: { executable: /bin/bash } + register: mktemp_dir + changed_when: false + + - name: Record chosen local port and build ControlMaster socket path + when: dev2_conn_method == "tunnel" + delegate_to: localhost + ansible.builtin.set_fact: + _local_port: "{{ pick_port.stdout | trim }}" + _ctrl_dir: "{{ mktemp_dir.stdout | trim }}" + _ctrl_sock: "{{ (mktemp_dir.stdout | trim) }}/ssh_tunnel_ctl" + changed_when: false + + - name: Start SSH ControlMaster and forward 127.0.0.1 local_port to DEV2 port 22 via DEV1 + when: dev2_conn_method == "tunnel" + delegate_to: localhost + ansible.builtin.shell: | + set -e + USER="{{ dev1_user }}" + HOST="{{ ansible_host | default(inventory_hostname) }}" + sshpass -p '{{ dev1_pass }}' ssh -f -N {{ ssh_opts_common }} \ + -M -S "{{ _ctrl_sock }}" \ + -L "127.0.0.1:{{ _local_port }}:{{ dev2_host }}:{{ dev2_port }}" \ + "${USER}@${HOST}" + args: { executable: /bin/bash } + register: start_tunnel + changed_when: true + + - name: Probe TCP reachability to DEV2 through the tunnel (nc) + when: dev2_conn_method == "tunnel" + delegate_to: localhost + ansible.builtin.shell: | + set -e + nc -z -w5 127.0.0.1 "{{ _local_port }}" + args: { executable: /bin/bash } + register: nc_probe + changed_when: false + ignore_errors: true + + - name: Stop if tunnel TCP probe failed + when: dev2_conn_method == "tunnel" and nc_probe.rc != 0 + ansible.builtin.meta: end_host + + - name: Pick DEV2 password for root (tunnel) try basicpass + when: dev2_conn_method == "tunnel" + delegate_to: localhost + ansible.builtin.shell: | + set -e + PORT="{{ _local_port }}" + sshpass -f basicpass ssh {{ ssh_opts_common }} \ + -o AddressFamily=inet \ + -p "$PORT" root@127.0.0.1 echo OK >/dev/null 2>&1 + args: { executable: /bin/bash } + register: dev2_try_basicpass + changed_when: false + ignore_errors: true + + - name: Select basicpass if tunnel login succeeded + when: dev2_conn_method == "tunnel" and dev2_try_basicpass.rc == 0 + delegate_to: localhost + ansible.builtin.set_fact: + dev2_passfile_used_tunnel: "basicpass" + changed_when: false + + - name: Try DEV2 login through tunnel with basicpass2 (only if first failed) + when: dev2_conn_method == "tunnel" and (dev2_passfile_used_tunnel == "NONE") + delegate_to: localhost + ansible.builtin.shell: | + set -e + PORT="{{ _local_port }}" + sshpass -f basicpass2 ssh {{ ssh_opts_common }} \ + -o AddressFamily=inet \ + -p "$PORT" root@127.0.0.1 echo OK >/dev/null 2>&1 + args: { executable: /bin/bash } + register: dev2_try_basicpass2 + changed_when: false + ignore_errors: true + + - name: Select basicpass2 if tunnel login succeeded + when: dev2_conn_method == "tunnel" and dev2_passfile_used_tunnel == "NONE" and dev2_try_basicpass2.rc == 0 + delegate_to: localhost + ansible.builtin.set_fact: + dev2_passfile_used_tunnel: "basicpass2" + changed_when: false + + # ============================ PHASE 0: SAFETY GUARD (MAC CHECK) ============================ + - name: Read remote eth0 MAC via tunnel (guard, sanitized) + when: + - dev2_conn_method == "tunnel" + - (dev2_passfile_used_tunnel | default('NONE')) != "NONE" + - (dev2_mac | default('') | length) > 0 + delegate_to: localhost + ansible.builtin.shell: | + PORT="{{ _local_port }}" + sshpass -f "{{ dev2_passfile_used_tunnel }}" ssh {{ ssh_opts_common }} \ + -p "$PORT" "{{ dev2_ssh_user }}@127.0.0.1" \ + "cat /sys/class/net/eth0/address 2>/dev/null || ip link show eth0 2>/dev/null" 2>&1 + args: { executable: /bin/bash } + register: dev2_eth0_mac_raw + changed_when: false + failed_when: false + + - name: Normalize remote eth0 MAC (extract last MAC-like token) + when: + - dev2_conn_method == "tunnel" + - (dev2_passfile_used_tunnel | default('NONE')) != "NONE" + - (dev2_mac | default('') | length) > 0 + delegate_to: localhost + ansible.builtin.set_fact: + remote_eth0_mac: >- + {{ + ( + (dev2_eth0_mac_raw.stdout | default('') | regex_replace('\r','')) ~ "\n" ~ + (dev2_eth0_mac_raw.stderr | default('') | regex_replace('\r','')) + ) + | regex_findall('([0-9a-fA-F]{2}(?::[0-9a-fA-F]{2}){5})') + | last + | default('') + | lower + }} + changed_when: false + + - name: Abort if remote eth0 MAC != discovered DEV2 MAC + when: + - dev2_conn_method == "tunnel" + - (dev2_passfile_used_tunnel | default('NONE')) != "NONE" + - (dev2_mac | default('') | length) > 0 + - (remote_eth0_mac | default('') | length) > 0 + - (remote_eth0_mac | lower) != (dev2_mac | lower) + ansible.builtin.fail: + msg: >- + Safety stop: tunnel reached wrong device. + expected_dev2_mac={{ dev2_mac }}, remote_eth0_mac={{ remote_eth0_mac }} + + # ============================ PHASE 0: FALLBACKS (only if tunnel auth failed) ============================ + - name: Try DEV2 login via LLDP IPv4 10.x (fallback if tunnel auth failed) + when: + - dev2_conn_method == "tunnel" + - dev2_passfile_used_tunnel == "NONE" + - (lldp_dev2_ip | default('')) is match('^10\\.') + delegate_to: localhost + ansible.builtin.shell: | + set -e + HOST="{{ lldp_dev2_ip }}" + for f in {{ dev2_passfiles | join(' ') }}; do + if timeout 20s sshpass -f "$f" ssh {{ ssh_opts_common }} \ + -o AddressFamily=inet \ + -o ConnectTimeout=10 \ + "{{ dev2_ssh_user }}@${HOST}" echo OK >/dev/null 2>&1; then + echo "$f"; exit 0 + fi + done + echo "NONE"; exit 1 + args: { executable: /bin/bash } + register: dev2_auth_lldp4 + changed_when: false + failed_when: false + ignore_errors: true + + - name: Record LLDP IPv4 fallback decision + when: + - dev2_conn_method == "tunnel" + - dev2_passfile_used_tunnel == "NONE" + - (lldp_dev2_ip | default('')) is match('^10\\.') + delegate_to: localhost + ansible.builtin.set_fact: + dev2_passfile_used_lldp4: "{{ (dev2_auth_lldp4.rc == 0) | ternary((dev2_auth_lldp4.stdout | trim), 'NONE') }}" + changed_when: false + + - name: Copy DEV2 passfiles to DEV1 for IPv6 nested SSH (last resort) + when: + - dev2_conn_method == "tunnel" + - dev2_passfile_used_tunnel == "NONE" + - dev2_passfile_used_lldp4 == "NONE" + - (lldp_dev2_ip6 | default('') | length) > 0 + ansible.builtin.copy: + src: "{{ item }}" + dest: "/tmp/{{ item }}" + mode: "0600" + loop: "{{ dev2_passfiles }}" + ignore_errors: true + + - name: Try DEV2 login via LLDP IPv6 (nested SSH through DEV1; last resort) + when: + - dev2_conn_method == "tunnel" + - dev2_passfile_used_tunnel == "NONE" + - dev2_passfile_used_lldp4 == "NONE" + - (lldp_dev2_ip6 | default('') | length) > 0 + delegate_to: localhost + ansible.builtin.shell: | + set -e + IP6="{{ lldp_dev2_ip6 }}" + for f in {{ dev2_passfiles | join(' ') }}; do + if sshpass -p '{{ dev1_pass }}' ssh {{ ssh_opts_common }} \ + "{{ dev1_user }}@{{ ansible_host|default(inventory_hostname) }}" \ + "timeout {{ ssh_timeout }}s sshpass -f '/tmp/${f}' ssh \ + -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \ + -o PubkeyAuthentication=no \ + -o PreferredAuthentications=password -o NumberOfPasswordPrompts=1 \ + -o ConnectTimeout={{ ssh_timeout }} \ + '{{ dev2_ssh_user }}@\[${IP6}%{{ dev1_iface }}\]' 'echo OK' " \ + >/dev/null 2>&1; then + echo "$f"; exit 0 + fi + done + echo "NONE"; exit 1 + args: { executable: /bin/bash } + register: dev2_auth_lldp6 + changed_when: false + failed_when: false + ignore_errors: true + + - name: Record LLDP IPv6 fallback decision + when: + - dev2_conn_method == "tunnel" + - dev2_passfile_used_tunnel == "NONE" + - dev2_passfile_used_lldp4 == "NONE" + - (lldp_dev2_ip6 | default('') | length) > 0 + delegate_to: localhost + ansible.builtin.set_fact: + dev2_passfile_used_lldp6: "{{ (dev2_auth_lldp6.rc == 0) | ternary((dev2_auth_lldp6.stdout | trim), 'NONE') }}" + changed_when: false + + # ============================ PHASE 0: FINAL DECISION + WRAPPER FACTS ============================ + - name: Decide final DEV2 connection mode (direct_lldp > tunnel > lldp4 > lldp6) + delegate_to: localhost + ansible.builtin.set_fact: + dev2_conn_final: >- + {%- if dev2_conn_method == 'direct_lldp' and (dev2_passfile_used_direct | default('NONE')) != 'NONE' -%} + direct_lldp + {%- elif dev2_conn_method == 'tunnel' and (dev2_passfile_used_tunnel | default('NONE')) != 'NONE' -%} + tunnel + {%- elif (dev2_passfile_used_lldp4 | default('NONE')) != 'NONE' -%} + lldp4_fallback + {%- elif (dev2_passfile_used_lldp6 | default('NONE')) != 'NONE' -%} + lldp6_via_dev1 + {%- else -%} + none + {%- endif -%} + changed_when: false + + - name: Abort if all DEV2 connection methods failed + when: dev2_conn_final == "none" + ansible.builtin.fail: + msg: > + DEV2 unreachable: tunnel auth={{ dev2_passfile_used_tunnel | default('n/a') }}, + direct auth={{ dev2_passfile_used_direct | default('n/a') }}, + lldp4 auth={{ dev2_passfile_used_lldp4 | default('n/a') }}, + lldp6 auth={{ dev2_passfile_used_lldp6 | default('n/a') }}. + + - name: Debug final connectivity decision + when: debugging | bool + delegate_to: localhost + ansible.builtin.debug: + msg: + - "dev2_conn_final={{ dev2_conn_final }}" + - "tunnel passfile={{ dev2_passfile_used_tunnel | default('n/a') }}" + - "direct passfile={{ dev2_passfile_used_direct | default('n/a') }}" + - "lldp4 passfile={{ dev2_passfile_used_lldp4 | default('n/a') }}" + - "lldp6 passfile={{ dev2_passfile_used_lldp6 | default('n/a') }}" + - "tunnel target={{ dev2_host }}:{{ dev2_port }} (forwarded to 127.0.0.1:{{ _local_port | default('na') }})" + - "lldp ipv4={{ lldp_dev2_ip | default('') }}" + - "lldp ipv6={{ lldp_dev2_ip6 | default('') }}" + + - name: Build DEV2 exec wrapper (controller-side) for Phase 1 commands + delegate_to: localhost + ansible.builtin.set_fact: + dev2_exec_cmd: | + set -e + MODE="{{ dev2_conn_final }}" + if [ -z "${DEV2_CMD:-}" ]; then + echo "ERROR: DEV2_CMD is empty" >&2 + exit 2 + fi + + case "$MODE" in + tunnel) + PORT="{{ _local_port | default('') }}" + PASS="{{ dev2_passfile_used_tunnel }}" + sshpass -f "$PASS" ssh {{ ssh_opts_common }} \ + -o AddressFamily=inet \ + -p "$PORT" "{{ dev2_ssh_user }}@127.0.0.1" \ + "{{ pathprefix }} ${DEV2_CMD}" 2>&1 + ;; + direct_lldp) + HOST="{{ lldp_dev2_ip }}" + PASS="{{ dev2_passfile_used_direct }}" + sshpass -f "$PASS" ssh {{ ssh_opts_common }} \ + -o AddressFamily=inet \ + "{{ dev2_ssh_user }}@${HOST}" \ + "{{ pathprefix }} ${DEV2_CMD}" 2>&1 + ;; + lldp4_fallback) + HOST="{{ lldp_dev2_ip }}" + PASS="{{ dev2_passfile_used_lldp4 }}" + sshpass -f "$PASS" ssh {{ ssh_opts_common }} \ + -o AddressFamily=inet \ + "{{ dev2_ssh_user }}@${HOST}" \ + "{{ pathprefix }} ${DEV2_CMD}" 2>&1 + ;; + lldp6_via_dev1) + IP6="{{ lldp_dev2_ip6 }}" + F="{{ dev2_passfile_used_lldp6 }}" + sshpass -p "{{ dev1_pass }}" ssh {{ ssh_opts_common }} \ + "{{ dev1_user }}@{{ ansible_host | default(inventory_hostname) }}" \ + "sshpass -f '/tmp/${F}' ssh \ + -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \ + -o PubkeyAuthentication=no \ + -o PreferredAuthentications=password -o NumberOfPasswordPrompts=1 \ + -o ConnectTimeout={{ ssh_timeout }} \ + '{{ dev2_ssh_user }}@\[${IP6}%{{ dev1_iface }}\]' \ + '{{ pathprefix }} '"${DEV2_CMD}" 2>&1" + ;; + *) + echo "ERROR: unknown MODE=$MODE" >&2 + exit 3 + ;; + esac + changed_when: false + + + ########################################################################## + # PHASE 1: ROLLBACK (execute stop, remove scripts, journal + custom field) + ########################################################################## + + - name: Run stopblock twice with 2s pause + delegate_to: localhost + ansible.builtin.shell: | + export DEV2_CMD='/root/stopblock.sh >/dev/null 2>&1 || true; sleep 2; /root/stopblock.sh >/dev/null 2>&1 || true' + {{ dev2_exec_cmd }} + args: + executable: /bin/bash + changed_when: true + + - name: Remove block scripts from DEV2 + delegate_to: localhost + ansible.builtin.shell: | + export DEV2_CMD='rm -f /root/stopblock.sh /root/startblock.sh' + {{ dev2_exec_cmd }} + args: + executable: /bin/bash + changed_when: true + + - name: Publish redirect and block removal journal + 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': (ansible_hostname | default(inventory_hostname)), + 'task_name': 'journal_add', + 'task_result': 'redirect and block removed' + } | to_json }}" + payload_encoding: "string" + changed_when: false + + - name: Set NetBox custom field indoor_rnb to cleared + 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': (ansible_hostname | default(inventory_hostname)), + 'task_name': 'custom_field_set', + 'task_add1': 'indoor_rnb', + 'task_result': 'cleared' + } | to_json }}" + payload_encoding: "string" + changed_when: false + + + post_tasks: + - name: Cleanup note + delegate_to: localhost + ansible.builtin.debug: + msg: "Cleanup: closing tunnel, removing temp IP, removing staged passfiles (best-effort)" + changed_when: false + + - name: Close SSH ControlMaster (best-effort) + when: dev2_conn_method | default('') == "tunnel" + delegate_to: localhost + ansible.builtin.shell: | + ssh -S "{{ _ctrl_sock | default('/dev/null') }}" -O exit 2>/dev/null || true + args: { executable: /bin/bash } + changed_when: false + ignore_errors: true + + - name: Remove tunnel control dir (best-effort) + when: dev2_conn_method | default('') == "tunnel" + delegate_to: localhost + ansible.builtin.file: + path: "{{ _ctrl_dir | default('/tmp/none') }}" + state: absent + ignore_errors: true + + - name: Remove staged passfiles from DEV1 (best-effort) + when: dev2_conn_method | default('') == "tunnel" + ansible.builtin.raw: > + {{ pathprefix }} + rm -f /tmp/basicpass /tmp/basicpass2 2>/dev/null || true + changed_when: false + failed_when: false + + - name: Remove temporary IP on DEV1 (tolerate 'Cannot assign requested address') + when: dev2_conn_method | default('') == "tunnel" + ansible.builtin.raw: > + {{ pathprefix }} + ip a del {{ dev2_side_ip }} dev {{ dev1_iface }} + register: del_ip + changed_when: del_ip.rc == 0 + failed_when: > + del_ip.rc != 0 + and ('Cannot assign requested address' not in (del_ip.stdout | default(''))) + and ('Cannot assign requested address' not in (del_ip.stderr | default('')))