Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
b97e2a6f
Commit
b97e2a6f
authored
Sep 04, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'backup-common' of
https://github.com/dhozac/ansible
into devel
parents
c69c3d6e
a5d63532
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
29 additions
and
22 deletions
+29
-22
lib/ansible/module_common.py
+14
-0
library/assemble
+4
-0
library/copy
+1
-18
library/lineinfile
+10
-4
No files found.
lib/ansible/module_common.py
View file @
b97e2a6f
...
...
@@ -47,6 +47,8 @@ import subprocess
import sys
import syslog
import types
import time
import shutil
try:
from hashlib import md5 as _md5
...
...
@@ -274,6 +276,18 @@ class AnsibleModule(object):
infile.close()
return digest.hexdigest()
def backuplocal(self, fn):
'''make a date-marked backup of the specified file, return True or False on success or failure'''
# backups named basename-YYYY-MM-DD@HH:MM~
ext = time.strftime("
%
Y-
%
m-
%
d@
%
H:
%
M~", time.localtime(time.time()))
backupdest = '
%
s.
%
s'
%
(fn, ext)
try:
shutil.copy2(fn, backupdest)
except shutil.Error, e:
self.fail_json(msg='Could not make backup of
%
s to
%
s:
%
s'
%
(fn, backupdest, e))
return backupdest
# == END DYNAMICALLY INSERTED CODE ===
...
...
library/assemble
View file @
b97e2a6f
...
...
@@ -50,6 +50,7 @@ def main():
argument_spec
=
dict
(
src
=
dict
(
required
=
True
),
dest
=
dict
(
required
=
True
),
backup
=
dict
(
default
=
False
,
choices
=
BOOLEANS
),
)
)
...
...
@@ -58,6 +59,7 @@ def main():
destmd5
=
None
src
=
os
.
path
.
expanduser
(
module
.
params
[
'src'
])
dest
=
os
.
path
.
expanduser
(
module
.
params
[
'dest'
])
backup
=
module
.
boolean
(
module
.
params
.
get
(
'backup'
,
False
))
if
not
os
.
path
.
exists
(
src
):
module
.
fail_json
(
msg
=
"Source (
%
s) does not exist"
%
src
)
...
...
@@ -72,6 +74,8 @@ def main():
destmd5
=
module
.
md5
(
dest
)
if
pathmd5
!=
destmd5
:
if
backup
and
destmd5
is
not
None
:
module
.
backuplocal
(
dest
)
shutil
.
copy
(
path
,
dest
)
changed
=
True
...
...
library/copy
View file @
b97e2a6f
...
...
@@ -20,19 +20,6 @@
import
os
import
shutil
import
time
def
backuplocal
(
fn
):
"""make a date-marked backup of the specified file, return True or False on success or failure"""
# backups named basename-YYYY-MM-DD@HH:MM~
ext
=
time
.
strftime
(
"
%
Y-
%
m-
%
d@
%
H:
%
M~"
,
time
.
localtime
(
time
.
time
()))
backupdest
=
'
%
s.
%
s'
%
(
fn
,
ext
)
try
:
shutil
.
copy2
(
fn
,
backupdest
)
except
shutil
.
Error
,
e
:
return
False
,
'Could not make backup of
%
s to
%
s:
%
s'
%
(
fn
,
backupdest
,
e
)
return
True
,
backupdest
def
main
():
...
...
@@ -76,11 +63,7 @@ def main():
try
:
if
backup
:
if
os
.
path
.
exists
(
dest
):
success
,
msg
=
backuplocal
(
dest
)
if
not
success
:
module
.
fail_jason
(
msg
=
msg
)
else
:
backup_file
=
msg
backup_file
=
module
.
backuplocal
(
dest
)
shutil
.
copyfile
(
src
,
dest
)
except
shutil
.
Error
:
module
.
fail_json
(
msg
=
"failed to copy:
%
s and
%
s are the same"
%
(
src
,
dest
))
...
...
library/lineinfile
View file @
b97e2a6f
...
...
@@ -21,7 +21,7 @@
import
re
import
os
def
present
(
module
,
name
,
regexp
,
line
,
insertafter
):
def
present
(
module
,
name
,
regexp
,
line
,
insertafter
,
backup
):
f
=
open
(
name
,
'rb'
)
lines
=
f
.
readlines
()
f
.
close
()
...
...
@@ -69,13 +69,15 @@ def present(module, name, regexp, line, insertafter):
changed
=
True
if
changed
:
if
backup
:
module
.
backuplocal
(
name
)
f
=
open
(
name
,
'wb'
)
f
.
writelines
(
lines
)
f
.
close
()
module
.
exit_json
(
changed
=
changed
,
msg
=
msg
)
def
absent
(
module
,
name
,
regexp
):
def
absent
(
module
,
name
,
regexp
,
backup
):
f
=
open
(
name
,
'rb'
)
lines
=
f
.
readlines
()
f
.
close
()
...
...
@@ -90,6 +92,8 @@ def absent(module, name, regexp):
lines
=
filter
(
matcher
,
lines
)
changed
=
len
(
found
)
>
0
if
changed
:
if
backup
:
module
.
backuplocal
(
name
)
f
=
open
(
name
,
'wb'
)
f
.
writelines
(
lines
)
f
.
close
()
...
...
@@ -103,18 +107,20 @@ def main():
regexp
=
dict
(
required
=
True
),
line
=
dict
(
aliases
=
[
'value'
]),
insertafter
=
dict
(
default
=
'EOF'
),
backup
=
dict
(
default
=
False
,
choices
=
BOOLEANS
),
),
)
params
=
module
.
params
backup
=
module
.
boolean
(
module
.
params
.
get
(
'backup'
,
False
))
if
params
[
'state'
]
==
'present'
:
if
'line'
not
in
params
:
module
.
fail_json
(
msg
=
'line= is required with state=present'
)
present
(
module
,
params
[
'name'
],
params
[
'regexp'
],
params
[
'line'
],
params
[
'insertafter'
])
params
[
'insertafter'
]
,
backup
)
else
:
absent
(
module
,
params
[
'name'
],
params
[
'regexp'
])
absent
(
module
,
params
[
'name'
],
params
[
'regexp'
]
,
backup
)
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment