Check if service exists with Ansible

后端 未结 6 968
面向向阳花
面向向阳花 2020-12-14 14:12

I have an Ansible playbook for deploying a Java app as an init.d daemon.

Being a beginner in both Ansible and Linux I\'m having trouble to conditionally execute task

6条回答
  •  没有蜡笔的小新
    2020-12-14 14:57

    It would be nice if the "service" module could handle "unrecognized service" errors.

    This is my approach, using the service command instead of checking for an init script:

    - name: check for apache
      shell: "service apache2 status"
      register: _svc_apache
      failed_when: >
        _svc_apache.rc != 0 and ("unrecognized service" not in _svc_apache.stderr)
    
    - name: disable apache
      service: name=apache2 state=stopped enabled=no
      when: "_svc_apache.rc == 0"
    
    • check the exit code of "service status" and accept the exit code 0 when the output contains "unrecognized service"
    • if the exit code was 0, that service is installed (stopped or running)

提交回复
热议问题