2
2
#
3
3
# DRBD module for ansible
4
4
#
5
+ # Copyright (C) 2013, GC3, University of Zurich. All rights
6
+ # reserved.
7
+ #
8
+ # This program is free software; you can redistribute it and/or modify it
9
+ # under the terms of the GNU General Public License as published by the
10
+ # Free Software Foundation; either version 2 of the License, or (at your
11
+ # option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful, but
14
+ # WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
+ # General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License along
19
+ # with this program; if not, write to the Free Software Foundation, Inc.,
20
+ # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
+ #
22
+ #
5
23
# module: drdb
6
24
# short_description: This module will configure a drbd resource
7
25
# description:
89
107
# examples:
90
108
# - code: drbd name=myresource disk=/dev/sdb device=/dev/drbd1 address0=10.0.0.1:7789 address1=10.0.0.2:7789
91
109
92
- function missing_argument(){
110
+ function json_fail(){
111
+ msg=" $* "
93
112
cat << EOF
94
113
{
95
114
"failed" : true,
96
- "msg" : "Missing required arguments ' $1 ' "
115
+ "msg" : "$msg "
97
116
}
98
117
EOF
99
118
exit 1
100
119
}
101
120
102
- [ -f $1 ] && source $1 || eval $*
121
+ function json_changed(){
122
+ msg=" $* "
123
+ cat << EOF
124
+ {
125
+ "changed": true,
126
+ "failed": false,
127
+ "msg": "$msg "
128
+ }
129
+ EOF
130
+ }
131
+
132
+ function json_ok(){
133
+ msg=" $* "
134
+ cat << EOF
135
+ {
136
+ "changed": false,
137
+ "failed": false,
138
+ "msg": "$msg "
139
+ }
140
+ EOF
141
+ }
103
142
143
+ trap " json_fail 'We got killed!'" SIGINT SIGTERM
104
144
145
+ [ -f $1 ] && source $1 || eval $*
105
146
106
147
[ -z " $device0 " ] && device0=$device
107
148
[ -z " $device1 " ] && device1=$device
117
158
[ -z " $metadisk1 " ] && metadisk1=$metadisk
118
159
119
160
120
-
121
161
[ -z " $name " ] && missing_argument name
122
162
[ -z " $disk0 " ] && missing_argument disk0
123
163
[ -z " $disk1 " ] && missing_argument disk1
126
166
[ -z " $address0 " ] && missing_argument address0
127
167
[ -z " $address1 " ] && missing_argument address1
128
168
129
- tmp=$( tempfile )
169
+ tmp=$( mktemp )
130
170
cat > $tmp << EOF
131
171
resource $name {
132
172
on $peer0 {
@@ -147,31 +187,15 @@ EOF
147
187
cmp -s $tmp $dest
148
188
if [ $? -eq 0 ]
149
189
then
150
- cat << EOF
151
- {
152
- "changed": false,
153
- "ok": true
154
- }
155
- EOF
190
+ rm -rf $tmp
191
+ json_ok " File is already latest version"
156
192
else
157
193
cp -f $tmp $dest
194
+ if [ $? -ne 0 ]
195
+ then
196
+ rm -f $tmp
197
+ json_fail " Error creating DRBD configuration file $dest "
198
+ fi
158
199
rm -f $tmp
159
- cat << EOF
160
- {
161
- "ok": true,
162
- "changed": true,
163
- "failed": false
164
- }
165
- EOF
200
+ json_changed " File has been updated"
166
201
fi
167
-
168
- # - failed=True, indicating that the action failed
169
- # - skipped=True, indicating a pre-condition for running the task was already
170
- # met
171
- # - changed=True/False, indicating the task changed/didn't change something,
172
- # used to trigger notifiers and coloring of the output.
173
- # - ansible_facts=<dict>, containing custom facts that will be usable as
174
- # variables in following tasks
175
- # - stdout=<str>, containing text output. Will be split into lines that can be
176
- # used if register'd
177
- # - msg, used for e.g. failure messages
0 commit comments