You are not logged in.

#1 2022-12-02 09:00:00

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,906
Website

[SOLVED] Tool to batch-edit systemd units

I have a use case where I need to edit a systemd unit on ~1700 Arch Linux systems.
Namely this one:

 $ cat /etc/systemd/network/terminals.netdev 
[NetDev]
Name = terminals
Kind = wireguard
Description = Terminal maintenance VPN.

[WireGuard]
...

I want to add MTUBytes = 1280 to the [NetDev] section, if the key MTUBytes does not yet exist in that section, so that it looks like:

[NetDev]
Name = terminals
Kind = wireguard
Description = Terminal maintenance VPN.
MTUBytes = 1280

[WireGuard]
...

afterwards.

Is there a command line tool that can manipulate that kind of INI-style systemd units or do I need to write one on my own?

Edit:
In case that was not clear: I cannot just sync the same file to all systems, since they differ in the [WireGuard] and [WireGuardPeer] sections, since all systems of course have different keys.

Last edited by schard (2022-12-02 14:43:33)

Offline

#2 2022-12-02 10:05:10

pitastrudl
Member
Registered: 2022-05-10
Posts: 8

Re: [SOLVED] Tool to batch-edit systemd units

If you can SSH to all these hosts you can use ansible. I would say it's the most fitting as you can then use it to further maintain these systems. Ansible offers some modules to do the kind of replacement you want, like https://docs.ansible.com/ansible/latest … odule.html.

Offline

#3 2022-12-02 11:57:38

Raynman
Member
Registered: 2011-10-22
Posts: 1,537

Re: [SOLVED] Tool to batch-edit systemd units

The only thing that comes to mind is a more general tool: Augeas. Checking it out further is on my TODO list, but it does seem to support INI files with a sparsely documented systemd lens on top of that.

Since Ansible was mentioned (although not by OP), there's https://github.com/paluh/ansible-augeas; SaltStack and Puppet support it without needing third-party modules. Salt also has a simple/limited INI module to avoid the regex hell of lineinfile.

Offline

#4 2022-12-02 14:10:01

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,342
Website

Re: [SOLVED] Tool to batch-edit systemd units

I'd just use sed.  Run the following on each system:

#!/bin/sh

sed -i '
/^\[NetDev\]$/ {
	:loop
	n
	/^MTUBytes/bend
	/^$/ {
		iMTUBytes = 1280
		bend
	}
	bloop
	:end
}' /etc/systemd/network/terminals.netdev

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2022-12-02 14:42:09

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,906
Website

Re: [SOLVED] Tool to batch-edit systemd units

Thanks for all your suggestions

@pitastrudl Yes, the systems are maintained via SSH. Ansible is not suitable for our changing systems setup. The deployment itself is not the issue.
@Trilby Thank you. I thought about using sed, but my sed-foo is not strong enough. I'll go with your solution.

Offline

Board footer

Powered by FluxBB