【问题标题】:Service status by ansible modules (not via "shell" or "command")ansible 模块的服务状态(不是通过“shell”或“c​​ommand”)
【发布时间】:2017-11-09 15:27:26
【问题描述】:

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

这里是停止服务的代码:

- name: stop 
  service:
    name: mongod
    state: stopped

但是如果你第二次运行上面的脚本,那么它会给你一个服务没有运行的错误。

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

我不想不想在 playbook 中使用“shell”或“c​​ommand”选项,我想使用 ansible 模块。

解决方案:How to get service status by Ansible? 使用的是 shell 或命令

【问题讨论】:

    标签: mongodb service ansible


    【解决方案1】:

    很遗憾,我找不到答案,所以我继续使用旧的解决方案:

    - 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
    

    如果您找到了解决方案,请告知。

    【讨论】:

      【解决方案2】:

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

      你可以试试这样的:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多