1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# GNU General Public License v3.0+
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r"""
---
module: vyos_ping
short_description: Test reachability using ping via VyOS REST API.
description:
- Sends an ICMP ping via the VyOS HTTPS REST API (/show endpoint) and
checks reachability. Mirrors C(vyos.vyos.vyos_ping).
version_added: "1.0.0"
author:
- VyOS Community (@vyos)
options:
dest:
description: Destination IP address or hostname.
type: str
required: true
count:
description: Number of packets to send.
type: int
default: 5
source:
description: Source interface or IP address.
type: str
ttl:
description: Time-to-live value.
type: int
size:
description: Packet size in bytes.
type: int
interval:
description: Interval between pings in seconds.
type: int
state:
description: C(present) = expect success; C(absent) = expect failure.
type: str
choices: [present, absent]
default: present
hostname:
type: str
required: true
port:
type: int
default: 443
api_key:
type: str
required: true
no_log: true
timeout:
type: int
default: 30
verify_ssl:
type: bool
default: false
"""
RETURN = r"""
commands:
description: Ping command tokens sent to the API.
returned: always
type: list
packet_loss:
description: Packet loss percentage string.
returned: always
type: str
packets_rx:
description: Packets received.
returned: always
type: int
packets_tx:
description: Packets transmitted.
returned: always
type: int
rtt:
description: RTT statistics dict (min/avg/max/mdev).
returned: when available
type: dict
"""
import re
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.vyos.rest.plugins.module_utils.vyos_rest import (
VYOS_REST_CONNECTION_ARGSPEC,
VyOSRestClient,
VyOSRestError,
)
def _build_ping_path(params):
path = ["ping", params["dest"]]
if params.get("count"):
path += ["count", str(params["count"])]
if params.get("source"):
path += ["interface", params["source"]]
if params.get("ttl"):
path += ["ttl", str(params["ttl"])]
if params.get("size"):
path += ["size", str(params["size"])]
return path
def _parse_ping_output(output):
stats = {"packet_loss": "100%", "packets_rx": 0, "packets_tx": 0}
tx_match = re.search(r"(\d+) packets transmitted", output)
rx_match = re.search(r"(\d+) received", output)
loss_match = re.search(r"(\d+(?:\.\d+)?%)\s+packet loss", output)
if tx_match:
stats["packets_tx"] = int(tx_match.group(1))
if rx_match:
stats["packets_rx"] = int(rx_match.group(1))
if loss_match:
stats["packet_loss"] = loss_match.group(1)
rtt_match = re.search(
r"rtt min/avg/max/mdev = ([\d.]+)/([\d.]+)/([\d.]+)/([\d.]+)",
output,
)
if rtt_match:
stats["rtt"] = {
"min": rtt_match.group(1),
"avg": rtt_match.group(2),
"max": rtt_match.group(3),
"mdev": rtt_match.group(4),
}
return stats
def main():
argument_spec = dict(
dest=dict(type="str", required=True),
count=dict(type="int", default=5),
source=dict(type="str"),
ttl=dict(type="int"),
size=dict(type="int"),
interval=dict(type="int"),
state=dict(type="str", default="present", choices=["present", "absent"]),
)
argument_spec.update(VYOS_REST_CONNECTION_ARGSPEC)
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
if module.check_mode:
module.exit_json(changed=False, commands=[], packet_loss="0%", packets_rx=0, packets_tx=0)
client = VyOSRestClient(module)
path = _build_ping_path(module.params)
try:
result = client.show(path)
except VyOSRestError as exc:
module.fail_json(msg=str(exc))
output = result.get("data", "")
stats = _parse_ping_output(output)
reachable = stats["packets_rx"] > 0
if module.params["state"] == "present" and not reachable:
module.fail_json(
msg="Ping to {dest} failed — no packets received.".format(dest=module.params["dest"]),
**stats,
)
elif module.params["state"] == "absent" and reachable:
module.fail_json(
msg="Expected {dest} to be unreachable but ping succeeded.".format(
dest=module.params["dest"],
),
**stats,
)
module.exit_json(changed=False, commands=path, **stats)
if __name__ == "__main__":
main()
|