Creating directory using module file (state=directory).

Hi,

I have following playbook:

  • hosts: group1
    user: ansible_user
    gather_facts: no
    tasks:
  • name: Create backup directory
    file: path={{backup_app_logs_dir}} state=directory

The problem is that group1 consists of more than 1 hosts. So create-directory action is executed on all of boxes the same time. I suspect that module check first if directory exists. If so, skip creating, otherwise perform mkdir. When more than 1 host get information “Perfrom mkdir”, they do it. First of them makes directory successfully, but next one fails because the directory shouldn’t be there according to verficiation:

Traceback (most recent call last):
File “/tmp/ansible-tmp-1429781323.08-188650158471467/file”, line 1993, in ?
main()
File “/tmp/ansible-tmp-1429781323.08-188650158471467/file”, line 278, in main
os.mkdir(curpath)
OSError: [Errno 17] File exists: ‘/path/to/backup/directory’

I can of course skip this error and go forward by setting ignore_errors to True (as I described above - my directory is created) but when I do the same action on multile host_groups, first errors stops the task so next groups are not handled at all.

Do you see any easy solution not to split it into two different playbooks?

Regards,
W.

I'm guessing that the directory is in a shared drive across the hosts?
if so I would suggest adding run_once: True to your play.