Conditional when win_service exists?

  ansible, ansible-facts, ansible-template, windows

I’m creating playbook to install fluentbit on windows hosts. Everything is working properly but i’m getting error when creating service, it doesn’t fail the install as then everything is already in place but I would like to figure out how I could leverage conditionals. Could you help me with this? 🙂

My adhoc test-play where I’ve tried to parse results from ansible.windows.win_service_info module is as follows:

---
- name: Check Windows service status
  hosts: win
  gather_facts: True
  tasks:
    - name: Check if a service is installed
      win_service:
        name: fluent-bit
      register: service_info
    - debug: msg="{{service_info}}"

    - name: Get info for a single service
      ansible.windows.win_service_info:
        name: fluent-bit
      register: service_info
    - debug: msg="{{ service_info }}"

    - name: Get info for a fluent-bit service
      ansible.windows.win_service_info:
        name: logging
      register: service_exists
    - debug: msg="{{ service_exists }}"

    - name: Send message if service exists
      debug:
        msg: "Service is installed"
      when: service_exists.state is not defined or service_exists.name is not defined

    - name: Send message if service exists
      debug:
        msg: "Service is NOT installed"
      when: service_exists.state is not running

I just don’t get it how I could parse output so that I could skip task when fluent-bit -service exists = True like here:

    TASK [debug] *****************************************************************************************
ok: [win-server-1] => {
    "msg": {
        "can_pause_and_continue": false,
        "changed": false,
        "depended_by": [],
        "dependencies": [],
        "description": "",
        "desktop_interact": false,
        "display_name": "fluent-bit",
        **"exists": true,**
        "failed": false,
        "name": "fluent-bit",
        "path": "C:fluent-bitbinfluent-bit.exe -c C:fluent-bitconffluent-bit.conf",
        "start_mode": "manual",
        "state": "stopped",
        "username": "LocalSystem"
    }
}

Cheers 🙂

Source: Windows Questions

LEAVE A COMMENT