-
Notifications
You must be signed in to change notification settings - Fork 23.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent module behavior when using connection_local to write to same file in parallel #40140
Comments
Files identified in the description: If these files are inaccurate, please update the |
-label needs_triage |
I was able to reproduce this with Ansible 2.5.4 too:
|
I can confirm that this behaviour persists in ansible 2.7.7
What I've noticed is that the lineinfile module sometimes seems to clobber the previous entries by somehow, and then you simply get only the latter entries in the final file. What's causing it is not obvious from verbose logs. |
I have traced this issue in the code of lineinfile.py and it seems to be caused by the way ansible modules are threaded and the timing of those threads. Specifically, when the if in line 245 is called I have tested this extensively, and an ad-hoc check with I have tried various things to see why it's being caused and possible workarounds within lineinfile.py with no success. I think one would need to dig into the way modules are threaded in ansible, and I would be way out of my depth there. I'll be glad to collaborate with anyone who wants to help resolve this. |
There is currently a lot of working going into adding file locking support in This issue is most likely caused by multiple processes on the same host interacting with the same file. The You can try testing with #52567 to see if that fixes your issue. |
Files identified in the description: If these files are incorrect, please update the |
I encountered this issue as well, my workaround was to perform the local_action lineinfile file writing in a different play, with serial set to 1. This way each line is written separately without interruptions. |
Issue still present in Ansible version 2.10.5 and reproducible using the same steps as in the original post. $ ansible --version
ansible 2.10.5
config file = None
configured module search path = ['/home/stefan/ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/stefan/.local/lib/python3.8/site-packages/ansible
executable location = /home/stefan/.local/bin/ansible
python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0] Also I think it may affect the blockinfile module as well (the last host will overwrite the contents of the file no matter what). Only fix I found so far is to just use the shell module and tasks:
- name: "log using echo"
connection: local
shell: "/bin/echo {{ inventory_hostname }} >> /tmp/ansible_test/bug_example.{{ number }}" |
I'm still able to produce this with the lineinfile module on Ansible Core 2.11.5. This seems to be a pretty significant issue that has been open for three years without being addressed. My use case is similar to what's described above. I have a task that looks for a file on a server, and if it exists writes the inventory hostname to a text file by delegating to localhost. The logs correctly show what should've been written to the file, but the count in the file is off significantly. This seems to work fine when setting serial to 1. |
It's a concurrency issue. Multiple processes writing to the same file at the same time, which is why |
In addition to @samdoran's suggestion, you can also use |
quick explanation: you are writing to the same file in parallel, so you get a race condition with several forks writing their own version of the same file (last one wins). Both |
Closing per above |
ISSUE TYPE
COMPONENT NAME
lineinfile module
ANSIBLE VERSION
CONFIGURATION
COLOR_VERBOSE(/home/username/.ansible.cfg) = cyan
DEFAULT_HASH_BEHAVIOUR(/home/username/.ansible.cfg) = merge
DEFAULT_HOST_LIST(/home/username/.ansible.cfg) = [u'/home/username/ansible/inventory']
DEFAULT_VAULT_PASSWORD_FILE(env: ANSIBLE_VAULT_PASSWORD_FILE) = /home/username/.ansible/vault_pass
DEPRECATION_WARNINGS(/home/username/.ansible.cfg) = False
OS / ENVIRONMENT
OpenBSD 6.3 amd64
SUMMARY
Files created or modified by lineinfile module are inconsistent when used as a local action, some new lines might be missing.
STEPS TO REPRODUCE
(as you can see deploytest4 is missing in the bug_example.1 file despite the fact that task run successfully for that host)
EXPECTED RESULTS
Generated results should be consistent on every run.
ACTUAL RESULTS
Some lines are missing from files.
The text was updated successfully, but these errors were encountered: