# lineinfile ansible module skips a line

**URL:** <https://forum.ansible.com/t/lineinfile-ansible-module-skips-a-line/24352>\
**Category:** Ansible Project\
**Created:** [April 28, 2017, 3:45pm UTC](https://forum.ansible.com/t/lineinfile-ansible-module-skips-a-line/24352 "2017-04-28T15:45:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![madhan0618](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@madhan0618](https://forum.ansible.com/u/madhan0618)\
**Post date:** [April 28, 2017, 3:45pm UTC](https://forum.ansible.com/t/lineinfile-ansible-module-skips-a-line/24352/1 "2017-04-28T15:45:56Z")

</div>

I just posted my question on stackoverflow. Figured I will post here as well in hopes someone here can help.

[http://stackoverflow.com/questions/43683862/lineinfile-ansible-module-skips-a-line](http://stackoverflow.com/questions/43683862/lineinfile-ansible-module-skips-a-line)

**content:**

I have a need to know the index of host names in the inventory. I am using the below code to create a variable file that I can use in a subsequent play book

```auto
- name: Debug me
  hosts: hosts
  tasks:
      - debug: msg="{{ inventory_hostname }}"
      - debug: msg="{{ play_hosts.index(inventory_hostname) }}"
      - local_action: 'lineinfile create=yes dest=/tmp/test.conf
                   line="host{{ play_hosts.index(inventory_hostname) }}=
                   {{ inventory_hostname }}"'

```

I have the following inventory file

```auto

```

```auto
[hosts]
my.host1.com
my.host2.com

```

Now when I run this, the test.conf that gets generated under /tmp sometimes has both hostnames like this

```auto

```

```auto
host1= my.host2.com
host0= my.host1.com

```

when I run the same playbook a few times each time emptying the test.conf before running. quite a few times the file only has one entry

```auto

```

```auto
host1= my.host2.com

```

or

```auto
host0= my.host1.com

```

how come the same ansible playbook behaving differently? Please help. As you probably can already tell, I am new to ansible and trying from different examples that I have found online.
