update hots file - the default inventory file

Can you provide output of lineinfile ? Mine look like

changed: [localhost] => (item=a) => {“ansible_loop_var”: “item”, “backup”: “”, “changed”: true, “item”: “a”, “msg”: “line added”}
changed: [localhost] => (item=b) => {“ansible_loop_var”: “item”, “backup”: “”, “changed”: true, “item”: “b”, “msg”: “line added”}

Thanks for the reply Abhi

What i noticed is it does update the file if there is no entry like blow is the o/p

TASK [Update the Inventory] *********************************************************************************************************************************************************
changed: [localhost] => (item=xx)

but if the entry already present in more than 1 group say … then it does not update the file & the output is

TASK [Update the Inventory] *********************************************************************************************************************************************************
ok: [localhost] => (item=xxx)

So if a fresh box it is ok ( ie never had been provisioned )

for eg

[group1]
server1
server2
[group2]
server1
server2

if you remove from group one & run does not update … So lineinfile should have someway to say the position exactly below [group1] add is missing …

Try "ini_file", e.g.
https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html

    - ini_file:
        dest: /etc/ansible/hosts
        section: MyServers
        option: "{{ item }}"
        allow_no_value: true
      loop: "{{ servers }}"

Thanks Vladimir Botka