ashishpm
ashishpm

Reputation: 491

Ansible lineinfile module not adding all the required lines to the file

I am using lineinfile module in ansible to append all the hosts in inventory to a file. This is the ansible playbook code -

- hosts: test
  connection: ssh
  gather_facts: no
  tasks:

    - name: Create file to store hosts to be updated
      delegate_to: localhost
      run_once: true
      file:
        path: /tmp/updated_hosts.txt
        state: touch
        mode: u=rw,g=rw,o=rw

    - name: WRITE of updated hosts in play
      delegate_to: localhost
      lineinfile:
        path: /tmp/updated_hosts.txt
        line: "{{ inventory_hostname }},"
      when: some condition

This is my inventory

[test]
test1
test2
test3
test4
test5
test6
test7
test8
test9
test10
test11
test12
test13
test14
test15
test16
test17
test18
test19
test20
test21
test22
test23
test24
test25
test26
test27
test28
test29
test30

But after running the playbook

PLAY [test] ***********************************************************************************************************************************************************************************************

TASK [Create file to store hosts to be updated] ***********************************************************************************************************************************************************
changed: [test1 -> localhost]

TASK [WRITE of updated hosts in play] *********************************************************************************************************************************************************************
changed: [test2 -> localhost]
changed: [test1 -> localhost]
changed: [test4 -> localhost]
changed: [test5 -> localhost]
changed: [test3 -> localhost]
changed: [test7 -> localhost]
changed: [test10 -> localhost]
changed: [test6 -> localhost]
changed: [test8 -> localhost]
changed: [test9 -> localhost]
changed: [test11 -> localhost]
changed: [test12 -> localhost]
changed: [test13 -> localhost]
changed: [test14 -> localhost]
changed: [test15 -> localhost]
changed: [test16 -> localhost]
changed: [test18 -> localhost]
changed: [test20 -> localhost]
changed: [test19 -> localhost]
changed: [test17 -> localhost]
changed: [test21 -> localhost]
changed: [test24 -> localhost]
changed: [test22 -> localhost]
changed: [test23 -> localhost]
changed: [test25 -> localhost]
changed: [test26 -> localhost]
changed: [test29 -> localhost]
changed: [test27 -> localhost]
changed: [test28 -> localhost]
changed: [test30 -> localhost]

PLAY RECAP ************************************************************************************************************************************************************************************************
test1                      : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test10                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test11                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test12                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test13                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test14                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test15                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test16                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test17                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test18                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test19                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test2                      : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test20                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test21                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test22                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test23                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test24                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test25                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test26                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test27                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test28                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test29                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test3                      : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test30                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test4                      : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test5                      : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test6                      : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test7                      : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test8                      : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test9                      : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

when I check the content of /tmp/updated_hosts.txt this is what I find -

test1,
test5,
test3,
test8,
test9,
test14,
test17,
test22,
test25,
test30,

Why am I not able to see all hosts ? Even though I see that the task has been executed on all the hosts.

P.S. - the hosts in inventory test1, test2 ... are just for the purpose of question.

EDIT - I have edited the ansible code to only write the hostname to the file when it satisfies some condition

Upvotes: 0

Views: 981

Answers (3)

TRW
TRW

Reputation: 1034

I think, the underlying problem is, that you task with lineinfile is only running on localhost and you run 30 threads at the same time to change a single file on a single system.

Then of course, the lineinfile module on one thread overrides the already written data in another thread. Because the system isn't verifying its output you get random "race-condition-like" output. You could try this by adding a "serial: 1" to the playbook.

- hosts: test
  connection: ssh
  gather_facts: no
  serial: 1
  tasks:
    ...

Upvotes: 1

Karthikeyan
Karthikeyan

Reputation: 11

Try the following logic it's worked in my code:

  - name: tracking
    lineinfile:
      path: /path/file_name.txt
      line: "{{inventory_hostname}}"
      state: present
      insertafter: EOF
    register: check
    retries: 1
    until: check is not changed
    when: Some condition 
    delegate_to: localhost
    ignore_errors: yes

Upvotes: 1

KrishnaR
KrishnaR

Reputation: 430

Your issue could be caused by multiple hosts trying to update the file at the same time. There is chance of missing some lines. Try to loop over the hosts using the with_inventory_hostnames lookup

    - name: WRITE of updated hosts in play
      delegate_to: localhost
      run_once: true
      lineinfile:
        path: /tmp/updated_hosts.txt
        line: "{{ item }},"
        insertafter: EOF
      with_inventory_hostnames:
        - test

Upvotes: 1

Related Questions