# Lineinfile task problem

**URL:** <https://forum.ansible.com/t/lineinfile-task-problem/7509>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [July 27, 2024, 8:13am UTC](https://forum.ansible.com/t/lineinfile-task-problem/7509 "2024-07-27T08:13:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marianna\_Buffa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/marianna_buffa/32/3008_2.png) [@Marianna\_Buffa](https://forum.ansible.com/u/Marianna_Buffa)\
**Post date:** [July 27, 2024, 8:13am UTC](https://forum.ansible.com/t/lineinfile-task-problem/7509/1 "2024-07-27T08:13:28Z")

</div>

Hello, I wrote a playbook to check if linux VM had new kernel downloaded and have to be rebooted to set them.  
So in the top of the tasks of the playbook I put the “needs restarting -r” command, and if it says yes, the following tasks execute the reboot of the VM. If it instead says no, the following tasks will not be executed because they have all a when condition on “if the VM has to be rebooted”.  
After this I have to know which VM of the inventory list has been rebooted, so I put a lineinfile task after the reboot task, for write in a file a hostname/ip for every VM it contacts at this point of the playbook. But when it write the hostname/IP of the VM in the file, it doesn’t work every time, infact some times, the list in the file reports some VM minus of the realm ones it rebooted.  
I think it is due to the contemporary attempt to write to the file, because the ssh connection to the VM are in parallel.  
I put the throttle option to 2 in the lineinfile task, but it seems not to work.  
Any suggestions?  
Thanks.

---

<div class="post-metadata">

**Author:** ![russoz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/russoz/32/127_2.png) [@russoz](https://forum.ansible.com/u/russoz)\
**Post date:** [July 27, 2024, 9:47am UTC](https://forum.ansible.com/t/lineinfile-task-problem/7509/2 "2024-07-27T09:47:54Z")

</div>

Hi @Marianna_Buffa  
Not sure I am visualizing your situation. Could you provide excerpts of your code?  
Cheers

---

<div class="post-metadata">

**Author:** ![Marianna\_Buffa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/marianna_buffa/32/3008_2.png) [@Marianna\_Buffa](https://forum.ansible.com/u/Marianna_Buffa)\
**Post date:** [July 27, 2024, 11:09am UTC](https://forum.ansible.com/t/lineinfile-task-problem/7509/3 "2024-07-27T11:09:06Z")

</div>

Hello, Here the tasks I’ve described before in my playbook.

- name: yum install pkg  
yum:  
name: yum-utils  
state: present

- name: needs-restarting -r  
shell: ‘needs-restarting -r’  
register: needs\_restarting\_output  
ignore\_errors: true

- name: mostra needs\_restarting\_output  
debug:  
var: needs\_restarting\_output.stdout

- name: check kernel word - la trova  
debug:  
msg: ‘kernel nuovo da installare’  
when: needs\_restarting\_output.stdout.find(‘kernel’) != -1 #se != -1 allora la trova

- name: cancella file kernel\_update.txt  
file:  
path: /var/lib/awx/projects/playbook/vm/kernel-update/kernel\_update.txt  
state: absent  
when: needs\_restarting\_output.stdout.find(‘kernel’) != -1 #se != -1 allora la trova  
delegate\_to: localhost  
ignore\_errors: true

- name: crea file kernel\_update.txt  
file:  
path: /var/lib/awx/projects/playbook/vm/kernel-update/kernel\_update.txt  
state: touch  
when: needs\_restarting\_output.stdout.find(‘kernel’) != -1 #se != -1 allora la trova  
delegate\_to: localhost

- name: attende creazione file /var/lib/awx/projects/playbook/vm/kernel-update/kernel\_update.txt  
wait\_for:  
path: /var/lib/awx/projects/playbook/vm/kernel-update/kernel\_update.txt  
state: present  
delay: 10  
timeout: 30  
when: needs\_restarting\_output.stdout.find(‘kernel’) != -1 #se != -1 allora la trova  
delegate\_to: localhost

- name: popola file kernel\_update.txt  
lineinfile:  
#line: ‘{{ host\_ip }} {{ ansible\_hostname }}’  
line: ‘{{ ansible\_default\_ipv4.address }} {{ ansible\_hostname }}’  
path: /var/lib/awx/projects/playbook/vm/kernel-update/kernel\_update.txt  
when: needs\_restarting\_output.stdout.find(‘kernel’) != -1 #se != -1 allora la trova  
delegate\_to: localhost

- name: reboot vm  
reboot:  
reboot\_timeout: 3600  
test\_command: w  
when: needs\_restarting\_output.stdout.find(‘kernel’) != -1 #se != -1 allora la trova

At the end I added a task to send me a mail with attached the file  
kernel\_update.txt

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [July 27, 2024, 3:29pm UTC](https://forum.ansible.com/t/lineinfile-task-problem/7509/4 "2024-07-27T15:29:45Z")

</div>

I think you may be having problems posting your code. Twice it looks like you are posting code, but I can see nothing. Can you check that you are posting correctly, preferably with the` preformatted text` format in the editor?

---

<div class="post-metadata">

**Author:** ![Marianna\_Buffa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/marianna_buffa/32/3008_2.png) [@Marianna\_Buffa](https://forum.ansible.com/u/Marianna_Buffa)\
**Post date:** [July 27, 2024, 3:50pm UTC](https://forum.ansible.com/t/lineinfile-task-problem/7509/5 "2024-07-27T15:50:44Z")

</div>

I update the post with the code. Sorry.

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [July 27, 2024, 10:23pm UTC](https://forum.ansible.com/t/lineinfile-task-problem/7509/6 "2024-07-27T22:23:51Z")

</div>

I suggest you don’t use line in file to create your report, but use a pattern with the assemble module: [ansible.builtin.assemble module – Assemble configuration files from fragments — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/assemble_module.html)

```yaml
- name: Create a directory for all the lines on the control node
  ansible.builtin.file:
    path: server_report
    state: directory
  delegate_to: localhost

- name: Render one file per line on the control node
  ansible.builtin.copy:
    dest: server_report/line_for_{{ inventory_hostname }}
    content: >
        "{{ ansible_default_ipv4.address }} {{ ansible_hostname }}"
  delegate_to: localhost
  when: needs_restarting_output.stdout.find(‘kernel’) != -1

- name: Assemble the pieces together 
  ansible.builtin.assemble:
    src: server_report
    dest: kernel_update.txt
  delegate_to: localhost
  run_once: true

```

---

<div class="post-metadata">

**Author:** ![binbashroot](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@binbashroot](https://forum.ansible.com/u/binbashroot)\
**Post date:** [July 28, 2024, 3:34pm UTC](https://forum.ansible.com/t/lineinfile-task-problem/7509/7 "2024-07-28T15:34:58Z")

</div>

I would take a bit of a different approach by using the return code as opposed to looking for a string/line and build an in memory inventory.

```auto
- name: Build dynamic inventory
  ansible.builtin.add_host:
    hostname: "{{ ansible_hostname }}"
    groups: reboot_group
  when: needs_restarting_output['rc'] | int == 1

- name: Create a list
  ansible.builtin.copy:
    content: |
       {% for host in groups['reboot_group'] %}
       {{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }} {{ hostvars['host']['ansible_facts']['hostname'] }}
       {% endfor %}
    dest: /tmp/destfile
  delegate_to: localhost
  run_once: true
  when: groups['reboot_group'] is defined

```

---

<div class="post-metadata">

**Author:** ![Marianna\_Buffa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/marianna_buffa/32/3008_2.png) [@Marianna\_Buffa](https://forum.ansible.com/u/Marianna_Buffa)\
**Post date:** [July 28, 2024, 7:46pm UTC](https://forum.ansible.com/t/lineinfile-task-problem/7509/8 "2024-07-28T19:46:48Z")

</div>

Thanks, both of them are great Solutions. I will try them And take you updated. Thanks again
