How to add several lines to file with Puppet

I did not find any elegant way to add several lines to an existing file.

I noticed the Simple Text Patterns wiki page, but I have to add a block into /etc/network/interfaces.

Any clue?


Solution 1:

I agree that augeas is by far the best answer to what you are trying.

I would also recommend the file_line resource from the stdlib puppet library. It tends to be much more lightweight than augeas.

You would have to define one resource for each line, and if the order is important you can add dependencies between the invocations.

Sample usage:

        file_line { "no_ipv6_networking":
            path  => "/etc/sysconfig/network",
            line  => "NETWORKING_IPV6=no",
            match => "^NETWORKING_IPV6=",
        }

Solution 2:

You coud use the augeas type for this, but the ideal solution would be to use netcf since you want to manage network interfaces.

Some time ago, I have started a Puppet Netcf provider. It is still not ready for production, but if you have some Ruby skills, it's probably the best solution for your need (and PRs are most definitely welcome, too).

The Netcf provider takes an XML interface definition as input, for example:

netcf_if {"eth1":
  ensure     => up,
  definition => '
<interface type="ethernet" name="eth1">
  <start mode="onboot"/>
  <protocol family="ipv4">
    <ip address="192.168.0.5" prefix="24"/>
    <route gateway="192.168.0.1"/>
  </protocol>
</interface>
';
}

You can find more examples of XML definitions in the Netcf repository.