Manage only a section of a config file with Puppet (multiline file_line)
Solution 1
You can simply use a single line match as the placeholder for a multi-line value:
$wlan_address = [...]
$wlan_netmask = [...]
$wlan_string = "allow-hotplug wlan0
iface wlan0 inet static
address ${wlan_address}
netmask ${wlan_netmask}"
file_line { 'wlan0':
path => '/etc/network/interfaces',
line => $wlan_string,
match => '^# PUPPET WLAN0$',
}
Solution 2
Managing part of a file is a configuration management anti pattern that should be discouraged as much as possible.
You have already identified the various tools that can do this.
- Augeas
- stdlib/file_line
- exec + sed
You can alternatively manage the whole file, and do an 'onlyif' so the file isn't overwritten every time.
As you have found, file_line only really works on init style config files, that are single line.
Your best bet will be to do an exec on that file with a sed insert.
Related videos on Youtube
Alex
Updated on September 18, 2022Comments
-
Alex over 1 year
There is a config file e.g. /etc/network/interfaces. I want to manage only a certain section of that file using Puppet.
Example:
# At the beginning at least is some dynamic content that can be different # from machine to machine and changed by an admin manually auto eth0 iface eth0 inet static address 192.168.108.5 netmask 255.255.255.0 # BEGIN PUPPET WLAN0 allow-hotplug wlan0 iface wlan0 inet static address 192.168.109.5 netmask 255.255.255.0 # END PUPPET WLAN0 # Potentially more stuff that I do not want to touch through Puppet # But could be static if it makes it more easy. E.g. simply force sections to end of file.
The stdlib file_line resource is very close to what I need. A regex and content to replace a match with. However, unfortunately, there is no way to make file_line match over multiple lines and replace with new content.
Alternative 1: create a bash script: ugly Exec resources and temp files.
Alternaitve 2: Use Augeas: seems overkill for a simple search & replace.
Is there another module than stdlib that allows me to do a regex match & replace over multiple lines?
Any other handcraft solutions better than juggling temp bash script and Exec resource?
-
Jeff Hewitt over 9 yearsIs the
#More stuf...
section static? -
Alex over 9 yearsIdeally, there could be any content. Like in the example above, there might be an additional interface, or not. Keeping it static is a possible constraint of course if it makes maintenance through puppet much easier.
-
Mikhail T. over 7 yearsYou may find this ticket worth watching...
-
-
Alex over 9 yearsOk, sure, but now we're not talking about "replacing a section" at all anymore. I could constrain it to "section must always be at the end". The idea here is though to allow some modifications in the file outside of the scope of Puppet (e.g. can be whatever and different on every machine). Sorry, I guess my previous comment on the question was not clear.