Access to the job id variable from running tasks?

I’m working on a play where I use the template module to put a shell script on the remote filesystem and then run that script. However, it’s inherently single-threaded because a) it’s a delegated task to a single database server for all hosts and b) the name of the file is the same - so it creates a race condition.

One option is for me to generate a random UUID and append the filename with that to force unique filenames (and thus allow me to run each script in parallel on the single remote endpoint), but I figured the job id might be a good option as well. However, as I was reading it looks like the job id is the same for all endpoints running the same task in a play - which means that’s not a good option. Additionally, I don’t see any documentation about how to get the job id back as part of the action (i.e. as a variable).

Any recommendations here for something more “clever” than just creating text to append to the filename using something like originating hostname + epoch or something like that?

delegate_to in Ansible does not make things run in a single process, it’s still parallel.

you can use the serial: 1 keyword, however, to make things work on just one thing at a time if you need things to happen that way.

If you want a unique filename for each individual server you could perhaps use “{{inventory_hostname}}” in the filename, which is the name of the host you are looping over.

Yep, that’s what I’m doing right now for the database side, the serial: 1 works really well. The inventory_hostname was my next stop but I figured it’d still be safer to find something with more granularity, like the job id. But this will work too. Thanks.