Ansible内置模块之systemd

B站影视 欧美电影 2025-04-01 16:40 1

摘要:# 启动并启用服务- name: Start the Apache serviceansible.builtin.systemd:name: httpdstate: startedenabled: yes# 停止服务- name: Stop the Apach

Ansible内置模块之 systemd

ansible.builtin.systemd 模块是 Ansible 的内置模块之一,用于管理 systemd 服务。在基于 systemd 的 Linux 系统上,它允许你启动、停止、重启和重新加载服务,管理服务的启用和禁用,以及其他 systemd 相关操作。


1. 选 项 说 明

2. 用 例

# 启动并启用服务
- name: Start the Apache service
ansible.builtin.systemd:
name: httpd
state: started
enabled: yes

# 停止服务
- name: Stop the Apache service
ansible.builtin.systemd:
name: httpd
state: stopped

# 重启服务
- name: Restart the Apache service
ansible.builtin.systemd:
name: httpd
state: restarted

# 重新加载服务
- name: Reload the Apache service
ansible.builtin.systemd:
name: httpd
state: reloaded

# 启用服务开机自启
- name: Ensure the Apache service is enabled at boot
ansible.builtin.systemd:
name: httpd
enabled: yes

# 禁用服务开机自启
- name: Ensure the Apache service is disabled at boot
ansible.builtin.systemd:
name: httpd
enabled: no

# 掩盖(mask)服务
- name: Mask the Apache service
ansible.builtin.systemd:
name: httpd
masked: yes

# 在执行其他操作之前重新加载系统管理器配置
- name: Reload systemd manager configuration before restarting the Apache service
ansible.builtin.systemd:
name: httpd
state: restarted
daemon_reload: yes

# 使用非阻塞模式启动服务
- name: Start the Apache service in non-blocking mode
ansible.builtin.systemd:
name: httpd
state: started
no_block: yes

# 在用户作用域中操作服务
- name: Start a user-scoped service
ansible.builtin.systemd:
name: my_user_service
state: started
user: yes

来源:奋斗的IT挨踢人

相关推荐