Check if service exists with Ansible

后端 未结 6 952
面向向阳花
面向向阳花 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:38

    My few cents. The same approach as above but for kubernetes

    Check if kublete service is running

    - name: "Obtain state of kublet service"
      command: systemctl status kubelet.service
      register: kubelet_status
      failed_when: kubelet_status.rc > 3
    

    Display debug message if kublet service is not running

    - debug:
        msg: "{{ kubelet_status.stdout }}"
      when: "'running' not in kubelet_status.stdout"
    
    0 讨论(0)
  • 2020-12-14 14:42

    See the service_facts module, new in Ansible 2.5.

    - name: Populate service facts
      service_facts:
    - debug:
        msg: Docker installed!
      when: "'docker' in services"
    
    0 讨论(0)
  • 2020-12-14 14:42

    I modified Florian's answer to only use the return code of the service command (this worked on Mint 18.2)

    - name: Check if Logstash service exist
      shell: service logstash status 
      register: logstash_status
      failed_when: not(logstash_status.rc == 3 or logstash_status.rc == 0)
    
    - name: Check if Logstash service exist
      service:
        name: logstash
        state: stopped 
      when: logstash_status.rc == 0
    
    0 讨论(0)
  • 2020-12-14 14:42

    Another approach for systemd (from Jakuje):

    - name: Check if cups-browsed service exists
      command: systemctl cat cups-browsed
      check_mode: no
      register: cups_browsed_exists
      changed_when: False
      failed_when: cups_browsed_exists.rc not in [0, 1]
    
    - name: Stop cups-browsed service
      systemd:
        name: cups-browsed
        state: stopped 
      when: cups_browsed_exists.rc == 0
    
    0 讨论(0)
  • 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)
    0 讨论(0)
  • 2020-12-14 15:00

    Of course I could also just check if the wrapper script exists in /etc/init.d. So this is what I ended up with:

      - name: Check if Service Exists
        stat: path=/etc/init.d/{{service_name}}
        register: service_status
    
      - name: Stop Service
        service: name={{service_name}} state=stopped
        when: service_status.stat.exists
        register: service_stopped
    
    0 讨论(0)
提交回复
热议问题