# How do I add a line to a file?

**URL:** <https://forum.ansible.com/t/how-do-i-add-a-line-to-a-file/13806>\
**Category:** Ansible Project\
**Created:** [October 30, 2013, 4:33pm UTC](https://forum.ansible.com/t/how-do-i-add-a-line-to-a-file/13806 "2013-10-30T16:33:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![klenwell](https://avatars.discourse-cdn.com/v4/letter/k/bbe5ce/32.png) [@klenwell](https://forum.ansible.com/u/klenwell)\
**Post date:** [October 30, 2013, 4:33pm UTC](https://forum.ansible.com/t/how-do-i-add-a-line-to-a-file/13806/1 "2013-10-30T16:33:22Z")

</div>

I would expect this to be pretty simple. I’m using the lineinfile module like so:

- name: Update bashrc for PythonBrew for foo user  
lineinfile:  
dest=/home/foo/.bashrc  
backup=yes  
line=“[[-s ${pythonbrew.bashrc\_path}]] && source ${pythonbrew.bashrc\_path}”  
owner=foo  
regexp=‘^’  
state=present  
insertafter=EOF  
create=True

The problem I’m having is that it’s replacing the last line in the file (which is “fi”) with my new line rather than appending the line.

Do I have the parameters correct? I’ve tried setting regexp to both ‘^’ and ‘’ (blank). Is there another way to go about this?

On Ansible 1.3.3.

---

<div class="post-metadata">

**Author:** ![klenwell](https://avatars.discourse-cdn.com/v4/letter/k/bbe5ce/32.png) [@klenwell](https://forum.ansible.com/u/klenwell)\
**Post date:** [October 30, 2013, 4:46pm UTC](https://forum.ansible.com/t/how-do-i-add-a-line-to-a-file/13806/2 "2013-10-30T16:46:38Z")

</div>

I’ve added the question to Stack Overflow:

[http://stackoverflow.com/q/19688885/1093087](http://stackoverflow.com/q/19688885/1093087)

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [October 30, 2013, 7:27pm UTC](https://forum.ansible.com/t/how-do-i-add-a-line-to-a-file/13806/3 "2013-10-30T19:27:33Z")

</div>

IIRC, when the regexp match nothing (e.g: regexp=‘THAT STRING WILL NOT BE FOUND’)

then the line will be appended after the lat one

---

<div class="post-metadata">

**Author:** ![klenwell](https://avatars.discourse-cdn.com/v4/letter/k/bbe5ce/32.png) [@klenwell](https://forum.ansible.com/u/klenwell)\
**Post date:** [October 30, 2013, 8:26pm UTC](https://forum.ansible.com/t/how-do-i-add-a-line-to-a-file/13806/4 "2013-10-30T20:26:21Z")

</div>

That did it. Thanks!

That seems like a real anti-pattern for what I imagine would be a pretty common use case.

---

<div class="post-metadata">

**Author:** ![klenwell](https://avatars.discourse-cdn.com/v4/letter/k/bbe5ce/32.png) [@klenwell](https://forum.ansible.com/u/klenwell)\
**Post date:** [October 30, 2013, 9:25pm UTC](https://forum.ansible.com/t/how-do-i-add-a-line-to-a-file/13806/5 "2013-10-30T21:25:47Z")

</div>

Actually, it finally struck me what the regexp should really be since I only want one copy of the line in the file:

- name: Update bashrc for PythonBrew for foo user  
lineinfile:  
dest=/home/foo/.bashrc  
line=‘[[-s ${pythonbrew.bashrc\_path}]] && source ${pythonbrew.bashrc\_path}’  
regexp=‘^[[ -s’  
state=present  
insertafter=EOF  
create=True

I’d like to match the whole line in the regexp param, but with the variable expansion, not sure how it would be done. In this case, the truncated regexp should be sufficient. Hopefully.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 31, 2013, 1:45am UTC](https://forum.ansible.com/t/how-do-i-add-a-line-to-a-file/13806/6 "2013-10-31T01:45:03Z")

</div>

FYI, I wouldn’t recommend asking Ansible questions on SO.

It divides the community and most folks are here – mainly I just don’t like SO’s policy on closing questions and not wanting to have discourse/conversation. I understand why they do it, but just don’t personally prefer it.

Tim and company do read SO, but you’ll get an answer here a lot faster.

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [October 31, 2013, 11:59am UTC](https://forum.ansible.com/t/how-do-i-add-a-line-to-a-file/13806/7 "2013-10-31T11:59:24Z")

</div>

Yes,

adding as the regexp one matching the line - or just part of it- to be inserted is much better.  
I have such cases too but answered too fast …

the variables {{ variable }} will be expanded in the rexgep string  
but depending on their contents they might need quoted. For example to avoid the ‘.’ being interpreted by the regexp engine
