# Copyright (c) 2023-2024 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
---

# IP Reachability tests

- name: Gather ip reachability state (directly connected interfaces)
  arista.eos.eos_command:
    commands: "ping {{ peer_ip | ansible.utils.ipaddr('address') }} source {{ ethernet_interface.ip_address | ansible.utils.ipaddr('address') }} repeat 1"  # noqa: jinja[invalid]
  loop: "{{ ethernet_interfaces | arista.avd.default({}) | arista.avd.convert_dicts('name') }}"
  loop_control:
    loop_var: ethernet_interface
  ignore_errors: "{{ eos_validate_state_validation_mode_loose }}"
  when: |
    ethernet_interfaces is arista.avd.defined and
    ethernet_interface.type is arista.avd.defined('routed') and
    ethernet_interface.ip_address is arista.avd.defined and
    ethernet_interface.peer is arista.avd.defined and
    ethernet_interface.peer_interface is arista.avd.defined and
    peer_ip is arista.avd.defined and peer_ip
  vars:
    peer_ip: "{{ (hostvars[ethernet_interface.peer | arista.avd.default('_')].ethernet_interfaces | arista.avd.default({}) | arista.avd.convert_dicts('name') | selectattr('name', 'eq', ethernet_interface.peer_interface | arista.avd.default) | map(attribute='ip_address'))[0] | arista.avd.default }}"

  register: ip_reachability_state
  tags:
    - ip_reachability

- name: Validate ip reachability (directly connected interfaces)
  delegate_to: localhost
  ansible.builtin.assert:
    that:
      - ip_reachability_test.stdout[0] | regex_search('1 received')
    fail_msg: "100% packet loss"
    quiet: true
  loop: "{{ ip_reachability_state.results }}"
  loop_control:
    loop_var: ip_reachability_test
  ignore_errors: "{{ eos_validate_state_validation_mode_loose }}"
  when: (ip_reachability_test.skipped is not defined)
  register: ip_reachability_results
  tags:
    - ip_reachability
