-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigip_delete_node_pools.py
202 lines (182 loc) · 6.47 KB
/
bigip_delete_node_pools.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/python
ANSIBLE_METADATA = {
'metadata_version': '3.0',
'status': ['development'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: bigip_delete_node_pools
short_description: "This module will delete nodes and delete any empty pools in the F5."
description:
- "This module will search through all the nodes based on a list of IPs ->
and delete the node membership then delete the node. With the delete_emptypools ->
flag set to true then the module will delete any empty pool in the F5.
options:
server:
description:
- What is the IP adddress of the F5
required: true
user:
description:
- Username to authenticate to the F5
required: true
password:
description:
- Password to authenticate to the F5
ip:
description:
- List of IPs in question to delete nodes against
Type:
- List
removeport:
description:
- Remove the trailing port
Type:
- Boolean
delete_emptypools:
description:
- Delete any empty pool on the F5 System
Type:
- Boolean
author:
- Marshall Brown ([email protected])
'''
EXAMPLES = '''
# Delete using just a single IP
- name: Try to delete from IP
bigip_delete_node_pools:
server: SERVER
user: USERNAME
password: PASSWORD
ip: 192.168.1.2
delegate_to: localhost
# Delete using list of IPs and remove trailing port as well as delete any empty F5 pool
- name: Try to delete from IP
bigip_delete_node_pools:
server: SERVER
user: USERNAME
password: PASSWORD
ip: lookup('dict', [192.168.2.1, 192.168.2.2, 192.168.2.3], wantlist=True)
remove_port: True
delete_emptypools: True
delegate_to: localhost
'''
RETURN = '''
original_message:
description: The original name param that was passed in
type: str
message:
description: The output message that the sample module generates
'''
from ansible.module_utils.basic import AnsibleModule
from ansibleutils import (
create_results,
return_results
)
import sys
sys.setrecursionlimit(5000)
try:
from f5.bigip import ManagementRoot
HAS_F5SDK = True
except ImportError:
HAS_F5SDK = False
class F5Node(object):
def __init__(self, params, results):
self.server = params['server']
self.username = params['user']
self.password = params['password']
self.ip = params['ip']
self.remove_port = params['remove_port']
self.result = results
self.pools_with_node = []
self.node_name = []
self.delete_emptypools = params['delete_emptypools']
mgmt = self.get_management_root_session()
self.f5_nodes = mgmt.tm.ltm.nodes.get_collection()
self.f5_pools = mgmt.tm.ltm.pools.get_collection()
def get_management_root_session(self):
return ManagementRoot(
self.server,
self.username,
self.password,
)
def deletenode(self):
self.result['node_names'] = []
for node in self.f5_nodes:
if node.address in self.ip:
self.result['node_names'].append(node.name)
#node.delete()
self.result['changed'] = True
self.result['success'] = True
else:
self.result['success'] = True
return self.result
def deleteemptypool(self):
self.result['deleted_pools'] = []
for pool in self.f5_pools:
if pool.members_s.items:
self.result['success'] = True
continue
else:
if self.remove_port == True:
poolname = pool.name
noportpool = poolname.rsplit('-', 1)[0]
self.result['deleted_pools'].append(noportpool)
else:
self.result['deleted_pools'].append(pool.name)
self.result['changed'] = True
self.result['success'] = True
#pool.delete()
return self.result
def deletemembership(self):
self.result['node_member'] = []
for pool_with_node in self.pools_with_node:
for node_member in pool_with_node.members_s.get_collection():
if node_member.name in self.node_name:
self.result['changed'] = True
self.result['success'] = True
self.result['node_member'].append(node_member.name)
#node_member.delete()
else: self.result['success'] = True
return self.result
def memberssearch(self):
for pool in self.f5_pools: # Lets get the pools and the nodes/members that are apart of the pools
for pool_member in pool.members_s.get_collection():
if pool_member.address in self.ip:
self.pools_with_node.append(pool)
self.node_name.append(pool_member.name)
return self.result
def main(self):
result = self.result
result = self.memberssearch()
result = self.deletemembership()
result = self.deletenode()
if self.delete_emptypools is True:
result = self.deleteemptypool()
return result
def _set_failed(result, msg):
result['msg'] = msg
result['success'] = False
return result
def main():
module = AnsibleModule(
argument_spec=dict(
server=dict(required=True, type='str'),
user=dict(required=True, type='str', aliases=['username']),
password=dict(required=True, type='str', no_log=True),
provider=dict(required=False, type='dict'),
ip=dict(required=True, type='str'),
remove_port=dict(required=False, type='bool', default='False'),
delete_emptypools=dict(required=False, type='bool', default='False')
),
supports_check_mode=False
)
if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required")
if not module.check_mode:
results = create_results(module.params)
results = F5Node(module.params, results).main()
return_results(module, results)
if __name__ == '__main__':
main()