# Systemd<no value>
// <!-- Required for asciidoctor -->
:toc:
// Set toclevels to be at least your hugo [markup.tableOfContents.endLevel] config key
:toclevels: 4

[cols="1^,1^,1^",options=header]
|===
| source | condition | target
| &#10004; | &#10004; | &#10004;
|===

== Description

The `systemd` resource reads and writes a single option inside a section of a systemd unit file, addressed by section name and option name rather than by line:

[source,ini]
----
[Service]
ExecStart=/usr/bin/myapp --version=1.2.3
Restart=always
----

Here `section: Service` and `option: ExecStart` address the first line.

**source**::
Returns the option's value.

**condition**::
Checks that the option holds `value`, or the source output when `value` is unset.

**target**::
Updates the option.

== Parameters

{{< resourceparameters "sources" "systemd" >}}

`file`, `section` and `option` are all mandatory - each missing one is reported as `the attribute 'spec.<name>' is required.`. `file` takes an absolute or relative path.

`index`:: systemd allows the same option to appear several times in a section, and `index` picks one, counting from zero. A negative value is rejected with `the attribute 'spec.index' must be greater than or equal to 0.`
+
Its default differs by stage: a **source** reads the first occurrence, while a **condition** and a **target** with `index` unset match *every* occurrence. Set it explicitly when a unit file repeats an option (`ExecStartPre` and `Environment` commonly do) and you only mean one of them.

`value`:: Defaults to the source output.

== Limitations

[IMPORTANT]
====
The unit file is rewritten from its parsed form, so **all comments are dropped** when a target changes something. If you need them preserved, drive the edit with the link:/docs/plugins/resource/file/["File" resource] and a `matchpattern`/`replacepattern` pair, or say so on link:https://github.com/updatecli/updatecli/issues[the issue tracker].
====

This resource edits unit files as text. It does not reload the daemon or restart units (no `systemctl daemon-reload` is issued, so a change applied by Updatecli takes effect only once something else reloads systemd).

== Example

[source,yaml]
----
# updatecli.yaml
{{<include "assets/code_example/docs/plugins/resources/systemd/updatecli.d/default.yaml">}}
----

== Links

* link:https://www.freedesktop.org/software/systemd/man/latest/systemd.unit.html[systemd.unit(5)] - unit file sections and options
