# best way to add multiple lines to a file.

**URL:** <https://forum.ansible.com/t/best-way-to-add-multiple-lines-to-a-file/16062>\
**Category:** Ansible Project\
**Created:** [August 28, 2014, 4:36pm UTC](https://forum.ansible.com/t/best-way-to-add-multiple-lines-to-a-file/16062 "2014-08-28T16:36:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darup\_tek](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@Darup\_tek](https://forum.ansible.com/u/Darup_tek)\
**Post date:** [August 28, 2014, 4:36pm UTC](https://forum.ansible.com/t/best-way-to-add-multiple-lines-to-a-file/16062/1 "2014-08-28T16:36:37Z")

</div>

I would like to add these lines to a file at the end sysctl.conf

net.ipv6.conf.all.disable\_ipv6=1

net.ipv4.tcp\_no\_metrics\_save = 1

net.ipv4.tcp\_moderate\_rcvbuf = 1

net.ipv4.tcp\_fin\_timeout = 15

net.ipv4.tcp\_keepalive\_time = 1800

net.ipv4.tcp\_keepalive\_probes = 4

I am adding these using several lineinfile:

What is the best way to knock it down with one module

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [August 28, 2014, 4:38pm UTC](https://forum.ansible.com/t/best-way-to-add-multiple-lines-to-a-file/16062/2 "2014-08-28T16:38:34Z")

</div>

i would use sysctl module + with\_items​

---

<div class="post-metadata">

**Author:** ![Darup\_tek](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@Darup\_tek](https://forum.ansible.com/u/Darup_tek)\
**Post date:** [August 28, 2014, 4:52pm UTC](https://forum.ansible.com/t/best-way-to-add-multiple-lines-to-a-file/16062/3 "2014-08-28T16:52:22Z")

</div>

I am currently doing this, want a better one 🙂

- name: Changes to sysctl.conf  
lineinfile:  
dest=/tmp/1  
regexp=“{{ item.regexp }}”  
line=“{{ item.line }}”  
with\_items:

- { regexp: ‘^net.ipv6.conf.all.disable\_ipv6’, line: ‘net.ipv6.conf.all.disable\_ipv6 = 1’ }

- { regexp: ‘^net.ipv4.tcp\_no\_metrics\_save’, line: ‘net.ipv4.tcp\_no\_metrics\_save = 1’ }

- { regexp: ‘^net.ipv4.tcp\_moderate\_rcvbuf’, line: ‘net.ipv4.tcp\_moderate\_rcvbuf = 1’ }

- { regexp: ‘^net.ipv4.tcp\_fin\_timeout’, line: ‘net.ipv4.tcp\_fin\_timeout = 15’ }

- { regexp: ‘^net.ipv4.tcp\_keepalive\_time’, line: ‘net.ipv4.tcp\_keepalive\_time = 1800’ }

- { regexp: ‘^net.ipv4.tcp\_keepalive\_probes’, line: ‘net.ipv4.tcp\_keepalive\_probes = 4’ }

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [August 28, 2014, 6:24pm UTC](https://forum.ansible.com/t/best-way-to-add-multiple-lines-to-a-file/16062/4 "2014-08-28T18:24:55Z")

</div>

Hí Darup,

i believe that Brian thougth sysctl module: [http://docs.ansible.com/sysctl\_module.html](http://docs.ansible.com/sysctl_module.html)

---

<div class="post-metadata">

**Author:** ![Takeshi\_Yaegashi](https://avatars.discourse-cdn.com/v4/letter/t/838e76/32.png) [@Takeshi\_Yaegashi](https://forum.ansible.com/u/Takeshi_Yaegashi)\
**Post date:** [August 29, 2014, 2:33am UTC](https://forum.ansible.com/t/best-way-to-add-multiple-lines-to-a-file/16062/5 "2014-08-29T02:33:57Z")

</div>

Hi there,

I agree it’s the best to use a specific module (sysctl\_module) for your specific task,  
but here’s the alternative I made: [https://galaxy.ansible.com/list#/roles/1475](https://galaxy.ansible.com/list#/roles/1475)

(the table rendering is broken, see [https://github.com/yaegashi/ansible-role-blockinfile](https://github.com/yaegashi/ansible-role-blockinfile) instead)

`

- hosts: all  
sudo: yes  
roles:
- yaegashi.blockinfile  
tasks:
- name: Update sysctl.conf  
blockinfile: |  
dest=/etc/sysctl.conf backup=yes  
content=‘net.ipv6.conf.all.disable\_ipv6=1  
net.ipv4.tcp\_no\_metrics\_save = 1  
net.ipv4.tcp\_moderate\_rcvbuf = 1  
net.ipv4.tcp\_fin\_timeout = 15  
net.ipv4.tcp\_keepalive\_time = 1800  
net.ipv4.tcp\_keepalive\_probes = 4’  
notify: Reload sysctl  
handlers:
- name: Reload sysctl.conf  
shell: sysctl -p

`

2014年8月29日金曜日 3時25分15秒 UTC+9 David Karban:

> Hí Darup,
> 
> i believe that Brian thougth sysctl module: [http://docs.ansible.com/sysctl\_module.html](http://docs.ansible.com/sysctl_module.html)
> 
> > I am currently doing this, want a better one 🙂
> > 
> > - name: Changes to sysctl.conf  
> > lineinfile:  
> > dest=/tmp/1  
> > regexp=“{{ item.regexp }}”  
> > line=“{{ item.line }}”  
> > with\_items:
> > 
> > - { regexp: ‘^net.ipv6.conf.all.disable\_ipv6’, line: ‘net.ipv6.conf.all.disable\_ipv6 = 1’ }
> > 
> > - { regexp: ‘^net.ipv4.tcp\_no\_metrics\_save’, line: ‘net.ipv4.tcp\_no\_metrics\_save = 1’ }
> > 
> > - { regexp: ‘^net.ipv4.tcp\_moderate\_rcvbuf’, line: ‘net.ipv4.tcp\_moderate\_rcvbuf = 1’ }
> > 
> > - { regexp: ‘^net.ipv4.tcp\_fin\_timeout’, line: ‘net.ipv4.tcp\_fin\_timeout = 15’ }
> > 
> > - { regexp: ‘^net.ipv4.tcp\_keepalive\_time’, line: ‘net.ipv4.tcp\_keepalive\_time = 1800’ }
> > 
> > - { regexp: ‘^net.ipv4.tcp\_keepalive\_probes’, line: ‘net.ipv4.tcp\_keepalive\_probes = 4’ }
> > 
> > –  
> > You received this message because you are subscribed to the Google Groups “Ansible Project” group.  
> > To unsubscribe from this group and stop receiving emails from it, send an email to [ansible-proje...@googlegroups.com](mailto:ansible-proje...@googlegroups.com).  
> > To post to this group, send email to [ansible...@googlegroups.com](mailto:ansible...@googlegroups.com).
> > 
> > To view this discussion on the web visit [https://groups.google.com/d/msgid/ansible-project/53caf444-54d6-4e4f-8031-347d1f7e4430%40googlegroups.com](https://groups.google.com/d/msgid/ansible-project/53caf444-54d6-4e4f-8031-347d1f7e4430%40googlegroups.com?utm_medium=email&utm_source=footer).
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> –
> 
> David Karban  
> Specialista na správu linuxových serverů  
> [www.karban.eu](http://www.karban.eu)

Regards,

---

<div class="post-metadata">

**Author:** ![Darup\_tek](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@Darup\_tek](https://forum.ansible.com/u/Darup_tek)\
**Post date:** [August 29, 2014, 4:55pm UTC](https://forum.ansible.com/t/best-way-to-add-multiple-lines-to-a-file/16062/6 "2014-08-29T16:55:56Z")

</div>

Thanks, I will try that.
