ansible - 如何将此临时命令编写为剧本
问题描述
我编写了一个临时命令来检查 NGINX 状态,发现它的状态是活动的(因为ActiveState
值为active
)。
现在,我想知道我们如何从 Ansible 剧本中实现同样的目标。
我只想从 Ansible 剧本中知道我的 NGINX 是否处于活动状态,apache 是否处于活动状态。
临时命令:
ansible -b webserver -m service -a "name=nginx"
ip-172-31-34-68.ap-south-1.compute.internal | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"name": "nginx",
"status": {
"ActiveEnterTimestamp": "Thu 2021-03-18 18:00:55 UTC",
"ActiveEnterTimestampMonotonic": "14598079",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "active",
"After": "tmp.mount sysinit.target basic.target -.mount systemd-tmpfiles-setup.service network.target remote-fs.target systemd-journald.socket nss-lookup.target system.slice"
}
}
解决方案
这个 ad-hoc 命令的等价物是service_facts
模块。
所以剧本是:
- hosts: webserver
gather_facts: no
tasks:
- service_facts:
- debug:
var: ansible_facts.services[item].state
loop: "{{ services }}"
vars:
services:
- nginx
- apache
如文档中所述,状态略有不同,但您肯定会在以下位置找到等价物running
:
服务的状态。,
running
,stopped
或unknown
.
推荐阅读
- c# - 403 消息:旧版 People API 尚未在项目中使用
- javascript - 使用 moment.js 将这些 GMT 时间/日期字符串转换为 UTC 时间/日期
- android - 2019 年 8 月 1 日更新补丁后,Android 9 WiFi 设置的左上箭头(返回按钮)无法正常工作
- java - 如何在 java 中创建用户定义的 SettableFuture
- bash - Dockerfile throws an error while running bash script from it
- .net - UdpClient BeginReceive 中的 SocketException 即使使用 try/catch 也会导致整个过程崩溃
- reactjs - 用 Y 参数告诉 apollo-client 从 X 查询返回什么?
- html - How can i change the style of the drop down panel of a select box to have round corners for chrome browser
- php - PHP 新会话 id 重新生成,而不是在重定向后使用现有会话
- vue.js - vuetify 选项卡的地图渲染速度很慢