Search code examples
ansibleyamlansible-facts

How to filter the 'win_service_info' result in an Ansible playbook?


This is my playbook. It shows a service of Windows Server.

    - hosts: windows

      tasks:

      - name: Get info all services
        ansible.windows.win_service_info:
           name: Zabbix Agent
        register: service_info

      - debug:
          msg: "{{ service_info }}"

But this shows me much results:

  "pre_shutdown_timeout_ms": 180000,
  "preferred_node": null,
  "process_id": 4092,
  "required_privileges": [],
  "service_exit_code": 0,
  "service_flags": [],
  "service_type": "win32_own_process",
  "sid_info": "none",
  "start_mode": "auto",
  "state": "started",
  "triggers": [],
  "username": "\",
  "wait_hint_ms": 0,
  "win32_exit_code": 0

Only I need the start_mode line.

How can I filter the output?


Solution

  • That object has documented properties that are available as jinja2 expressions:

      - debug:
          msg: the start_mode of Zabbix is {{ service_info.services[0].start_mode }}
    

    Here, because you specified a specific name, the list should only have one item in it, but be careful if you change the _info: to use a wildcard in the future, as the list may have multiple items

    Strictly speaking, it may be that the services list has no items if Zabbix Agent isn't registered; I don't have a windows instance to confirm what happens in that case