Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb ansible模块的服务状态(不通过“外壳”或“命令”)_Mongodb_Service_Ansible - Fatal编程技术网

Mongodb ansible模块的服务状态(不通过“外壳”或“命令”)

Mongodb ansible模块的服务状态(不通过“外壳”或“命令”),mongodb,service,ansible,Mongodb,Service,Ansible,通过Ansible Playbook,我想先检查服务是否存在(正在运行),然后停止mongod等服务 以下是停止服务的代码: - name: stop service: name: mongod state: stopped 但是,如果您再次运行上述脚本,那么它会提示您服务未运行的错误 如何在运行此脚本之前检查状态?如何确保服务正在运行,然后停止服务 我不想在剧本中使用“shell”或“command”选项,我想使用ansible模块 位于的解决方案正在使用shell或命

通过Ansible Playbook,我想先检查服务是否存在(正在运行),然后停止mongod等服务

以下是停止服务的代码:

- name: stop 
  service:
    name: mongod
    state: stopped
但是,如果您再次运行上述脚本,那么它会提示您服务未运行的错误

如何在运行此脚本之前检查状态?如何确保服务正在运行,然后停止服务

我不想在剧本中使用“shell”或“command”选项,我想使用ansible模块


位于的解决方案正在使用shell或命令

很遗憾,我找不到答案,因此我继续使用旧解决方案:

- name: Check if mongod is active
  command: systemctl status mongod
  register: deb_check
  ignore_errors: yes
  no_log: True
  failed_when: false
# I ignored all errors when the process is dead

- name: Stop mongof if it is active otherwise SKIP
  service:
    name: mongod
    state: stopped
  when: deb_check.stdout.find('dead') == -1

如果您找到了解决方案,请提供建议。

正确的答案是与处理程序一起工作,因为处理程序只会在播放结束时执行,因此它们特别用于避免多次启动或停止服务

您可以尝试类似的方法:

tasks:
    - name: Check if mongod is active
      command: systemctl status sshd
      register: deb_check
      ignore_errors: yes
      no_log: True
      failed_when: false

    - name: Raise a flag whenever the service should be stopped
      notify: stop mongod
      when: '"dead" in deb_check.stdout'
handlers:
    - name: stop mongod
      service:
          name: mongod
          state: stopped