Issue with lineinfile module #69739

16 views
Skip to first unread message

Amith sha

unread,
May 29, 2020, 1:14:35 AM5/29/20
to Ansible Project
SUMMARY

Tried to write a line using lineinfule module sequentially were observed that the random lines are missed to write on the file.


STEPS TO REPRODUCE
  serial: 100
  become: True
  gather_facts: False
  tasks:
    - name: Executing script
      script: scripts/getDisksData.py
      register: disk_data
      tags:
        - run_script
        - get_disk_data

    - set_fact: disk_data="{{inventory_hostname }} {{ disk_data.stdout_lines[0] }}"
      tags:
        - run_script
        - get_disk_data

    - name: Write Disks Data to file
      lineinfile: line={{ disk_data }} dest=working_dir/{{ app_id }}_instance_disk_data.txt insertafter=EOF state=present
      delegate_to: localhost
      tags:
        - run_script
        - get_disk_data
EXPECTED RESULTS

Passed 4 ips to the inventory so 4 entries should be made on the file.


ACTUAL RESULTS

Got 2 entries on the first attempt.
Got 4 entries on second attempt.

Vladimir Botka

unread,
May 29, 2020, 2:15:10 AM5/29/20
to Amith sha, ansible...@googlegroups.com
On Thu, 28 May 2020 22:14:35 -0700 (PDT)
Amith sha <amith...@gmail.com> wrote:

> Tried to write a line using lineinfule module sequentially were observed
> that the random lines are missed to write on the file.
>
> serial: 100
>
> - name: Write Disks Data to file
> lineinfile: ...
> delegate_to: localhost
>
> EXPECTED RESULTS
> Passed 4 ips to the inventory so 4 entries should be made on the file.
>
> ACTUAL RESULTS
> Got 2 entries on the first attempt.
> Got 4 entries on second attempt.


It's a "race". Yes. This might be viewed as Python bug. This simply does not
work in Python. See https://en.wikipedia.org/wiki/Race_condition

It's been discussed and tested. See
https://groups.google.com/forum/#!topic/ansible-project/3DvVtiynMRU

The remedy is simple. Use template and set "run_once: true". If you insist on
lineinfile use "throttle: 1" to serialize the writing to a file. The
inefficiency compared to template is obvious.
https://docs.ansible.com/ansible/latest/user_guide/playbooks_strategies.html#using-keywords-to-control-execution

IMHO, there is nothing Ansible can do about it. Except probably to mention
"race" when "throttle" is described in "Using keywords to control execution"

"This can be useful in restricting tasks that may be CPU-intensive or
interact with a rate-limiting API"

I've submitted PR
https://github.com/ansible/ansible/commit/7f79b5942abeb2593cb83021c2470d6da7bc992e

"This can be useful in restricting tasks that may be CPU-intensive,
interact with a rate-limiting API, or access a critical section"

https://github.com/ansible/ansible/issues/69739#issuecomment-635771688

HTH,

-vlado
Reply all
Reply to author
Forward
0 new messages