file ... directory only created on 2nd run

I have this TASK in a roles tasks/main.yml

it reliably (almost 100% of the occassions I’d say) fails to create the directory ‘data’ on the 1st run of the playbook.
Also with 100% reliabilty it succeeds with this on a 2nd run

  • name: create db dirs
    file:
    path: “{{ db_base }}/{{ item }}”
    state: directory
    owner: root
    become: yes
    loop:
  • data
  • backup

a missing backup dir does not cause an error directly following playbook TASKS, bust still it seems to me it gets created on the first round most of the times.

any idea what could be causing such a behaviour?

Depends on what the actual failure is, you didn’t provide any error details.

the file TASK itself messages sucessful execution

2022-01-21 10:45:26,850 p=1753880 u=gwagner n=ansible | TASK [resize_vgsys_mariadb : create mariadb ‘backup’ & ‘data’ dirs] *************************************************************************************************
2022-01-21 10:45:27,140 p=1753880 u=gwagner n=ansible | changed: [vm-51150-0198.intern] => (item=/opt/db/backup/mysql)
2022-01-21 10:45:27,383 p=1753880 u=gwagner n=ansible | changed: [vm-51150-0198.intern] => (item=/opt/db/data/mysql)

later on another TASK complains about the missing directory

TASK [mariadb_install : prepare install - cp my.cnf >> /opt/db/data/mysql/] *****************************************************************************************
failed: [vm-51150-0198.intern] (item=my.cnf) => changed=false
ansible_loop_var: item
item: my.cnf
msg: Destination directory /opt/db/data/mysql does not exist

Put a task just before that, with some debug for instance a directory listing so you can check if it’s really there or permission issues etc

Otherwise looks like a race condition in your playbook

The output you posted - with items “/opt/db/backup/mysql” and “/opt/db/data/mysql” - is not from the task you posted previously. That task had items “backup” and “data”.

Either you’ve inadvertently confused which steps are doing what, or you’ve left out relevant details in a kind but unsuccessful attempt to minimize your post for our benefit. If the latter, please, more detail is always better than more posts asking for additional detail.

It appears to me you’ve left out something, possibly relevant, between creation of /opt/db/{backup,data} and /opt/db/{backup,data}/mysql.