best way to add multiple lines to a file.

4,638 views
Skip to first unread message

Darup tek

unread,
Aug 28, 2014, 12:36:37 PM8/28/14
to ansible...@googlegroups.com
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

Brian Coca

unread,
Aug 28, 2014, 12:38:37 PM8/28/14
to ansible...@googlegroups.com
i would use sysctl module + with_items​

Darup tek

unread,
Aug 28, 2014, 12:52:22 PM8/28/14
to ansible...@googlegroups.com
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' }


David Karban

unread,
Aug 28, 2014, 2:25:15 PM8/28/14
to ansible...@googlegroups.com
Hí Darup,

i believe that Brian thougth sysctl module: http://docs.ansible.com/sysctl_module.html





--
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.
To post to this group, send email to 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.

For more options, visit https://groups.google.com/d/optout.



--
David Karban
Specialista na správu linuxových serverů
www.karban.eu

Takeshi Yaegashi

unread,
Aug 28, 2014, 10:33:58 PM8/28/14
to ansible...@googlegroups.com
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
(the table rendering is broken, see 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:
Regards,
-- 
YAEGASHI Takeshi <yaeg...@debian.org

Darup tek

unread,
Aug 29, 2014, 12:55:56 PM8/29/14
to ansible...@googlegroups.com
Thanks, I will try that.
Reply all
Reply to author
Forward
0 new messages