Repository: ansible/ansible-modules-core

Issue:
subversion: export fails if dest directory exists
Using the following syntax, the task always fails if /tmp/foo already exists.

`- name: (testing) svn export of desired callflow.xml
      subversion: repo=http://repo.local/repos/target/ dest=/tmp/foo/ export=yes`

The error shown is:
`msg: ERROR: /tmp/foo/ folder already exists, but its not a subversion repository.`

This appears to be happening because the subversion module does not account for exporting if the target directory exists (but is not a working copy):

`if not os.path.exists(dest):`
...export and checkout both handled
`elif os.path.exists("%s/.svn" % (dest, )):`
...checkout handled. export presumably not handled because this is a working directory
`else:
        module.fail_json(msg="ERROR: %s folder already exists, but its not a subversion repository." % (dest, ))`

I think if we insert another case before the `else` of `elif os.path.exists(dest) and export:` and handle export to a preexisting directory there, it should work.



Discussion:

Resolving patch:
diff --git a/source_control/subversion.py b/source_control/subversion.py
--- a/source_control/subversion.py
+++ b/source_control/subversion.py
@@ -123,7 +123,12 @@ def checkout(self):
 		
     def export(self, force=False):
         '''Export svn repo to directory'''
-        self._exec(["export", "-r", self.revision, self.repo, self.dest])
+        cmd = ["export"]
+        if force:
+            cmd.append("--force")
+        cmd.extend(["-r", self.revision, self.repo, self.dest])
+
+        self._exec(cmd)
 
     def switch(self):
         '''Change working directory's repo.'''
@@ -173,7 +178,7 @@ def main():
             dest=dict(required=True),
             repo=dict(required=True, aliases=['name', 'repository']),
             revision=dict(default='HEAD', aliases=['rev', 'version']),
-            force=dict(default='yes', type='bool'),
+            force=dict(default='no', type='bool'),
             username=dict(required=False),
             password=dict(required=False),
             executable=dict(default=None),
@@ -194,7 +199,7 @@ def main():
     os.environ['LANG'] = 'C'
     svn = Subversion(module, dest, repo, revision, username, password, svn_path)
 
-    if not os.path.exists(dest):
+    if export or not os.path.exists(dest):
         before = None
         local_mods = False
         if module.check_mode:
@@ -202,7 +207,7 @@ def main():
         if not export:
             svn.checkout()
         else:
-            svn.export()
+            svn.export(force=force)
     elif os.path.exists("%s/.svn" % (dest, )):
         # Order matters. Need to get local mods before switch to avoid false
         # positives. Need to switch before revert to ensure we are reverting to
@@ -222,9 +227,12 @@ def main():
     else:
         module.fail_json(msg="ERROR: %s folder already exists, but its not a subversion repository." % (dest, ))
 
-    after = svn.get_revision()
-    changed = before != after or local_mods
-    module.exit_json(changed=changed, before=before, after=after)
+    if export:
+        module.exit_json(changed=True)
+    else:
+        after = svn.get_revision()
+        changed = before != after or local_mods
+        module.exit_json(changed=changed, before=before, after=after)
 
 # import module snippets
 from ansible.module_utils.basic import *


Test patch: