@@ -935,6 +935,19 @@ def _validate_path(path):
935
935
raise OperationTypeError ('`path` must be a string or `os.PathLike` object' )
936
936
937
937
938
+ def _raise_or_remove_invalid_path (fs_type , path , force , force_backup , force_backup_dir ):
939
+ if force :
940
+ if force_backup :
941
+ backup_path = '{0}.{1}' .format (path , get_timestamp ())
942
+ if force_backup_dir :
943
+ backup_path = '{0}/{1}' .format (force_backup_dir , backup_path )
944
+ yield 'mv {0} {1}' .format (path , backup_path )
945
+ else :
946
+ yield 'rm -rf {0}' .format (path )
947
+ else :
948
+ raise OperationError ('{0} exists and is not a {1}' .format (path , fs_type ))
949
+
950
+
938
951
@operation (pipeline_facts = {
939
952
'link' : 'path' ,
940
953
})
@@ -943,6 +956,7 @@ def link(
943
956
target = None , present = True , assume_present = False ,
944
957
user = None , group = None , symbolic = True ,
945
958
create_remote_dir = True ,
959
+ force = False , force_backup = True , force_backup_dir = None ,
946
960
state = None , host = None ,
947
961
):
948
962
'''
@@ -956,6 +970,9 @@ def link(
956
970
+ group: group to own the link
957
971
+ symbolic: whether to make a symbolic link (vs hard link)
958
972
+ create_remote_dir: create the remote directory if it doesn't exist
973
+ + force: if the target exists and is not a file, move or remove it and continue
974
+ + force_backup: set to ``False`` to remove any existing non-file when ``force=True``
975
+ + force_backup_dir: directory to move any backup to when ``force=True``
959
976
960
977
``create_remote_dir``:
961
978
If the remote directory does not exist it will be created using the same
@@ -1003,7 +1020,10 @@ def link(
1003
1020
1004
1021
# Not a link?
1005
1022
if info is False :
1006
- raise OperationError ('{0} exists and is not a link' .format (path ))
1023
+ yield _raise_or_remove_invalid_path (
1024
+ 'link' , path , force , force_backup , force_backup_dir ,
1025
+ )
1026
+ info = None
1007
1027
1008
1028
add_args = ['ln' ]
1009
1029
if symbolic :
@@ -1086,6 +1106,7 @@ def file(
1086
1106
present = True , assume_present = False ,
1087
1107
user = None , group = None , mode = None , touch = False ,
1088
1108
create_remote_dir = True ,
1109
+ force = False , force_backup = True , force_backup_dir = None ,
1089
1110
state = None , host = None ,
1090
1111
):
1091
1112
'''
@@ -1099,6 +1120,9 @@ def file(
1099
1120
+ mode: permissions of the files as an integer, eg: 755
1100
1121
+ touch: whether to touch the file
1101
1122
+ create_remote_dir: create the remote directory if it doesn't exist
1123
+ + force: if the target exists and is not a file, move or remove it and continue
1124
+ + force_backup: set to ``False`` to remove any existing non-file when ``force=True``
1125
+ + force_backup_dir: directory to move any backup to when ``force=True``
1102
1126
1103
1127
``create_remote_dir``:
1104
1128
If the remote directory does not exist it will be created using the same
@@ -1128,7 +1152,10 @@ def file(
1128
1152
1129
1153
# Not a file?!
1130
1154
if info is False :
1131
- raise OperationError ('{0} exists and is not a file' .format (path ))
1155
+ yield _raise_or_remove_invalid_path (
1156
+ 'file' , path , force , force_backup , force_backup_dir ,
1157
+ )
1158
+ info = None
1132
1159
1133
1160
# Doesn't exist & we want it
1134
1161
if not assume_present and info is None and present :
@@ -1195,6 +1222,7 @@ def directory(
1195
1222
path ,
1196
1223
present = True , assume_present = False ,
1197
1224
user = None , group = None , mode = None , recursive = False ,
1225
+ force = False , force_backup = True , force_backup_dir = None ,
1198
1226
_no_check_owner_mode = False ,
1199
1227
_no_fail_on_link = False ,
1200
1228
state = None , host = None ,
@@ -1209,6 +1237,9 @@ def directory(
1209
1237
+ group: group to own the folder
1210
1238
+ mode: permissions of the folder
1211
1239
+ recursive: recursively apply user/group/mode
1240
+ + force: if the target exists and is not a file, move or remove it and continue
1241
+ + force_backup: set to ``False`` to remove any existing non-file when ``force=True``
1242
+ + force_backup_dir: directory to move any backup to when ``force=True``
1212
1243
1213
1244
Examples:
1214
1245
@@ -1246,7 +1277,10 @@ def directory(
1246
1277
if _no_fail_on_link and host .get_fact (Link , path = path ):
1247
1278
host .noop ('directory {0} already exists (as a link)' .format (path ))
1248
1279
return
1249
- raise OperationError ('{0} exists and is not a directory' .format (path ))
1280
+ yield _raise_or_remove_invalid_path (
1281
+ 'directory' , path , force , force_backup , force_backup_dir ,
1282
+ )
1283
+ info = None
1250
1284
1251
1285
# Doesn't exist & we want it
1252
1286
if not assume_present and info is None and present :
0 commit comments