From 17a40aa259c0b48fd1a441be1dd6f2a6987996d1 Mon Sep 17 00:00:00 2001 From: Matthew Gilliard Date: Wed, 1 Jul 2015 12:07:27 +0100 Subject: [PATCH] Handle race condition in directory creation. If we try to make a directory, but someone else creates the directory at the same time as us, we don't need to raise that error to the user. They asked for the directory to exist, and now it does. This fixes the race condition which was causing that error to be raised, and closes #1648. --- lib/ansible/modules/files/file.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/files/file.py b/lib/ansible/modules/files/file.py index 5f11f2dd583..a68308f6518 100644 --- a/lib/ansible/modules/files/file.py +++ b/lib/ansible/modules/files/file.py @@ -18,6 +18,7 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . +import errno import shutil import stat import grp @@ -280,7 +281,13 @@ def main(): if not os.path.isabs(path): curpath = curpath.lstrip('/') if not os.path.exists(curpath): - os.mkdir(curpath) + try: + os.mkdir(curpath) + except OSError, ex: + # Possibly something else created the dir since the os.path.exists + # check above. As long as it's a dir, we don't need to error out. + if not (ex.errno == errno.EEXISTS and os.isdir(curpath)): + raise tmp_file_args = file_args.copy() tmp_file_args['path']=curpath changed = module.set_fs_attributes_if_different(tmp_file_args, changed)