我如何知道ansible中的服务状态?
How do i know the service status in ansible?
在我的 ansible 编码中,我想知道服务的状态,例如 service httpd status
(服务是否正在运行)结果将存储到变量中。使用该状态,我将在 ansible 中使用一些其他代码。
我正在使用 ansible 服务模块,没有状态选项。如果我使用 shell 模块,我会收到此警告
[WARNING]: Consider using service module rather than running service
还有其他模块在获取服务状态吗?
不,没有获取服务状态的标准模块。
但是如果您知道自己在做什么,则可以抑制针对特定 command
任务的警告:
- command: service httpd status
args:
warn: false
我不久前发布了关于此技巧的快速 note。
希望service: allow user to query service status #3316尽快合并到核心模块中。
您可以使用此 diff to system/service.py
手动修补它
这是我使用 ansible 2.2.0.0 的差异。我 运行 在我的 mac/homebrew 安装中安装了它,它对我有用。
这是我编辑的文件:/usr/local/Cellar/ansible/2.2.0.0_2/libexec/lib/python2.7/site-packages/ansible/modules/core/system/service.py
@@ -36,11 +36,12 @@
- Name of the service.
state:
required: false
- choices: [ started, stopped, restarted, reloaded ]
+ choices: [ started, stopped, status, restarted, reloaded ]
description:
- C(started)/C(stopped) are idempotent actions that will not run
- commands unless necessary. C(restarted) will always bounce the
- service. C(reloaded) will always reload. B(At least one of state
+ commands unless necessary. C(status) would report the status of
+ the service C(restarted) will always bounce the service.
+ C(reloaded) will always reload. B(At least one of state
and enabled are required.)
sleep:
required: false
@@ -1455,7 +1456,7 @@
module = AnsibleModule(
argument_spec = dict(
name = dict(required=True),
- state = dict(choices=['running', 'started', 'stopped', 'restarted', 'reloaded']),
+ state = dict(choices=['running', 'started', 'stopped', 'status', 'restarted', 'reloaded']),
sleep = dict(required=False, type='int', default=None),
pattern = dict(required=False, default=None),
enabled = dict(type='bool'),
@@ -1501,6 +1502,9 @@
else:
service.get_service_status()
+ if module.params['state'] == 'status':
+ module.exit_json(state=service.running)
+
# Calculate if request will change service state
service.check_service_changed()
您可以使用 service_facts 模块。
比如我想查看Apache的状态。
- name: Check for apache status
service_facts:
- debug:
var: ansible_facts.services.apache2.state
输出为:
ok: [192.168.blah.blah] => {
"ansible_facts.services.apache2.state": "running"
}
如果您想查看所有这些,只需在数组中向上两层即可:
var: ansible_facts.services
输出将列出所有服务,看起来像这样(为简洁起见被截断):
"apache2": {
"name": "apache2",
"source": "sysv",
"state": "running"
},
"apache2.service": {
"name": "apache2.service",
"source": "systemd",
"state": "running"
},
"apparmor": {
"name": "apparmor",
"source": "sysv",
"state": "running"
},
etc,
etc
我正在使用 Ansible 2.7。以下是该模块的文档:Click here
这里是启动服务然后使用服务事实检查状态的示例,在我的示例中,您必须注册变量然后使用调试变量输出它并指向 json 链中的正确格式结果输出:
## perform start service for alertmanager
- name: Start service alertmanager if not started
become: yes
service:
name: alertmanager
state: started
## check to see the state of the alertmanager service status
- name: Check status of alertmanager service
service_facts:
register: service_state
- debug:
var: service_state.ansible_facts.services["alertmanager.service"].state
在我的 ansible 编码中,我想知道服务的状态,例如 service httpd status
(服务是否正在运行)结果将存储到变量中。使用该状态,我将在 ansible 中使用一些其他代码。
我正在使用 ansible 服务模块,没有状态选项。如果我使用 shell 模块,我会收到此警告
[WARNING]: Consider using service module rather than running service
还有其他模块在获取服务状态吗?
不,没有获取服务状态的标准模块。
但是如果您知道自己在做什么,则可以抑制针对特定 command
任务的警告:
- command: service httpd status
args:
warn: false
我不久前发布了关于此技巧的快速 note。
希望service: allow user to query service status #3316尽快合并到核心模块中。
您可以使用此 diff to system/service.py
手动修补它这是我使用 ansible 2.2.0.0 的差异。我 运行 在我的 mac/homebrew 安装中安装了它,它对我有用。
这是我编辑的文件:/usr/local/Cellar/ansible/2.2.0.0_2/libexec/lib/python2.7/site-packages/ansible/modules/core/system/service.py
@@ -36,11 +36,12 @@
- Name of the service.
state:
required: false
- choices: [ started, stopped, restarted, reloaded ]
+ choices: [ started, stopped, status, restarted, reloaded ]
description:
- C(started)/C(stopped) are idempotent actions that will not run
- commands unless necessary. C(restarted) will always bounce the
- service. C(reloaded) will always reload. B(At least one of state
+ commands unless necessary. C(status) would report the status of
+ the service C(restarted) will always bounce the service.
+ C(reloaded) will always reload. B(At least one of state
and enabled are required.)
sleep:
required: false
@@ -1455,7 +1456,7 @@
module = AnsibleModule(
argument_spec = dict(
name = dict(required=True),
- state = dict(choices=['running', 'started', 'stopped', 'restarted', 'reloaded']),
+ state = dict(choices=['running', 'started', 'stopped', 'status', 'restarted', 'reloaded']),
sleep = dict(required=False, type='int', default=None),
pattern = dict(required=False, default=None),
enabled = dict(type='bool'),
@@ -1501,6 +1502,9 @@
else:
service.get_service_status()
+ if module.params['state'] == 'status':
+ module.exit_json(state=service.running)
+
# Calculate if request will change service state
service.check_service_changed()
您可以使用 service_facts 模块。
比如我想查看Apache的状态。
- name: Check for apache status
service_facts:
- debug:
var: ansible_facts.services.apache2.state
输出为:
ok: [192.168.blah.blah] => {
"ansible_facts.services.apache2.state": "running"
}
如果您想查看所有这些,只需在数组中向上两层即可:
var: ansible_facts.services
输出将列出所有服务,看起来像这样(为简洁起见被截断):
"apache2": {
"name": "apache2",
"source": "sysv",
"state": "running"
},
"apache2.service": {
"name": "apache2.service",
"source": "systemd",
"state": "running"
},
"apparmor": {
"name": "apparmor",
"source": "sysv",
"state": "running"
},
etc,
etc
我正在使用 Ansible 2.7。以下是该模块的文档:Click here
这里是启动服务然后使用服务事实检查状态的示例,在我的示例中,您必须注册变量然后使用调试变量输出它并指向 json 链中的正确格式结果输出:
## perform start service for alertmanager
- name: Start service alertmanager if not started
become: yes
service:
name: alertmanager
state: started
## check to see the state of the alertmanager service status
- name: Check status of alertmanager service
service_facts:
register: service_state
- debug:
var: service_state.ansible_facts.services["alertmanager.service"].state