2

I am running into an issue and I am not sure what is causing it.

Playbook:

- name: Testing
  hosts: testing
  become: false
  gather_facts: false

  tasks:
    - name: Services Info
      ansible.windows.win_service_info:
        name: WSearch
      register: services_Info

    - name: Output Services
      debug:
        var: services_Info.services

    - name: Output Services
      debug:
        var: services_Info.services.start_mode

Output for services_Info.services

{
  "services_Info.services": [
    {
      "checkpoint": 0,
      "controls_accepted": [],
      "dependencies": [
        "RPCSS",
        "BrokerInfrastructure"
      ],
      "dependency_of": [
        "WMPNetworkSvc"
      ],
      "description": "Provides content indexing, property caching, and search results for files, e-mail, and other content.",
      "desktop_interact": false,
      "display_name": "Windows Search",
      "error_control": "normal",
      "failure_actions": [
        {
          "type": "restart",
          "delay_ms": 30000
        },
        {
          "type": "restart",
          "delay_ms": 30000
        },
        {
          "type": "restart",
          "delay_ms": 30000
        },
        {
          "type": "restart",
          "delay_ms": 30000
        },
        {
          "type": "restart",
          "delay_ms": 30000
        },
        {
          "type": "none",
          "delay_ms": 0
        }
      ],
      "failure_actions_on_non_crash_failure": true,
      "failure_command": null,
      "failure_reboot_msg": null,
      "failure_reset_period_sec": 86400,
      "launch_protection": "none",
      "load_order_group": "",
      "name": "WSearch",
      "path": "C:\\Windows\\system32\\SearchIndexer.exe /Embedding",
      "pre_shutdown_timeout_ms": 10000,
      "preferred_node": null,
      "process_id": 0,
      "required_privileges": [
        "SeChangeNotifyPrivilege",
        "SeManageVolumePrivilege",
        "SeImpersonatePrivilege",
        "SeAssignPrimaryTokenPrivilege",
        "SeIncreaseQuotaPrivilege",
        "SeTcbPrivilege"
      ],
      "service_exit_code": 0,
      "service_flags": [],
      "service_type": "win32_own_process",
      "sid_info": "unrestricted",
      "start_mode": "disabled",
      "state": "stopped",
      "triggers": [],
      "username": "NT AUTHORITY\\SYSTEM",
      "wait_hint_ms": 0,
      "win32_exit_code": 0
    }
  ],
  "_ansible_verbose_always": true,
  "_ansible_no_log": null,
  "changed": false
}

The output above is the result of debugging the variable services_Info.services.
But, when going more specific to include start_mode I get:

"services_Info.services.start_mode": "VARIABLE IS NOT DEFINED!"

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Joe M.
  • 23
  • 3

1 Answers1

2

services_Info.services is an list, as you can see with the usage of brackets [ ], here, in your output:

{
  "services_Info.services": [
  ]
}

You have to know that the output your are seeing from Ansible, per default, is a JSON structure, and that YAML is a natural superset of JSON.

In JSON, the two main structures are

  • objects { "foo": "bar" }
  • array — also called list in Ansible, since it inherits the dialect of its programming language, Python — [ "foo", "bar" ]

Mind that you can adapt this output with callback plugins. A wildly used plugin is the YAML one.

So, since this is a list, you have to address an element of the list, for example the first one: services_Info.services.0.start_mode.
Which will work like a charm in your case but that could be problematic if you have multiple services.

In this later case, you can use the map filter:

services_Info.services | map(attribute='start_mode')

This this will address a list of services, it will return you a list of start_mode, and in the case of your output it will be a list of a single item:

[
  "disabled"
]
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Thank you! When comparing the outputs I had a feeling it was something to do with the '[]' brackets but I had no clue how to look up the question. I also appreciate the jinja documentation site I had just learned what jinja was yesterday term wise and hadn't looked too deeply into it yet. – Joe M. Aug 10 '23 at 13:42