Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_sp_vnic_order.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {'metadata_version': '1.0',
11 'status': ['preview'],
12 'supported_by': 'community'}
13
14 DOCUMENTATION = r'''
15 ---
16 module: ucs_sp_vnic_order
17
18 short_description: Configures vNIC order for service profiles and templates on Cisco UCS Manager
19
20 description:
21 - Configures Configures vNIC order for service profiles and templates on Cisco UCS Manager
22
23 options:
24 sp_name:
25 description: DN of the service profile
26 required: true
27 type: str
28 vnics:
29 description: List of vNIC order properties
30 type: list
31 elements: dict
32 required: true
33 suboptions:
34 name:
35 description: Name of the vNIC
36 required: true
37 type: str
38 admin_vcon:
39 description: Name of the virtual connection
40 choices: ["1","2","3","4","any"]
41 type: str
42 order:
43 description: vNIC connection order. Choices are 0-256 or unspecified.
44 type: str
45 transport:
46 description: transport medium
47 choices: ["ethernet", "fc"]
48 required: true
49 type: str
50 state:
51 description: Desired state of the vNIC.
52 choices: [present, absent]
53 default: present
54 type: str
55 org_dn:
56 description: root org dn
57 default: org-root
58 type: str
59 extends_documentation_fragment:
60 - cisco.ucs.ucs
61 requirements:
62 - ucsmsdk
63 author:
64 - Brett Johnson (@sdbrett)
65
66 '''
67
68 EXAMPLES = r'''
69 - name: Configure vnic order
70 cisco.ucs.ucs_sp_vnic_order:
71 sp_name: my_sp
72 vnics:
73 - name: 'my_vnic'
74 admin_vcon: '1'
75 order: '1'
76 transport: 'ethernet'
77 hostname: 192.168.99.100
78 username: admin
79 password: password
80 - name: Configure vhba order
81 cisco.ucs.ucs_sp_vnic_order:
82 sp_name: my_sp
83 vnics:
84 - name: 'my_vhba'
85 admin_vcon: '2'
86 order: '1'
87 transport: 'fc'
88 hostname: 192.168.99.100
89 username: admin
90 password: password
91 - name: Configure vnic and vhba order
92 cisco.ucs.ucs_sp_vnic_order:
93 sp_name: my_sp
94 vnics:
95 - name: my_vhba
96 admin_vcon: '2'
97 order: '1'
98 transport: fc
99 - name: my_vnic
100 admin_vcon: '1'
101 order: '1'
102 transport: ethernet
103 hostname: 192.168.99.100
104 username: admin
105 password: password
106 - name: Remove vnic order configuration from my_vnic
107 cisco.ucs.ucs_sp_vnic_order:
108 sp_name: my_sp
109 vnics:
110 - name: 'my_vnic'
111 transport: ethernet
112 state: absent
113 hostname: 192.168.99.100
114 username: admin
115 password: password
116
117 '''
118
119 RETURN = r'''
120 #
121 '''
122
123
124 from ansible.module_utils.basic import AnsibleModule
125 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
126
127
128 def get_service_profile(handle, org_dn, sp_name):
129 dn = org_dn + "/ls-" + sp_name
130 sp = handle.query_dn(dn)
131 return sp
132
133
134 def update_vnic_assignment_order(ucs, vnic, sp):
135 from ucsmsdk.mometa.ls.LsVConAssign import LsVConAssign
136
137 mo = LsVConAssign(parent_mo_or_dn=sp, admin_vcon=vnic['admin_vcon'],
138 order=vnic['order'], transport=vnic['transport'],
139 vnic_name=vnic['name'])
140 ucs.login_handle.add_mo(mo, True)
141 ucs.login_handle.commit()
142
143
144 def remove_vnic_assignment_order(ucs, vnic, sp):
145 from ucsmsdk.mometa.ls.LsVConAssign import LsVConAssign
146
147 mo = LsVConAssign(parent_mo_or_dn=sp, admin_vcon='any',
148 order='unspecified', transport=vnic['transport'],
149 vnic_name=vnic['name'])
150 ucs.login_handle.add_mo(mo, True)
151 ucs.login_handle.commit()
152
153
154 def get_vnic(ucs, dn):
155 return ucs.login_handle.query_dn(dn)
156
157
158 def get_vnic_dn(sp_dn, transport, name):
159 if transport == 'ethernet':
160 return sp_dn + '/ether-' + name
161 return sp_dn + '/fc-' + name
162
163
164 def matches_existing_vnic_order(vnic, vnic_mo):
165 if vnic['state'] == 'absent':
166 kwargs = dict(admin_vcon='any')
167 kwargs['order'] = 'unspecified'
168 else:
169 kwargs = dict(admin_vcon=vnic['admin_vcon'])
170 kwargs['order'] = vnic['order']
171
172 if vnic['transport'] == 'ethernet':
173 kwargs['type'] = 'ether'
174 else:
175 kwargs['type'] = vnic['transport']
176 return vnic_mo.check_prop_match(**kwargs)
177
178
179 def main():
180 vnic_spec = dict(
181 name=dict(type='str', required=True),
182 admin_vcon=dict(type='str', choices=['1', '2', '3', '4', 'any']),
183 order=dict(type='str'),
184 transport=dict(type='str', required=True, choices=['ethernet', 'fc']),
185 state=dict(type='str', default='present', choices=['present', 'absent']),
186 )
187 argument_spec = ucs_argument_spec.copy()
188 argument_spec.update(
189 sp_name=dict(required=True, type='str'),
190 vnics=dict(required=True, type='list', elements='dict', options=vnic_spec),
191 org_dn=dict(required=False, type='str', default='org-root'),
192 )
193 module = AnsibleModule(argument_spec,
194 supports_check_mode=True)
195 ucs = UCSModule(module)
196
197 err = False
198 changed = False
199
200 try:
201 sp_dn = dn = module.params['org_dn'] + "/ls-" + module.params['sp_name']
202 sp = ucs.login_handle.query_dn(dn)
203 if not sp:
204 raise ValueError("SP '%s' does not exist" % sp_dn)
205
206 for vnic in module.params['vnics']:
207 vnic_mo = get_vnic(ucs, (get_vnic_dn(sp_dn, vnic['transport'], vnic['name'])))
208
209 if vnic['state'] != 'absent' and not vnic_mo:
210 raise ValueError("vNIC '%s' is not assigned to service profile '%s'" % (vnic['name'], sp_dn))
211
212 if vnic_mo:
213 if not matches_existing_vnic_order(vnic, vnic_mo):
214 changed = True
215 break
216
217 if changed and not module.check_mode:
218 for vnic in module.params['vnics']:
219 vnic_mo = get_vnic(ucs, (get_vnic_dn(sp_dn, vnic['transport'], vnic['name'])))
220 if vnic['state'] == 'absent' and vnic_mo:
221 remove_vnic_assignment_order(ucs, vnic, sp)
222 elif not vnic_mo:
223
224 update_vnic_assignment_order(ucs, vnic, sp)
225 elif not matches_existing_vnic_order(vnic, vnic_mo):
226 update_vnic_assignment_order(ucs, vnic, sp)
227
228 except Exception as e:
229 err = True
230 ucs.result['msg'] = "setup error: %s " % str(e)
231
232 ucs.result['changed'] = changed
233 if err:
234 module.fail_json(**ucs.result)
235 module.exit_json(**ucs.result)
236
237
238 if __name__ == '__main__':
239 main()