notmycloud.systemd_unit
Ansible Systemd-单元
描述
生成和配置Systemd单元文件,既可以全局配置,也可以为持久用户配置。
使用方法
只需要systemd_units
字典。systemd_units
下的每个键代表一个用户名,root
配置在/etc/systemd/system
中的系统单元。每个用户名下,指定用户是否允许持久,默认为是。然后包括单元类型:unit
、timer
、mount
等……对于每种单元类型,包含单元的配置。
单元定义
config
键包含单元文件的INI样式配置。键名对单元文件选项是区分大小写的,这种方法允许您为单元文件定义任何部分和选项。选项可以定义为数组,以便在不相互覆盖的情况下多次输入。
此角色会覆盖现有单元文件!
单元选项
以下选项可在每个单元定义中设置:
- enabled: 将设置单元在启动时启用
- state: 任何有效的状态值,默认为started
- restart_on_change: 如果我们对单元定义进行任何修改,将会重启该单元,默认为是
- backup_before_write: 在写入我们的定义之前,会创建一个单独的.bak文件,默认为是
- filepath: 允许您覆盖默认的单元文件路径。默认是
/etc/systemd/system
用于root,或者~/.config/systemd/user
用于用户
---
systemd_units:
用户名: # root用户将在/etc/systemd/system处理中系统单元,其他用户将在~/.config/systemd/user处理中
enable_linger: # 默认为是
UNIT_EXTENSION: # service, target, socket等...
UNIT_NAME: # 单元文件名
config: # 包含单元文件的选项
SECTION: # Unit, Service, Install等...
OPTION: # 可以是OPTION=value或OPTION=ARRAY,用于可以列出多次的选项。
options: # 应用于单元的选项,但不进入单元文件
enabled: # 默认为是
state: # 默认为started,选择:reloaded;restarted;started;stopped。
restart_on_change: # 默认为是
backup_before_write: # 默认为是
filepath: 默认为`/etc/systemd/system`用于root或`~/.config/systemd/user`用于用户
示例:Logrotate
文件: /usr/lib/systemd/system/logrotate.service
[Unit]
Description=旋转日志文件
Documentation=man:logrotate(8) man:logrotate.conf(5)
ConditionACPower=true
[Service]
Type=oneshot
ExecStart=/usr/sbin/logrotate /etc/logrotate.conf
# 性能选项
Nice=19
IOSchedulingClass=best-effort
IOSchedulingPriority=7
# 加固选项
# 详细信息: https://www.freedesktop.org/software/systemd/man/systemd.exec.html
# 用户目录日志不保护Home
# 邮件传递不保护私有网络
# 对于小于235的系统,工作SELinux不保护内核可调
# 对于i686的gzip不禁止内存写入执行
PrivateDevices=true
PrivateTmp=true
ProtectControlGroups=true
ProtectKernelModules=true
ProtectSystem=full
RestrictRealtime=true
文件: /usr/lib/systemd/system/logrotate.timer
[Unit]
Description=每日旋转日志文件
Documentation=man:logrotate(8) man:logrotate.conf(5)
[Timer]
OnCalendar=daily
AccuracySec=12h
Persistent=true
[Install]
WantedBy=timers.target
需要配置的变量: (注意:我们不写注释行!)
systemd_units:
root:
service:
logrotate:
config:
Unit:
Description: 旋转日志文件
Documentation: 'man:logrotate(8) man:logrotate.conf(5)'
ConditionACPower: 'true'
Service:
Type: oneshot
ExecStart: /usr/sbin/logrotate /etc/logrotate.conf
Nice: 19
IOSchedulingClass: best-effort
IOSchedulingPriority: 7
PrivateDevices: 'true'
PrivateTmp: 'true'
ProtectControlGroups: 'true'
ProtectKernelModules: 'true'
ProtectSystem: full
RestrictRealtime: 'true'
options:
enabled: false
filepath: /usr/lib/systemd/system/logrotate.service
timer:
logrotate:
config:
Unit:
Description: 每日旋转日志文件
Documentation: 'man:logrotate(8) man:logrotate.conf(5)'
Timer:
OnCalendar: daily
AccuracySec: 12h
Persistent: 'true'
Install:
WantedBy: timers.target
options:
filepath: /usr/lib/systemd/system/logrotate.timer
支持
如需支持,请提出问题并提供以下内容:
- 示例任务/剧本以复制您的问题
- 创建的结果文件。
- 如果修改现有文件,请提供未修改的版本。