Ansible内置模块之service

原创:厦门微思网络

Ansible内置模块之 service

ansible.builtin.service 模块是 Ansible 的内置模块之一,用于管理系统服务。它允许你启动、停止、重启和重新加载服务,并确保服务处于预期的状态。其功能与ansible.builtin.systemd内置模块基本一致, 相比于systemd, 它所能支持的服务类型更全面, 所以能够兼容更多的Linux发行版.

1. 选 项 说 明

|---------------|----|------|-----|---------------------------------------------------|
| 选项 | 必须 | 类型 | 默认值 | 说明 |
| name | 是 | str | 无 | 要管理的服务名称 |
| state | 否 | str | 无 | 服务的目标状态。可为started,stopped,restarted,reloaded |
| enabled | 否 | bool | 无 | 是否启用服务开机自启。可以是 yes 或 no |
| daemon_reload | 否 | bool | no | 是否在执行其他操作之前重新加载系统管理器配置。 |
| force | 否 | bool | no | 存在 state=restarted 或 state=reloaded 时,是否强制重启或重新加载 |
| sleep | 否 | int | 无 | 在重启服务之前等待的时间(秒)。该选项不支持 systemd 类型的服务 |
| use | 否 | str | 无 | 指定服务管理工具,例如 sysv,upstart,systemd,否则会自动检测 |

2. 用 例

启动服务

  • name: Start the Apache service

ansible.builtin.service:

name: httpd

state: started

停止服务

  • name: Stop the Apache service

ansible.builtin.service:

name: httpd

state: stopped

重启服务

  • name: Restart the Apache service

ansible.builtin.service:

name: httpd

state: restarted

重新加载服务

  • name: Reload the Apache service

ansible.builtin.service:

name: httpd

state: reloaded

启用服务开机自启

  • name: Ensure the Apache service is enabled at boot

ansible.builtin.service:

name: httpd

enabled: yes

禁用服务开机自启

  • name: Ensure the Apache service is disabled at boot

ansible.builtin.service:

name: httpd

enabled: no

在执行其他操作之前重新加载系统管理器配置

  • name: Reload systemd manager configuration before restarting the Apache service

ansible.builtin.service:

name: httpd

state: restarted

daemon_reload: yes

指定使用 systemd 管理服务

  • name: Start the Apache service using systemd

ansible.builtin.service:

name: httpd

state: started

use: systemd


END

微思网络,始于2002年

专业IT认证培训23年,面向全国招生!


点击查看更多【培训课程目录】

微思-主要课程有:

*网络技术:华为HCIA/ HCIP/HCIE;思科CCNA/CCNP/CCIE

*Linux技术:红帽 RHCE/RHCA

*K8S&容器:CKA/CKS

*数据库:ORACLE OCP/ OCM ;MySQL ;达梦数据库

*虚拟化:VMware VCP/VCAP

*安全认证:CISP体系/CISSP/ CISA;CCSK;CISAW体系

*管理类:PMP 项目管理;软考中/高项;ITIL体系;Togaf

其他课程如:ACP;Azure...

相关推荐
minji...4 分钟前
Linux 网络基础之网络IP层(十)IP 协议,网段划分,IP地址相关问题
linux·运维·服务器·网络·tcp/ip·智能路由器·php
枳实-叶15 分钟前
【Linux驱动开发】第10天:设备树零基础入门——DTS/DTB/DTC全解+编译流程
linux·运维·驱动开发
枳实-叶18 分钟前
【Linux驱动开发】第11天:设备树(Device Tree)超详细全解:从诞生背景到工作原理
linux·运维·驱动开发
IceSugarJJ19 分钟前
Windows下VSCode+ WSL项目启动流程
linux·windows·vscode·ubuntu·wsl
Bert.Cai32 分钟前
Linux shift命令详解
linux·运维·服务器
cui_ruicheng35 分钟前
Linux网络编程(六):UDP聊天室与线程池
linux·服务器·网络·udp
Yeats_Liao39 分钟前
物联网接入层技术剖析(一):从select到epoll
java·linux·后端·物联网·struts
一只小逸白41 分钟前
LeetCode Go 常用函数速查表
linux·leetcode·golang
文青小兵41 分钟前
云计算Linux——数据库MySQL读写分离、数据库备份、恢复(十八)
linux·运维·服务器·数据库·mysql·云计算
Ghost Face...1 小时前
PCI总线配置核心实现与架构解析
linux·运维·架构