Concurrency Issue in a multi-node case with NFS drive mounted.

I have a multi-node setup where I am using ansible to perform certain action. There exist a NFS mounted directory which is shared between the nodes.

I am having unexpected behaviour when the ansible tries to modify the same file from different nodes.

For example: Node01, Node02, Node03

File resides in: /home/demouser/test.txt (/home/demouser is nfs mounted and shared across all the nodes.)

My ansible script is something like this: (Ansible Version 1.8.4)

  • hosts: all tasks:
  • name: executing shell command
  • shell: echo “test” >> /home/demouser/demotmp.txt | uniq > /home/demouser/demotmp_unique.txt | cp /tmp/demotmp_unique.txt /home/demouser/test.txt

Occassionally I get a scenario when the updated file is empty. My guess is, this happens as ansible tries to modify the file on all nodes at roughly the same time.

Is there a module which allows locking a file? Or is there a better way to do this?

Thanks

I would expect this, don't update the same file from multiple nodes,
use run_once: True on the task to have a single node do it, otherwise
you'll get this type of race condition.

modules do not lock as they normally execute on diff servers, also
locking in this case would be counter productive, you'll have multiple
processes on diff machines competing with each other and then
replacing the same file over and over.