模块说明
- Manage services
参数说明
| Parameter | Comments | 
| arguments aliases: args string | 命令行上提供了其他参数。 将远程主机与systemd一起使用时,此设置将被忽略。 | 
| enabled boolean | 服务是否应在启动时启动。 至少需要state和enabled中的一个。 Choices: 
 | 
| name string / required | 服务的名称。 | 
| pattern string added in Ansible 0.7 | 如果服务没有响应status命令,请将要查找的子字符串命名为ps命令输出中的子字符串,作为状态结果的替代。 如果找到该字符串,则假定该服务已启动。 将远程主机与systemd一起使用时,此设置将被忽略。 | 
| runlevel string | 仅适用于 OpenRC 初始化脚本(例如 Gentoo)。 该服务所属的运行级别。 在将远程主机与 systemd 一起使用时,此设置将被忽略。 Default:  | 
| sleep integer added in Ansible 1.3 | 如果服务正在重新启动,那么在停止和启动命令之间休眠这几秒钟。 这有助于解决行为不端的init脚本,这些脚本在发出进程停止信号后立即退出。 并非所有服务管理器都支持睡眠,即当使用systemd时,此设置将被忽略。 | 
| state string | started/stopped 是幂等操作,除非必要,否则不会运行命令。 restarted 总是会弹出服务。 reloaded 将始终重新加载。 至少需要state和enabled中的一个。 请注意,如果服务尚未启动,即使您选择的init系统无法正常启动,reloaded也会启动该服务。 Choices: 
 | 
| use string added in Ansible 2.2 | 服务模块实际上使用系统特定的模块,通常通过自动检测,此设置可以强制使用特定的模块。 通常,它使用“ansible_service_mgr”事实的值,当找不到匹配时,它会返回到旧的“service”模块。 Default:  | 
示例
- name: Start service httpd, if not started
  ansible.builtin.service:
    name: httpd
    state: started
- name: Stop service httpd, if started
  ansible.builtin.service:
    name: httpd
    state: stopped
- name: Restart service httpd, in all cases
  ansible.builtin.service:
    name: httpd
    state: restarted
- name: Reload service httpd, in all cases
  ansible.builtin.service:
    name: httpd
    state: reloaded
- name: Enable service httpd, and not touch the state
  ansible.builtin.service:
    name: httpd
    enabled: yes
- name: Start service foo, based on running process /usr/bin/foo
  ansible.builtin.service:
    name: foo
    pattern: /usr/bin/foo
    state: started
- name: Restart network service for interface eth0
  ansible.builtin.service:
    name: network
    state: restarted
    args: eth0参考文档
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html#ansible-collections-ansible-builtin-service-module
    
    









