Created
October 18, 2018 22:43
-
-
Save jamesbeedy/323c1febec09b7612e8f35ce36f878e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
ANSIBLE_METADATA = {'metadata_version': '1.1', | |
'status': ['preview'], | |
'supported_by': 'community'} | |
DOCUMENTATION = r''' | |
--- | |
module: linux_bridge | |
version_added: "2.4" | |
short_description: Manage Linux bridges | |
requirements: [ brctl ] | |
description: | |
- Manage Linux bridges | |
options: | |
bridge: | |
required: true | |
description: | |
- Name of bridge to manage | |
state: | |
required: false | |
default: "present" | |
choices: [ present, absent ] | |
description: | |
- Whether the bridge should exist | |
''' | |
EXAMPLES = r''' | |
- linux_bridge: bridge=br-int state=present | |
''' | |
RETURN = r''' | |
''' | |
from ansible.module_utils.basic import * | |
class LinuxBridge(object): | |
def __init__(self, module): | |
self.module = module | |
self.bridge = module.params['bridge'] | |
self.state = module.params['state'] | |
return | |
def brctl(self, cmd): | |
return self.module.run_command(['brctl'] + cmd) | |
def ifconfig(self, cmd): | |
return self.module.run_command(['ifconfig'] + cmd) | |
def br_exists(self): | |
syspath = "/sys/class/net/" + self.bridge | |
if os.path.exists(syspath): | |
return True | |
else: | |
return False | |
return | |
def addbr(self): | |
(rc, out, err) = self.brctl(['addbr', self.bridge]) | |
if rc != 0: | |
raise Exception(err) | |
self.ifconfig([self.bridge, 'up']) | |
return | |
def delbr(self): | |
self.ifconfig([self.bridge, 'down']) | |
(rc, out, err) = self.brctl(['delbr', self.bridge]) | |
if rc != 0: | |
raise Exception(err) | |
return | |
def check(self): | |
try: | |
if self.state == 'absent' and self.br_exists(): | |
changed = True | |
elif self.state == 'present' and not self.br_exists(): | |
changed = True | |
else: | |
changed = False | |
except Exception as e: | |
self.module.fail_json(msg = str(e)) | |
self.module.exit_json(changed = changed) | |
return | |
def run(self): | |
changed = False | |
try: | |
if self.state == 'absent' and self.br_exists(): | |
self.delbr() | |
changed = True | |
elif self.state == 'present' and not self.br_exists(): | |
self.addbr() | |
changed = True | |
except Exception as e: | |
self.module.fail_json (msg = str(e)) | |
self.module.exit_json (changed = changed) | |
return | |
def main(): | |
module = AnsibleModule ( | |
argument_spec = { | |
'bridge' : { 'required' : True }, | |
'state' : {'default' : 'present', | |
'choices' : ['present', 'absent'] | |
} | |
}, | |
supports_check_mode = True, | |
) | |
br = LinuxBridge(module) | |
if module.check_mode: | |
br.check() | |
else: | |
br.run() | |
return | |
if __name__ == '__main__': | |
main() | |
#!/usr/bin/python | |
#-*- coding: utf-8 -*- | |
DOCUMENTATION = ''' | |
module: linux_bridge_port | |
short_description: Manage Linux bridge ports | |
requirements: [ brctl ] | |
descroption: | |
- Manage Linux bridge ports | |
options: | |
bridge: | |
required: true | |
description: | |
- Name of bridge to manage | |
port: | |
requirement: true | |
description: | |
- Name of port to manage on the bridge | |
state: | |
required: false | |
default: "present" | |
choices: [ present, absent ] | |
description: | |
- Whether the port should exist | |
''' | |
EXAMPLES = ''' | |
# create port eth0 on bridge br-int | |
- linux_bridge_port: bridge=br-int port=eth0 state=present | |
''' | |
from ansible.module_utils.basic import * | |
class LinuxPort(object): | |
def __init__(self, module): | |
self.module = module | |
self.bridge = module.params['bridge'] | |
self.port = module.params['port'] | |
self.state = module.params['state'] | |
return | |
def brctl(self, cmd): | |
return self.module.run_command(['brctl'] + cmd) | |
def port_exists(self) : | |
syspath = "/sys/class/net/%s/brif/%s" % (self.bridge, self.port) | |
if os.path.exists(syspath): | |
return True | |
else: | |
return False | |
return | |
def addif(self): | |
(rc, out, err) = self.brctl(['addif', self.bridge, self.port]) | |
if rc != 0: | |
raise Exception(err) | |
return | |
def delif(self): | |
(rc, out, err) = self.brctl(['delif', self.bridge, self.port]) | |
if rc != 0: | |
raise Exception(err) | |
return | |
def check(self): | |
try: | |
if self.state == 'absent' and self.port_exists(): | |
changed = True | |
elif self.state == 'present' and not self.port_exists(): | |
changed = True | |
else: | |
changed = False | |
except Exception as e: | |
self.module.fail_json(msg = str(e)) | |
self.module.exit_json(changed = changed) | |
return | |
def run(self): | |
changed = False | |
try: | |
if self.state == 'absent': | |
if self.port_exists(): | |
self.delif() | |
changed = True | |
elif self.state == 'present': | |
if not self.port_exists(): | |
self.addif() | |
changed = True | |
except Exception as e: | |
self.module.fail_json(msg = str (e)) | |
self.module.exit_json(changed = changed) | |
return | |
def main(): | |
module = AnsibleModule ( | |
argument_spec = { | |
'bridge' : { 'required': True}, | |
'port' : {'required' : True}, | |
'state' : {'default' : 'present', | |
'choices' : ['present', 'absent']} | |
}, | |
supports_check_mode = True, | |
) | |
port = LinuxPort(module) | |
if module.check_mode: | |
port.check() | |
else: | |
port.run() | |
return | |
if __name__ == "__main__": | |
main() | |
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
DOCUMENTATION = ''' | |
module: vlan | |
short_description: Manage vlan | |
requirements: [ ip ] | |
description: | |
- Manage VLAN. vlan interface name is DEV.ID | |
options: | |
vlan: | |
required: true | |
description: | |
- vlan id | |
port: | |
required: true | |
description: | |
- interface for tagged vlan | |
state: | |
required: false | |
default: "present" | |
choices: [ present, absent ] | |
description: | |
- Whether the vlan should exist | |
''' | |
EXAMPLES = ''' | |
# Create vlan id 3000 interface on interface eth0 | |
- vlan: vlan=3000 port=eth0 | |
''' | |
from ansible.module_utils.basic import * | |
class Vlan(object): | |
def __init__(self, module): | |
self.module = module | |
self.vlan = module.params['vlan'] | |
self.port = module.params['port'] | |
self.state = module.params['state'] | |
self.vif = "%s.%s" % (self.port, self.vlan) | |
return | |
def ip(self, cmd): | |
return self.module.run_command(['ip'] + cmd) | |
def ifconfig(self, cmd): | |
return self.module.run_command(['ifconfig'] + cmd) | |
def vlan_exists(self): | |
syspath = "/sys/class/net/" + self.vif | |
return os.path.exists(syspath) | |
def addvlan(self): | |
cmd = ['link', 'add', 'link', self.port, 'name', self.vif, | |
'type', 'vlan', 'id', self.vlan ] | |
(rc, out, err) = self.ip(cmd) | |
if rc != 0: | |
raise Exception(err) | |
self.ifconfig([self.vif, 'up']) | |
return | |
def delvlan(self): | |
self.ifconfig([self.vif, 'down']) | |
cmd = ['link', 'del', 'dev', self.vif] | |
(rc, out, err) = self.ip(cmd) | |
if rc != 0: | |
raise Exception(err) | |
return | |
def check(self): | |
try: | |
if self.state == 'absent' and self.vlan_exists(): | |
changed = True | |
elif self.state == 'present' and not self.vlan_exists(): | |
changed = True | |
else: | |
changed = False | |
except: | |
self.module.fail_json(msg = str(e)) | |
self.module.exit_json(changed = changed) | |
return | |
def run(self): | |
changed = False | |
try: | |
if self.state == 'absent' and self.vlan_exists(): | |
self.delvlan() | |
changed = True | |
elif self.state == 'present' and not self.vlan_exists(): | |
self.addvlan() | |
changed = True | |
except Exception as e: | |
self.module.fail_json(msg = str(e)) | |
self.module.exit_json(changed = changed) | |
return | |
def main(): | |
module = AnsibleModule ( | |
argument_spec = { | |
'vlan' : { 'required' : True }, | |
'port' : { 'required' : True }, | |
'state' : {'default' : 'present', | |
'choices' : ['present', 'absent'] | |
} | |
}, | |
supports_check_mode = True, | |
) | |
vlan = Vlan(module) | |
if module.check_mode: | |
vlan.check() | |
else: | |
vlan.run() | |
return | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment