anasbouzid.systemd
Ansible 角色: systemd
这个角色提供了一种简单而灵活的方式来管理所有 systemd 单位文件。
特点
- 创建所有 systemd 单位文件。
- 创建模板单位。
- 设置所有单位指令。
- 启用/禁用每个单位。
- 在变更时重启单位。
要求
- Ansible 2.8 或更高版本。
角色变量
可用的变量如下所示,并附有默认值(见 defaults/main.yml
):
systemd_units: []
一个字典列表。对于每个单位,您必须提供 name
,所有其他键如 语法部分 所列。
systemd_units_restart_changed: yes
文件更改时是否重启单位。您也可以为每个单位指定参数 restart_changed
。模板单位将始终被跳过。
systemd_units_enabled: yes
是否在启动时启用单位。您也可以为每个单位指定参数 enabled
。模板单位将始终被跳过。
语法
一个 systemd_unit
具有 systemd 模块 的所有参数,以及多个以 _section
结尾的键,您可以指定任意数量的节。每个节都是一个 配置指令 的字典。如果未提供节,则会创建一个空文件。将其用作有效的方法来完全禁用一个单位。
- name: foo.service
scope: user
# 其他 systemd 模块参数...
Unit_section:
Description: Foo
Service_section:
ExecStart: /usr/sbin/foo-daemon
Install_section:
WantedBy: multi-user.target
上面的单位将创建一个 foo.service
文件,其内容如下:
[Unit]
Description=Foo
[Service]
ExecStart=/usr/sbin/foo-daemon
[Install]
WantedBy=multi-user.target
一个指令键可以是 字符串
, 列表
或 字典
,每个键类型将如下转换:
ExecStart: "/usr/bin/foo-daemon"
Wants:
- [email protected]
- [email protected]
Environment:
VAR1: word1 word2
VAR2: word3
VAR3: $word 5 6
ExecStart=/usr/bin/foo-daemon
Wants=foo@1.service
Wants=foo@2.service
Environment="VAR1=word1 word2"
Environment="VAR2=word3"
Environment="VAR3=$word 5 6"
将 snake_case 键转换为 PascalCase
systemd_convert_snake_case_keys: yes
此变量允许您以 snake_case
写所有指令。当启用时,此角色将转换所有 snake_case
指令为 PascalCase
,例如 exec_start: "/usr/bin/foo-daemon"
将转换为 ExecStart=/usr/bin/foo-daemon
。如果您不喜欢此功能,可以禁用它。
systemd_readable_snake_case_keys: # 见 defaults/main.yml
该角色维护一个可读的 snake_case
指令字典。实际上,一些指令是缩写,要以 snake_case 写 CPUShares
,您必须写 c_p_u_shares
。由于这个变量,您可以写 cpu_shares
。
依赖关系
无。
示例剧本
这是一个创建和启动服务单位 sleep.service
的示例剧本文件。
- hosts: all
become: true
vars:
systemd_units:
- name: sleep.service
Service_section:
ExecStart: /bin/sleep 300
Install_section:
WantedBy: multi-user.target
roles:
- { role: anasbouzid.systemd, tags: [systemd] }
这是一个用于创建和启动目标单位 sleep.target
的示例 systemd_units
变量,该单位实例化了 3 个服务模板。
systemd_units:
- name: [email protected]
Unit_section:
Description: Sleep for %i seconds
PartOf: sleep.target
Service_section:
Restart: always
ExecStart: /bin/sleep %i
Install_section:
WantedBy: multi-user.target
- name: sleep.target
Unit_section:
Wants:
- [email protected]
- [email protected]
- [email protected]
Install_section:
WantedBy: multi-user.target
同一个变量也可以用 snake_case
写。
systemd_units:
- name: [email protected]
unit_section:
description: Sleep for %i seconds
part_of: sleep.target
service_section:
restart: always
exec_start: /bin/sleep %i
install_section:
wanted_by: multi-user.target
- name: sleep.target
unit_section:
wants:
- [email protected]
- [email protected]
- [email protected]
install_section:
wanted_by: multi-user.target
为了组织代码,您可以将单位分成不同类别。
systemd_services: []
systemd_targets: []
systemd_sockets: []
systemd_units: "{{ systemd_services + systemd_targets + systemd_sockets }}"
或根据功能进行分组。
systemd_nginx_units: []
systemd_queue_units: []
systemd_php_units: []
systemd_units: "{{ systemd_nginx_units + systemd_queue_units + systemd_php_units }}"
许可证
MIT