windows - 检查是否安装了服务,如果是,则获取状态并生成报告 - 使用 Ansible
问题描述
ansible 的新手。我需要使用 ansible 检查服务/守护进程是否正在运行(Windows 和 Linux)。将为 Windows 和 Linux 提供单独的 yaml 文件。所以我需要:
- 检查服务是否存在
- 如果是,请获取状态
- 将结果导出到文件/报告/csv
对于Windows来说,这是否是一个可行的剧本样本,因为要检查多个服务并捕获结果:
- name: "check status of services"
win_service:
name:
- service1
- service2
- service3
register: result
failed_when: result is not defined
对于Linux,我整理了一个剧本来查看服务是否存在并正在运行:
---
- name: Gathering service facts
service_facts:
register: services_state
- name: Service1 status
service_facts:
- debug:
msg: Service1 is installed
when:
- ansible_facts.services['Service1.service'] is defined
msg: Service1 is running
when:
- services_state.ansible_facts.services["Service1.service"].state is running
- name: Service2 status
service_facts:
- debug:
msg: Service2 is installed
when:
- ansible_facts.services['Service2.service'] is defined
msg: Service2 is running
when:
- services_state.ansible_facts.services["Service2.service"].state is running
- name: Service3 status
service_facts:
- debug:
msg: Service3 is installed
when:
- ansible_facts.services['Service3.service'] is defined
msg: Service3 is running
when:
- services_state.ansible_facts.services["Service3.service"].state is running
你能帮忙看看这两个操作系统的剧本/ymls吗?
此外,对于这两者,我需要将输出重定向到文件以生成报告,因为剧本将在多个服务器上运行
解决方案
请探索win_service ansible模块以验证远程 Windows 服务器中的服务状态并将输出注册到 var
参考文档: https ://docs.ansible.com/ansible/latest/modules/win_service_module.html
- name: "check status of test-service"
win_service:
name: test-service
register: result
failed_when: result is not defined
- name: "print status of service"
debug:
var: result
当您在 ansible 中调试 Windows 服务状态的输出时,您会得到如下所示的输出:
ok: [10.xx.xx.xx] => {
"result": {
"can_pause_and_continue": false,
"changed": false,
"depended_by": [],
"dependencies": [],
"description": "test-service service",
"desktop_interact": false,
"display_name": "test-service",
"exists": true,
"failed": false,
"failed_when_result": false,
"name": "test-service",
"path": "C:\\Users\\justlearning\\test-service.exe",
"start_mode": "auto",
"state": "running",
"username": ".\\justlearning"
}
}
您可以将此状态打印到文件中并将其复制到同一远程服务器中。在您的ansible 主机中有一个名为service_status.txt的文件,其中有一个变量“{{ result }}”并使用win_copy ansible模块进行复制
参考文档: https ://docs.ansible.com/ansible/latest/modules/win_copy_module.html
因此,您的剧本中的下一个任务将如下所示:
- name: copy service status file to server
win_copy:
src: /Users/justlearning/Downloads/service_status.txt
dest: C:\tmp\
同样,您将拥有一个用于处理linux 中的systemd服务或普通服务模块本身的模块,以及一个用于 linux的复制模块
参考文档:
https://docs.ansible.com/ansible/latest/modules/systemd_module.html
https://docs.ansible.com/ansible/latest/modules/service_module.html
https://docs.ansible.com/ansible/latest/modules/copy_module.html
推荐阅读
- swift - iOS奇怪的状态栏颜色
- graphql - 从 gatsby-plugin-feed 中的 RSS 返回图像
- java - 在非弹簧注入类中使用 application.properties
- java - 这个工厂方法问题的更好解决方案是什么
- angular - Angular7 中的 ElasticSearch Post 调用抛出 400 错误
- r - 使用 foreach 时无法从同一包中找到函数(Windows 开发)
- c# - SizeChanged 事件,但仅在完成全部更改后
- android - 为什么 Textview.setText() 在片段中为空?
- azure - Azure 虚拟机规模集中的修补
- javascript - Vue JS 从一个 json 对象创建 json 对象