Skip to content

sudoers

A sudo rule in its own file in /etc/sudoers.d, checked with visudo before it’s written.

Use sudoers to give a user or group sudo rights to some commands, in a file of its own in /etc/sudoers.d. Conn3x checks the rule with visudo before it replaces anything, so a bad rule never stops sudo working for everyone.

Let the deploy group restart one service without a password:

step "deploy_restart" {
sudoers {
name = "deploy-restart"
group = "deploy"
commands = ["/usr/bin/systemctl restart app", "/usr/bin/systemctl status app"]
nopassword = true
}
}

Let alice run psql as postgres, with her password:

step "alice_psql" {
sudoers {
name = "alice-psql"
user = "alice"
runas = "postgres"
commands = ["/usr/bin/psql"]
}
}
Name Type Required Description
commands list of string Yes The commands, each an absolute path with any arguments, such as /usr/bin/systemctl restart app, or [“ALL”].
group string One of The group the rule is for. Set this or user.
host string No The host the rule applies on, or ALL (the default).
name string Yes The file’s name in /etc/sudoers.d: letters, digits, _ and -. sudo skips a name with a dot or ending in ~.
nopassword bool No Let the commands run without the user’s password. Defaults to false.
on_remove string No “forget” (default) leaves the rule in place, “delete” removes its file.
runas string No Who the commands run as: a user, user:group, or ALL (the default).
user string One of The user the rule is for. Set this or group.

Conn3x sets these; reference them as conn3x_sudoers.<circuit>__<step>["<host>"].<name>.

Name Type Description
in_sync bool Whether the file holds the rule, owned by root with mode 0440.
revision string Changes each time Conn3x changes this on the host. Handlers watch it.

A step lowers to the conn3x_sudoers resource, one instance per host, and connects with its circuit’s ssh block.

The file. Each step writes one file, /etc/sudoers.d/<name>, owned by root with mode 0440, holding one rule:

%deploy ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart app, /usr/bin/systemctl status app

Checking first. The new content goes to a temporary file, and visudo -cf checks it. If visudo rejects it, the apply fails and the old file stays as it was.

Reading. On every plan, Conn3x reads the file. in_sync is true when it holds exactly the rule Conn3x writes, owned by root with mode 0440. A hand edit shows as a change and is put back.

Commands. Each command starts with its absolute path, as sudo requires, followed by any arguments, or is "ALL". Conn3x escapes the characters sudoers reads specially in a command: ,, :, = and \.

Rules. name is letters, digits, _ and -. sudo skips a file in /etc/sudoers.d whose name holds a dot or ends in ~, so a name like app.conf would be a rule that silently does nothing. Set exactly one of user and group. runas is a user, user:group, or ALL, the default. nopassword defaults to false.

Changes. Changing anything but name rewrites the file. Changing name replaces the step, and on_remove decides what happens to the old file.

When you delete a sudoers step, on_remove decides what happens on the host. The default, "forget", leaves the rule in place, so the user or group keeps the right. Set "delete" to remove the file when the step goes.

conn3x convert ansible turns community.general.sudoers tasks with state: present into sudoers steps. Ansible’s nopassword defaults to true and this step’s to false, so a task that doesn’t set it gets nopassword = true written out. validation is dropped: Conn3x always checks with visudo. A task with setenv, noexec, or a sudoers_path other than /etc/sudoers.d stays a TODO.