forked from oravirt/ansible-oracle-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oracle_asmvol
executable file
·193 lines (154 loc) · 5.88 KB
/
oracle_asmvol
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: oracle_asmvol
short_description: Manage Oracle ASMCMD Volumes
description:
- Manage Oracle advm Volumes
version_added: "2.1.0.0"
options:
name:
description:
- The name of the volume
required: True
default: None
size:
description:
- The size of the volume
default: None
column:
description:
- Number of columns in a stripe set
required: False
width:
description:
- Stripe width of a volume
required: False
diskgroup:
description:
- The diskgroup in which to create the volume
required: True
default: None
aliases: ['dg']
state:
description:
- The state of the volume.
default: present
choices: ['present','absent', 'status']
oracle_sid:
description:
- The name of the ASM instance
default: +ASM
aliases: ['sid']
oracle_home:
description:
- The GI ORACLE_HOME
required: false
default: None
aliases: ['oh']
notes:
author: Mikael Sandström, [email protected], @oravirt
'''
EXAMPLES = '''
# Create an ASM volume
oracle_asmvol: name=acfsvol dg=acfsdg size=100G state=present oh=/u01/app/grid/12.1.0.2/grid sid='+ASM1'
# Delete an ASM volume
oracle_asmvol: name=acfsvol dg=acfsdg state=absent oh=/u01/app/grid/12.1.0.2/grid sid='+ASM1'
'''
import os
# Check if the volume exists
def check_vol_exists(module, msg, oracle_home, diskgroup, name):
command = '%s/bin/asmcmd volinfo -G %s %s' % (oracle_home, diskgroup, name)
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
msg[0] = 'Error, stdout: %s, stderr: %s, command is %s' % (stdout, stderr, command)
return False
if 'not found' in stdout:
return False
else:
return True
def create_vol(module, msg, oracle_home, diskgroup, column, width, redundancy, name, size):
command = '%s/bin/asmcmd volcreate %s -G %s -s %s ' % (oracle_home, name, diskgroup, size)
if column is not None:
command += ' --column %s' % (column)
if width is not None:
command += ' --width %s' % (width)
if redundancy is not None:
command += ' --redundancy %s' % (redundancy)
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
msg[0] = 'Error, stdout: %s, stderr: %s, command is %s' % (stdout, stderr, command)
return False
else:
return True #<-- all is well
def remove_vol(module, msg, oracle_home, diskgroup, name):
command = '%s/bin/asmcmd voldelete %s -G %s ' % (oracle_home, name, diskgroup)
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
msg[0] = 'Error, stdout: %s, stderr: %s, command is %s' % (stdout, stderr, command)
return False
else:
return True #<-- all is well
def main():
msg = ['']
module = AnsibleModule(
argument_spec = dict(
name = dict(required=True, aliases = ['volume_name']),
diskgroup = dict(required=True, aliases = ['dg']),
size = dict(required=False),
column = dict(default=None),
width = dict(default=None),
redundancy = dict(default=None),
state = dict(default="present", choices = ["present", "absent"]),
oracle_home = dict(default=None, aliases = ['oh']),
oracle_sid = dict(default='+ASM', aliases= ['sid']),
),
)
name = module.params["name"]
diskgroup = module.params["diskgroup"]
size = module.params["size"]
column = module.params["column"]
width = module.params["width"]
redundancy = module.params["redundancy"]
state = module.params["state"]
oracle_home = module.params["oracle_home"]
oracle_sid = module.params["oracle_sid"]
if oracle_home is not None:
os.environ['ORACLE_HOME'] = oracle_home
elif 'ORACLE_HOME' in os.environ:
oracle_home = os.environ['ORACLE_HOME']
else:
msg[0] = 'ORACLE_HOME variable not set. Please set it and re-run the command'
module.fail_json(msg=msg[0], changed=False)
if oracle_sid != '+ASM':
os.environ['ORACLE_SID'] = oracle_sid
elif 'ORACLE_SID' in os.environ:
oracle_sid = os.environ['ORACLE_SID']
if state == 'present' and not size:
msg[0] = 'Missing argument: size. Please add and re-run the command'
module.fail_json(msg=msg[0], changed=False)
if state == 'present':
if not check_vol_exists(module, msg, oracle_home, diskgroup, name):
if create_vol(module, msg, oracle_home, diskgroup, column, width, redundancy, name, size):
msg[0] = 'Volume %s successfully created' % (name)
module.exit_json(msg=msg[0], changed=True)
else:
module.fail_json(msg=msg[0], changed=False)
else:
msg[0] = 'Volume %s already exists' % (name)
module.exit_json(msgt=msg[0], changed=False)
elif state == 'absent' :
if check_vol_exists(module, msg, oracle_home, diskgroup, name):
if remove_vol(module, msg, oracle_home, diskgroup, name):
msg[0] = 'Volume %s successfully removed' % (name)
module.exit_json(msg=msg[0], changed=True)
else:
module.fail_json(msg=msg[0], changed=False)
else:
msg[0] = 'Volume %s doesn\'t exist' % (name)
module.exit_json(msg=msg[0], changed=False)
module.exit_json(msg="Unhandled exit", changed=False)
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()