robertdebock.service
Ansible 角色服务
为您的 Linux 系统添加自定义服务。
GitHub | GitLab | 下载 | 版本 |
---|---|---|---|
示例剧本
该示例取自 molecule/default/converge.yml
,在每次推送、拉取请求和发布时进行测试。
---
- name: Converge
hosts: all
become: true
gather_facts: true
vars:
_service_test_command:
default: /usr/bin/sleep
Alpine: /bin/sleep
Debian: /bin/sleep
Ubuntu-16: /bin/sleep
Ubuntu-18: /bin/sleep
service_test_command: "{{ _service_test_command[ansible_distribution ~ '-' ~ ansible_distribution_major_version] | default(_service_test_command[ansible_os_family] | default(_service_test_command['default'])) }}" # noqa 204 只是很长。
roles:
- role: robertdebock.service
service_list:
- name: simple-service
description: 简单服务
start_command: "{{ service_test_command }} 3600"
state: started
enabled: true
- name: stopped-service
description: 简单服务
start_command: "{{ service_test_command }} 3601"
state: stopped
enabled: false
- name: specific-stop-service
description: 特定停止服务
start_command: "{{ service_test_command }} 1440"
stop_command: /usr/bin/killall -f "sleep 1440"
- name: specific-user-group-service
description: 特定用户组服务
start_command: "{{ service_test_command }} 28800"
user_name: root
group_name: root
- name: specific-workingdirectory-service
description: 特定工作目录服务
start_command: "{{ service_test_command }} 57600"
working_directory: /tmp
- name: specific-pattern-service
description: 特定状态模式服务
start_command: "{{ service_test_command }} 115200"
status_pattern: 115200
- name: variable-service
description: 带有环境变量的服务
start_command: "{{ service_test_command }} ${time}"
environment_variables:
time: 230400
- name: pidfile-service
description: 带有 PID 文件的服务
start_command: "{{ service_test_command }} 460800"
pidfile: /var/run/pidfile-service.pid
- name: environmentfile-service
description: 带有环境文件的服务
start_command: "{{ service_test_command }} 921600"
environmentfile: /environmentfile.txt
机器需要准备。在 CI 中使用 molecule/default/prepare.yml
进行准备:
---
- name: Prepare
hosts: all
become: true
gather_facts: false
serial: 30%
roles:
- role: robertdebock.bootstrap
post_tasks:
- name: 放置 /environmentfile.txt
ansible.builtin.copy:
content: "value=variable"
dest: /environmentfile.txt
mode: "0644"
有关如何使用这些角色的完整说明和示例。
角色变量
变量的默认值设置在 defaults/main.yml
中:
---
# 服务的默认文件
# service_list 可以包含要添加到系统的服务列表。
# 每个条目的强制项是:
# - name: 服务的 (简短) 名称,例如 "tomcat"。
# - description: 更长的名称,例如 "Tomcat 应用服务器"。
# - start_command: 启动守护进程的命令,
# 例如 "/usr/local/bin/java -jar some.jar"
# 可选项包括:
# - stop_command: 默认情况下,启动的程序会被找到并停止。
# 如果运行中的程序在启动期间重命名或扩展(包括路径),
# 您可以在这里指定自定义停止命令,例如 "pkill foo"
# - status_pattern: 查找程序状态时要查找的程序(或模式),例如 "artifactory"。
# - type: 程序启动的方式; "simple" 或 "forking"。简单表示程序在前台运行,例如 "nc -l 1234"。分叉表示程序本身分叉,例如 "nc -l 12345 &"
# - working_directory: 启动服务之前要切换的目录。
# - environment_variables: 要设置的变量列表。例如:
# environment_variables:
# variable1: value1
# variable2: value2
# - after: 在提到的服务之后启动。
# - restart_mode: 使用的模式,例如 "always"。
# - restart_seconds: 允许重启完成的时间。
service_list: []
要求
- pip 包在 requirements.txt 中列出。
使用的角色状态
以下角色用于准备系统。您可以通过其他方式准备您的系统。
需求 | GitHub | GitLab |
---|---|---|
robertdebock.bootstrap |
上下文
此角色是众多兼容角色的一部分。有关更多信息,请查看这些角色的 文档。
以下是相关角色的概述:
兼容性
该角色已在以下 容器镜像 上进行测试:
容器 | 标签 |
---|---|
EL | 9 |
Debian | 全部 |
Fedora | 全部 |
Ubuntu | 全部 |
要求的最低 Ansible 版本为 2.12,已对以下版本进行了测试:
- 以前的版本。
- 当前版本。
- 开发版本。
如果发现问题,请在 GitHub 上注册。
许可证
作者信息
请考虑 赞助我。
安装
ansible-galaxy install robertdebock.service
许可证
apache-2.0
下载
351.7k
拥有者
I know my way around (Linux) infrastructure, have a passion for automation, Docker, Ansible, Molecule and ci/cd.