|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import json |
| 4 | +from ansible.module_utils.basic import * |
| 5 | + |
| 6 | +DOCUMENTATION = ''' |
| 7 | +--- |
| 8 | +module: lag_facts |
| 9 | +Ansible_version_added: "2.0.0.2" |
| 10 | +Sonic_version: "2.0" |
| 11 | +short_description: Retrieve lag(LACP) information from a device |
| 12 | +description: |
| 13 | + - Retrieved facts will be inserted to: |
| 14 | + lag_facts: |
| 15 | + - 'names': [list all portchannel names] |
| 16 | + - 'lags': {portchannel: detailed portchannel information } |
| 17 | +''' |
| 18 | + |
| 19 | +EXAMPLES = ''' |
| 20 | +# Gather lab facts |
| 21 | + - name: Gather lag info |
| 22 | + lag_facts: host=10.255.0.200 |
| 23 | + - name: display lag information |
| 24 | + debug: var=lag_facts |
| 25 | +''' |
| 26 | + |
| 27 | +class LagModule(object): |
| 28 | + def __init__(self): |
| 29 | + self.module = AnsibleModule( |
| 30 | + argument_spec=dict( |
| 31 | + host=dict(required=True), |
| 32 | + ), |
| 33 | + supports_check_mode=False, |
| 34 | + ) |
| 35 | + self.lag_names = [] |
| 36 | + self.lags = {} |
| 37 | + return |
| 38 | + |
| 39 | + def run(self): |
| 40 | + ''' |
| 41 | + Main method of the class |
| 42 | + ''' |
| 43 | + self.get_po_names() |
| 44 | + for po in self.lag_names: |
| 45 | + self.lags[po] = {} |
| 46 | + self.lags[po]['po_stats'] = self.get_po_status(po) |
| 47 | + self.lags[po]['po_config'] = self.get_po_config(po) |
| 48 | + self.lags[po]['po_intf_stat'] = self.get_po_intf_stat(po) |
| 49 | + self.module.exit_json(ansible_facts={'lag_facts': {'names': self.lag_names, 'lags': self.lags}}) |
| 50 | + return |
| 51 | + |
| 52 | + def get_po_names(self): |
| 53 | + ''' |
| 54 | + Collect configured lag interface names |
| 55 | + ''' |
| 56 | + rt, out, err = self.module.run_command("sonic-cfggen -m /etc/sonic/minigraph.xml -v \"minigraph_portchannels.keys() | join(' ')\"") |
| 57 | + if rt != 0: |
| 58 | + fail_msg="Command to retrieve portchannel names failed return=%d, out=%s, err=%s" %(rt, out, err) |
| 59 | + self.module.fail_json(msg=fail_msg) |
| 60 | + else: |
| 61 | + self.lag_names = out.split() |
| 62 | + return |
| 63 | + |
| 64 | + def get_po_status(self, po_name): |
| 65 | + ''' |
| 66 | + Collect lag information by command docker teamdctl |
| 67 | + ''' |
| 68 | + rt, out, err = self.module.run_command("docker exec -i teamd teamdctl "+po_name+" state dump") |
| 69 | + if rt != 0: |
| 70 | + fail_msg="failed dump port channel %s status return=%d, out=%s, err=%s" %(po_name, rt, out, err) |
| 71 | + self.module.fail_json(msg=fail_msg) |
| 72 | + json_info = json.loads(out) |
| 73 | + return json_info |
| 74 | + |
| 75 | + def get_po_config(self, po_name): |
| 76 | + ''' |
| 77 | + Collect lag information by command docker teamdctl |
| 78 | + ''' |
| 79 | + rt, out, err = self.module.run_command("docker exec -i teamd teamdctl "+po_name+" config dump") |
| 80 | + if rt != 0: |
| 81 | + fail_msg="failed dump port channel %s config return=%d, out=%s, err=%s" %(po_name, rt, out, err) |
| 82 | + self.module.fail_json(msg=fail_msg) |
| 83 | + json_info = json.loads(out) |
| 84 | + return json_info |
| 85 | + |
| 86 | + def get_po_intf_stat(self, po_name): |
| 87 | + ''' |
| 88 | + Collect lag information by command docker teamdctl |
| 89 | + ''' |
| 90 | + rt, out, err = self.module.run_command("ip link show " + po_name) |
| 91 | + if rt != 0: |
| 92 | + fail_msg="failed show interface status of %s return=%d, out=%s, err=%s" %(po_name, rt, out, err) |
| 93 | + self.module.fail_json(msg=fail_msg) |
| 94 | + if 'NO-CARRIER' in out: |
| 95 | + return 'Down' |
| 96 | + else: |
| 97 | + return 'Up' |
| 98 | + |
| 99 | +def main(): |
| 100 | + lags = LagModule() |
| 101 | + lags.run() |
| 102 | + |
| 103 | +if __name__ == '__main__': |
| 104 | + main() |
0 commit comments