Skip to content

Commit 73e8cb0

Browse files
authored
Merge pull request #4 from kairoaraujo/core_3491f9e
aix_devices changed to receive attributes as dict
2 parents 40045ae + 7b2f0e4 commit 73e8cb0

1 file changed

Lines changed: 66 additions & 42 deletions

File tree

lib/ansible/modules/system/aix_devices.py

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33

4-
# Copyright: (c) 2017, Kairo Araujo <kairo@kairo.eti.br>
4+
# Copyright: (c) 2017, 2018 Kairo Araujo <kairo@kairo.eti.br>
55
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
66

77
from __future__ import absolute_import, division, print_function
@@ -22,14 +22,11 @@
2222
description:
2323
- This module discovers, defines, removes and modifies attributes of AIX
2424
devices.
25-
version_added: "2.5"
25+
version_added: "2.7"
2626
options:
2727
attributes:
2828
description:
2929
- Specifies attributes to device separated by comma.
30-
- If attribute has comma, explicit list using
31-
[ "parameter=value,with,comma", "parameter=foobar" ]. See alias
32-
example.
3330
device:
3431
description:
3532
- Device name.
@@ -63,19 +60,35 @@
6360
device: all
6461
state: present
6562
66-
- name: Scan new virtual devices.
63+
- name: Scan new virtual devices (vio0).
6764
aix_devices:
6865
device: vio0
6966
state: present
7067
71-
- name: Removes ent0.
68+
- name: Removing IP alias to en0
7269
aix_devices:
73-
device: ent0
70+
device: en0
71+
attributes:
72+
delalias4: 10.0.0.100,255.255.255.0
73+
74+
- name: Removes ent2.
75+
aix_devices:
76+
device: ent2
7477
state: absent
7578
76-
- name: Put device en0 in Defined
79+
- name: Put device en2 in Defined
7780
aix_devices:
78-
device: en0
81+
device: en2
82+
state: defined
83+
84+
- name: Removes ent4 (inexistent).
85+
aix_devices:
86+
device: ent4
87+
state: absent
88+
89+
- name: Put device en4 in Defined (inexistent)
90+
aix_devices:
91+
device: en4
7992
state: defined
8093
8194
- name: Put vscsi1 and children devices in Defined state.
@@ -93,21 +106,25 @@
93106
- name: Changes en1 mtu to 9000 and disables arp.
94107
aix_devices:
95108
device: en1
96-
attributes: mtu=900,arp=off
109+
attributes:
110+
mtu: 900
111+
arp: off
97112
state: present
98113
99114
- name: Configure IP, netmask and set en1 up.
100115
aix_devices:
101116
device: en1
102-
attributes: netaddr=192.168.0.100,netmask=255.255.255.0,state=up
117+
attributes:
118+
netaddr: 192.168.0.100
119+
netmask: 255.255.255.0
120+
state: up
103121
state: present
104122
105123
- name: Adding IP alias to en0
106124
aix_devices:
107125
device: en0
108-
attributes: [
109-
"alias4=10.0.0.100,255.255.255.0"
110-
]
126+
attributes:
127+
alias4: 10.0.0.100,255.255.255.0
111128
state: present
112129
'''
113130

@@ -116,12 +133,12 @@
116133
description: Return changed for aix_device actions as true or false.
117134
returned: always
118135
type: boolean
119-
version_added: "2.5"
136+
version_added: "2.7"
120137
msg:
121138
description: Return message regarding the action.
122139
returned: always
123140
type: string
124-
version_added: "2.5"
141+
version_added: "2.7"
125142
'''
126143

127144
from ansible.module_utils.basic import AnsibleModule
@@ -138,7 +155,7 @@ def _check_device(module, device):
138155
139156
"""
140157
lsdev_cmd = module.get_bin_path('lsdev', True)
141-
rc, lsdev_out, err = module.run_command("%s -C -l %s" % (lsdev_cmd, device))
158+
rc, lsdev_out, err = module.run_command(["%s" % lsdev_cmd, '-C', '-l', "%s" % device])
142159

143160
if rc != 0:
144161
module.fail_json(msg="Failed to run lsdev", rc=rc, err=err)
@@ -163,22 +180,22 @@ def _check_device_attr(module, device, attr):
163180
Returns:
164181
165182
"""
166-
lsattr_cmd = module.get_bin_path("lsattr", True)
167-
rc, lsattr_out, err = module.run_command("%s -El %s -a %s" % (lsattr_cmd, device, attr))
183+
lsattr_cmd = module.get_bin_path('lsattr', True)
184+
rc, lsattr_out, err = module.run_command(["%s" % lsattr_cmd, '-El', "%s" % device, '-a', "%s" % attr])
168185

169-
hide_attrs = ['delalias4', 'delalias6']
186+
hidden_attrs = ['delalias4', 'delalias6']
170187

171188
if rc == 255:
172189

173-
if attr in hide_attrs:
190+
if attr in hidden_attrs:
174191
current_param = ''
175192
else:
176193
current_param = None
177194

178195
return current_param
179196

180197
elif rc != 0:
181-
module.fail_json(msg="Failed to run lsattr.", rc=rc, err=err)
198+
module.fail_json(msg="Failed to run lsattr: %s" % err, rc=rc, err=err)
182199

183200
else:
184201
current_param = lsattr_out.split()[1]
@@ -199,7 +216,7 @@ def discover_device(module, device):
199216
changed = True
200217
msg = ''
201218
if not module.check_mode:
202-
rc, cfgmgr_out, err = module.run_command("%s %s" % (cfgmgr_cmd, device))
219+
rc, cfgmgr_out, err = module.run_command(["%s" % cfgmgr_cmd, "%s" % device])
203220
changed = True
204221
msg = cfgmgr_out
205222

@@ -208,50 +225,56 @@ def discover_device(module, device):
208225

209226
def change_device_attr(module, attributes, device, force):
210227
""" Change AIX device attribute. """
228+
211229
chdev_cmd = module.get_bin_path('chdev', True)
212230
attr_changed = []
213231
attr_not_changed = []
214232
attr_invalid = []
215233

216-
for attr in attributes:
217-
new_param = attr.split('=')[1]
218-
current_param = _check_device_attr(module, device, attr.split('=')[0])
234+
for attr in list(attributes.keys()):
235+
new_param = attributes[attr]
236+
current_param = _check_device_attr(module, device, attr)
219237

220238
if current_param is None:
221239
attr_invalid.append(attr)
222240

223241
elif current_param != new_param:
224242
if not module.check_mode:
225-
rc, chdev_out, err = module.run_command("%s -l %s %s -a %s" % (chdev_cmd, device, force, attr))
243+
if force:
244+
cmd = ["%s" % chdev_cmd, '-l', "%s" % device, '-a', "%s=%s" % (attr, attributes[attr]), "%s" % force]
245+
else:
246+
cmd = ["%s" % chdev_cmd, '-l', "%s" % device, '-a', "%s=%s" % (attr, attributes[attr])]
247+
248+
rc, chdev_out, err = module.run_command(cmd)
226249
if rc != 0:
227250
module.exit_json(msg="Failed to run chdev.", rc=rc, err=err)
228251
else:
229-
attr_changed.append(attr)
252+
attr_changed.append(attributes[attr])
230253
else:
231-
attr_not_changed.append(attr)
254+
attr_not_changed.append(attributes[attr])
232255

233256
changed = True
234257
msg = ''
235258
if not module.check_mode:
236259
if len(attr_changed) > 0:
237260
changed = True
238-
attr_changed_msg = 'Attributes changed: %s. ' % ','.join(attr_changed)
261+
attr_changed_msg = "Attributes changed: %s. " % ','.join(attr_changed)
239262
else:
240263
changed = False
241264
attr_changed_msg = ''
242265

243266
if len(attr_not_changed) > 0:
244-
attr_not_changed_msg = 'Attributes already set: %s. ' % ','.join(attr_not_changed)
267+
attr_not_changed_msg = "Attributes already set: %s. " % ','.join(attr_not_changed)
245268

246269
else:
247270
attr_not_changed_msg = ''
248271

249272
if len(attr_invalid) > 0:
250-
attr_invalid_msg = 'Invalid attributes: %s ' % ', '.join(attr_invalid)
273+
attr_invalid_msg = "Invalid attributes: %s " % ', '.join(attr_invalid)
251274
else:
252275
attr_invalid_msg = ''
253276

254-
msg = '%s%s%s' % (attr_changed_msg, attr_not_changed_msg, attr_invalid_msg)
277+
msg = "%s%s%s" % (attr_changed_msg, attr_not_changed_msg, attr_invalid_msg)
255278

256279
return changed, msg
257280

@@ -277,9 +300,15 @@ def remove_device(module, device, force, recursive, state):
277300
rmdev_cmd = module.get_bin_path('rmdev', True)
278301

279302
if not module.check_mode:
280-
rc, rmdev_out, err = module.run_command('%s -l %s %s %s' % (rmdev_cmd, device, state, recursive))
303+
if state:
304+
rc, rmdev_out, err = module.run_command(
305+
["%s" % rmdev_cmd, "-l", "%s" % device, "%s" % recursive, "%s" % state])
306+
else:
307+
rc, rmdev_out, err = module.run_command(
308+
["%s" % rmdev_cmd, "-l", "%s" % device, "%s" % recursive])
309+
281310
if rc != 0:
282-
module.fail_json(msg='Failed to run rmdev', rc=rc, err=err)
311+
module.fail_json(msg="Failed to run rmdev", rc=rc, err=err)
283312
else:
284313
changed = True
285314
msg = rmdev_out
@@ -291,7 +320,7 @@ def main():
291320

292321
module = AnsibleModule(
293322
argument_spec=dict(
294-
attributes=dict(type='list'),
323+
attributes=dict(type='dict'),
295324
device=dict(type='str'),
296325
force=dict(type='bool', default=False),
297326
recursive=dict(type='bool', default=False),
@@ -319,11 +348,6 @@ def main():
319348

320349
if state == 'present':
321350
if attributes:
322-
# validate attributes
323-
for attr in attributes:
324-
if len(attr.split('=')) < 2:
325-
module.fail_json(msg="attributes format is attribute_name=value.", err="Attribute in wrong format %s" % attr)
326-
327351
# change attributes on device
328352
device_status, device_state = _check_device(module, device)
329353
if device_status:

0 commit comments

Comments
 (0)