1

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-bit\\bin\\fluent-bit.exe -c C:\\fluent-bit\\conf\\fluent-bit.conf",
        "start_mode": "manual",
        "state": "stopped",
        "username": "LocalSystem"
    }
}

Cheers :)

  • => `when: service_info.exists`. Note: using the same variable name for every register will probably get you into trouble. Change it. – Zeitounator Dec 09 '21 at 15:26
  • I think i figured it out, maybe. `--- - name: Check Windows service status hosts: win gather_facts: True tasks: - name: Get info for services ansible.windows.win_service_info: name: fluent-bit register: service_info - debug: msg: '{{service_info}} on {{ inventory_hostname }}' - name: Send message if service exists debug: msg: "Service is installed" when: service_info.exists != True - name: msg service exists debug: msg: "Service is NOT installed" when: service_info.exists != True – Aslak Laine Dec 09 '21 at 15:27
  • Zeitounator, thanks for your reply. :) – Aslak Laine Dec 09 '21 at 15:30
  • Comments are not meant for large portion of code. This should go either in an edit to your original question or in a self answer if you feel this perfectly solves your issue and that it is useful for other users in the future. My opinion is that your question was asked a bit too early before you got enough time to fully inspect the subject by yourself (and this is actually why I did not answer but only gave you a hint in a comment). The best option would be to delete it before it most probably get closed by votes. – Zeitounator Dec 09 '21 at 15:54
  • @Zeitounator, thanks for your tips – Aslak Laine Dec 09 '21 at 16:20
  • You did not fully get my tip. You edit your question to add more info for someone to provide an answer. If your provide an answer this goes in..... an answer. You are fully authorized to self-answer your question on SO. – Zeitounator Dec 09 '21 at 16:36

1 Answers1

1

So, got it working as I wanted with service_info.exists != True, now it will skip the task if service is already present.

  • 2
    Comparing to a bare boolean is a bad practice. Use `when: not service_info.exists`. If you need extra security (in case your boolean value might be passed as a string which happens frequently in anslible) use `when: not (service_info.exists | bool)` – Zeitounator Dec 09 '21 at 18:48