在我的编码中,我想知道服务的状态,如service httpd status
(服务是否是runngin),结果将存储到变量中。使用这种状态我会使用一些其他的代码。我如何知道服务状态?
我正在使用安全的服务模块,没有状态选项。如果我使用的外壳模块我得到这个警告
[WARNING]: Consider using service module rather than running service
因此,它是任何其他模块做让服务状态?
在我的编码中,我想知道服务的状态,如service httpd status
(服务是否是runngin),结果将存储到变量中。使用这种状态我会使用一些其他的代码。我如何知道服务状态?
我正在使用安全的服务模块,没有状态选项。如果我使用的外壳模块我得到这个警告
[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()
这里httpd服务启动,将工作良好。如果服务停止,我得到错误。 – SSN
如果'service'命令以非零退出代码退出,则应该使用'failed_when:false'来处理它。 –