| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# Copyright 2022 Red Hat |
| 4 |
# GNU General Public License v3.0+ |
| 5 |
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
""" |
| 8 |
The module file for iosxr_ping |
| 9 |
""" |
| 10 |
|
| 11 |
from __future__ import absolute_import, division, print_function |
| 12 |
|
| 13 |
|
| 14 |
__metaclass__ = type |
| 15 |
|
| 16 |
DOCUMENTATION = """ |
| 17 |
module: iosxr_ping |
| 18 |
short_description: Tests reachability using ping from IOSXR switch. |
| 19 |
description: |
| 20 |
- Tests reachability using ping from switch to a remote destination. |
| 21 |
- For a general purpose network module, see the L(net_ping,https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/net_ping_module.html) |
| 22 |
module. |
| 23 |
- For Windows targets, use the L(win_ping,https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_ping_module.html) |
| 24 |
module instead. |
| 25 |
- For targets running Python, use the L(ping,https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ping_module.html) |
| 26 |
module instead. |
| 27 |
notes: |
| 28 |
- Tested against IOSXR 7.2.2. |
| 29 |
- This module works with connection C(network_cli). |
| 30 |
- For a general purpose network module, see the L(net_ping,https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/net_ping_module.html) |
| 31 |
module. |
| 32 |
- For Windows targets, use the L(win_ping,https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_ping_module.html) |
| 33 |
module instead. |
| 34 |
- For targets running Python, use the L(ping,https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ping_module.html) module instead. |
| 35 |
|
| 36 |
author: Sagar Paul (@KB-perByte) |
| 37 |
options: |
| 38 |
count: |
| 39 |
description: |
| 40 |
- Repeat count the number of packets to send. |
| 41 |
type: int |
| 42 |
afi: |
| 43 |
description: |
| 44 |
- Define echo type ipv4 or ipv6. |
| 45 |
choices: |
| 46 |
- ipv4 |
| 47 |
- ipv6 |
| 48 |
default: ipv4 |
| 49 |
type: str |
| 50 |
dest: |
| 51 |
description: |
| 52 |
- The IP Address or hostname (resolvable by switch) of the remote node. |
| 53 |
required: true |
| 54 |
type: str |
| 55 |
df_bit: |
| 56 |
description: |
| 57 |
- Set the DF bit in IP-header. |
| 58 |
default: false |
| 59 |
type: bool |
| 60 |
sweep: |
| 61 |
description: |
| 62 |
- Sweep ping. |
| 63 |
default: false |
| 64 |
type: bool |
| 65 |
validate: |
| 66 |
description: |
| 67 |
- Validate return packet. |
| 68 |
default: false |
| 69 |
type: bool |
| 70 |
source: |
| 71 |
description: |
| 72 |
- Source address or source interface. |
| 73 |
type: str |
| 74 |
size: |
| 75 |
description: |
| 76 |
- Datagram size the size of packets to send. |
| 77 |
type: int |
| 78 |
state: |
| 79 |
description: |
| 80 |
- Determines if the expected result is success or fail. |
| 81 |
choices: |
| 82 |
- absent |
| 83 |
- present |
| 84 |
default: present |
| 85 |
type: str |
| 86 |
vrf: |
| 87 |
description: |
| 88 |
- The VRF to use for forwarding. |
| 89 |
type: str |
| 90 |
""" |
| 91 |
|
| 92 |
EXAMPLES = """ |
| 93 |
- name: Test reachability to 198.51.100.251 using default vrf |
| 94 |
cisco.iosxr.iosxr_ping: |
| 95 |
dest: 198.51.100.251 |
| 96 |
|
| 97 |
- name: Test reachability to 198.51.100.252 using prod vrf |
| 98 |
cisco.iosxr.iosxr_ping: |
| 99 |
dest: 198.51.100.252 |
| 100 |
vrf: prod |
| 101 |
afi: ipv4 |
| 102 |
|
| 103 |
- name: Test unreachability to 198.51.100.253 using default vrf |
| 104 |
cisco.iosxr.iosxr_ping: |
| 105 |
dest: 198.51.100.253 |
| 106 |
state: absent |
| 107 |
|
| 108 |
- name: Test reachability to 198.51.100.250 using prod vrf and setting count and source |
| 109 |
cisco.iosxr.iosxr_ping: |
| 110 |
dest: 198.51.100.250 |
| 111 |
source: loopback0 |
| 112 |
vrf: prod |
| 113 |
count: 20 |
| 114 |
|
| 115 |
- name: Test reachability to 198.51.100.249 using df-bit and size |
| 116 |
cisco.iosxr.iosxr_ping: |
| 117 |
dest: 198.51.100.249 |
| 118 |
df_bit: true |
| 119 |
size: 1400 |
| 120 |
|
| 121 |
- name: Test reachability to ipv6 address |
| 122 |
cisco.iosxr.iosxr_ping: |
| 123 |
dest: 2001:db8:ffff:ffff:ffff:ffff:ffff:ffff |
| 124 |
afi: ipv6 |
| 125 |
""" |
| 126 |
|
| 127 |
RETURN = """ |
| 128 |
commands: |
| 129 |
description: Show the command sent. |
| 130 |
returned: always |
| 131 |
type: list |
| 132 |
sample: ["ping vrf prod 198.51.100.251 count 20 source loopback0"] |
| 133 |
packet_loss: |
| 134 |
description: Percentage of packets lost. |
| 135 |
returned: always |
| 136 |
type: str |
| 137 |
sample: "0%" |
| 138 |
packets_rx: |
| 139 |
description: Packets successfully received. |
| 140 |
returned: always |
| 141 |
type: int |
| 142 |
sample: 20 |
| 143 |
packets_tx: |
| 144 |
description: Packets successfully transmitted. |
| 145 |
returned: always |
| 146 |
type: int |
| 147 |
sample: 20 |
| 148 |
rtt: |
| 149 |
description: Show RTT stats. |
| 150 |
returned: always |
| 151 |
type: dict |
| 152 |
sample: {"avg": 2, "max": 8, "min": 1} |
| 153 |
""" |
| 154 |
|
| 155 |
from ansible.module_utils.basic import AnsibleModule |
| 156 |
|
| 157 |
from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.argspec.ping.ping import ( |
| 158 |
PingArgs, |
| 159 |
) |
| 160 |
from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.config.ping.ping import Ping |
| 161 |
|
| 162 |
|
| 163 |
def main(): |
| 164 |
""" |
| 165 |
Main entry point for module execution |
| 166 |
|
| 167 |
:returns: the result form module invocation |
| 168 |
""" |
| 169 |
module = AnsibleModule(argument_spec=PingArgs.argument_spec) |
| 170 |
|
| 171 |
result = Ping(module).execute_module() |
| 172 |
module.exit_json(**result) |
| 173 |
|
| 174 |
|
| 175 |
if __name__ == "__main__": |
| 176 |
main() |