ansible copy module bash スクリプトの実行中上書き動作

bash スクリプトの実行中上書き動作について 2021

bash スクリプトの実行中上書き動作は ansible copy (template) module で再現しない。

本挙動が発生するかしないかは inode が変わるか変わらないがポイントとなっており、コピー前後の inode をデバッグ出力もしています。上のスクリーンショットのログは cp コマンドの場合のログで、inode が変わってないことが見て取れます。(スクリーンショットにはありませんが、copy module では inode が変化してました)
実行結果は GitHub Actions のログでも確認できますので、詳細はそちらを参照してください。

ちなみに copy module は remote_src: true でリモートマシン内でのファイルコピーができますが、その場合も本挙動は「再現しない」です。
なぜなら copy module の実装で remote_src の true/false によらず atomic_move を使用して新しいファイルに更新しているからです。atomic_move は os.rename を使ったファイルリネームで実装されており、この atomic_move を使っていれば copy module 以外でも本挙動は発生しないと考えてよいでしょう。

ブログズミ: bash スクリプトの実行中上書き動作が ansible copy module で再現するのか試してみた 2022 ★★★

inode が変わることで発生する問題もある。

In short, when Ansible replaces a file it creates a new inode and replaces the old file with that. Docker keeps the previous inode as the mountpoint which means that the content of the file in the container never gets updated.

https://github.com/ansible/ansible-modules-core/issues/4188