|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright: (c) 2022, Network to Code (@networktocode) <[email protected]> |
| 4 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 | +"""Ansible plugin definition for jdiff action plugin.""" |
| 6 | +from __future__ import (absolute_import, division, print_function) |
| 7 | + |
| 8 | +__metaclass__ = type |
| 9 | + |
| 10 | + |
| 11 | +DOCUMENTATION = """ |
| 12 | +--- |
| 13 | +module: jdiff |
| 14 | +short_description: Ansible module for jdiff. |
| 15 | +version_added: '1.1.0' |
| 16 | +description: |
| 17 | + - Ansible module wrapper on jdiff python library. |
| 18 | +requirements: |
| 19 | + - jdiff |
| 20 | +author: Patryk Szulczewski (@pszulczewski) |
| 21 | +options: |
| 22 | + check_type: |
| 23 | + description: |
| 24 | + - Check type supported by jdiff |
| 25 | + required: true |
| 26 | + type: str |
| 27 | + evaluate_args: |
| 28 | + description: |
| 29 | + - arguments for evaluate() method |
| 30 | + required: true |
| 31 | + type: dict |
| 32 | + jmespath: |
| 33 | + description: |
| 34 | + - JMESPath to extract specific values |
| 35 | + type: str |
| 36 | + default: "*" |
| 37 | + exclude: |
| 38 | + description: |
| 39 | + - list of keys to exclude |
| 40 | + type: list |
| 41 | + elements: str |
| 42 | +""" |
| 43 | + |
| 44 | +EXAMPLES = """ |
| 45 | +- name: "EXACT_MATCH - VALIDATE INTERFACE STATUS" |
| 46 | + networktocode.netauto.jdiff: |
| 47 | + check_type: "exact_match" |
| 48 | + evaluate_args: |
| 49 | + reference_data: "{{ ref_data }}" |
| 50 | + value_to_compare: "{{ data_to_compare }}" |
| 51 | + exclude: |
| 52 | + - interfaceStatistics |
| 53 | + register: result |
| 54 | + vars: |
| 55 | + ref_data: | |
| 56 | + { |
| 57 | + "Ethernet1": { |
| 58 | + "status": "up", |
| 59 | + "interfaceStatistics": { |
| 60 | + "inBitsRate": 3403.4362520883615, |
| 61 | + "inPktsRate": 3.7424095978179257, |
| 62 | + "outBitsRate": 16249.69114419833, |
| 63 | + "updateInterval": 300, |
| 64 | + "outPktsRate": 2.1111866059750692 |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + data_to_compare: | |
| 69 | + { |
| 70 | + "Ethernet1": { |
| 71 | + "status": "down", |
| 72 | + "interfaceStatistics": { |
| 73 | + "inBitsRate": 3413.4362520883615, |
| 74 | + "inPktsRate": 3.7624095978179257, |
| 75 | + "outBitsRate": 16259.69114419833, |
| 76 | + "updateInterval": 300, |
| 77 | + "outPktsRate": 2.1211866059750692 |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +
|
| 82 | +- name: "TOLERANCE - VALIDATE PREFIXES ARE WITH 10% TOLERANCE" |
| 83 | + networktocode.netauto.jdiff: |
| 84 | + check_type: "tolerance" |
| 85 | + evaluate_args: |
| 86 | + reference_data: "{{ ref_data }}" |
| 87 | + value_to_compare: "{{ data_to_compare }}" |
| 88 | + tolerance: 5 |
| 89 | + jmespath: "*.*.ipv4.[accepted_prefixes]" |
| 90 | + register: result |
| 91 | + vars: |
| 92 | + ref_data: | |
| 93 | + { |
| 94 | + "10.1.0.0": { |
| 95 | + "address_family": { |
| 96 | + "ipv4": { |
| 97 | + "accepted_prefixes": 100, |
| 98 | + "sent_prefixes": 1 |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + data_to_compare: | |
| 104 | + { |
| 105 | + "10.1.0.0": { |
| 106 | + "address_family": { |
| 107 | + "ipv4": { |
| 108 | + "accepted_prefixes": 90, |
| 109 | + "sent_prefixes": 0 |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +
|
| 115 | +- name: "PARAMETER - VALIDATE PEER TYPE" |
| 116 | + networktocode.netauto.jdiff: |
| 117 | + check_type: "parameter_match" |
| 118 | + evaluate_args: |
| 119 | + value_to_compare: "{{ data_to_compare }}" |
| 120 | + mode: match |
| 121 | + params: |
| 122 | + linkType: external |
| 123 | + jmespath: peers[*].[$ip$,linkType] |
| 124 | + register: result |
| 125 | + vars: |
| 126 | + data_to_compare: | |
| 127 | + { |
| 128 | + "peers": [ |
| 129 | + { |
| 130 | + "ip": "10.1.0.0", |
| 131 | + "linkType": "external" |
| 132 | + }, |
| 133 | + { |
| 134 | + "ip": "10.2.0.0", |
| 135 | + "linkType": "external" |
| 136 | + } |
| 137 | + ] |
| 138 | + } |
| 139 | +
|
| 140 | +- name: "REGEX - VALIDATE MAC FORMAT" |
| 141 | + networktocode.netauto.jdiff: |
| 142 | + check_type: "regex" |
| 143 | + evaluate_args: |
| 144 | + value_to_compare: "{{ data_to_compare }}" |
| 145 | + regex: "^([0-9a-fA-F]{2}(:|-)){5}([0-9a-fA-F]{2})$" |
| 146 | + mode: match |
| 147 | + jmespath: interfaces.*.[$name$,burnedInAddress] |
| 148 | + register: result |
| 149 | + vars: |
| 150 | + data_to_compare: | |
| 151 | + { |
| 152 | + "interfaces": { |
| 153 | + "Management1": { |
| 154 | + "burnedInAddress": "08:00:27:e6:b2:f8", |
| 155 | + "name": "Management1" |
| 156 | + }, |
| 157 | + "Management2": { |
| 158 | + "burnedInAddress": "08-00-27-e6-b2-f9", |
| 159 | + "name": "Management2" |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +
|
| 164 | +- name: "OPERATOR - VALIDATE RX LEVEL WITHIN RANGE" |
| 165 | + networktocode.netauto.jdiff: |
| 166 | + check_type: "operator" |
| 167 | + evaluate_args: |
| 168 | + value_to_compare: "{{ data_to_compare }}" |
| 169 | + params: |
| 170 | + params: |
| 171 | + mode: in-range |
| 172 | + operator_data: [-8, -2] |
| 173 | + jmespath: ports[*].[$name$,RxPower] |
| 174 | + register: result |
| 175 | + vars: |
| 176 | + data_to_compare: | |
| 177 | + { |
| 178 | + "ports": [ |
| 179 | + { |
| 180 | + "name": "1/1", |
| 181 | + "RxPower": -3.83, |
| 182 | + "state": "Connected" |
| 183 | + }, |
| 184 | + { |
| 185 | + "name": "1/2", |
| 186 | + "RxPower": -3.21, |
| 187 | + "state": "Connected" |
| 188 | + } |
| 189 | + ] |
| 190 | + } |
| 191 | +""" |
| 192 | + |
| 193 | +RETURN = """ |
| 194 | +changed: |
| 195 | + description: Indicates if change was made - always False. |
| 196 | + returned: success |
| 197 | + type: bool |
| 198 | +fail_details: |
| 199 | + description: output indicating where the check failed |
| 200 | + returned: success |
| 201 | + type: dict |
| 202 | +success: |
| 203 | + description: Indicates if the check was successful. |
| 204 | + returned: success |
| 205 | + type: bool |
| 206 | +""" |
| 207 | + |
| 208 | +from ansible.module_utils.basic import AnsibleModule |
| 209 | + |
| 210 | + |
| 211 | +def main(): |
| 212 | + """Module function.""" |
| 213 | + argument_spec = dict( |
| 214 | + check_type=dict(type='str', required=True), |
| 215 | + evaluate_args=dict(type='dict', required=True), |
| 216 | + jmespath=dict(type='str', default='*'), |
| 217 | + exclude=dict(type='list', elements='str', default=[]), |
| 218 | + ) |
| 219 | + |
| 220 | + AnsibleModule( |
| 221 | + argument_spec=argument_spec, |
| 222 | + required_together=[['check_type', 'evaluate_args']], |
| 223 | + supports_check_mode=True |
| 224 | + ) |
| 225 | + |
| 226 | + |
| 227 | +if __name__ == "__main__": # pragma: no cover |
| 228 | + main() |
0 commit comments