ansible

Ansible - Role/Task doesn't return correct value


I'm really new to Ansible here. I'm converting a playbook from just a playbook that uses modules directly to a playbook that uses Roles and Tasks. With the original playbook, I could get values back from the module, but I can't get the return values from module used in the Task back to the playbook.

The playbook inspects the registry of Windows target nodes.

Here's a snippet of my original playbook:

---
- name: Reads Windows registry entries
  hosts: all
  gather_facts: yes
  tasks:
  - name: Obtain information about a registry key property 
    ansible.windows.win_reg_stat:
      path: HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion
      name: ProductName
    register: product_name
  - name: Summary info
    debug:
      msg:
        - "Windows version = {{product_name.value}}"

It gave me a return value like this:

["Windows version = {'changed': False, 'type': 'REG_SZ', 'raw_value': 'Windows 10 Enterprise', 'value': 'Windows 10 Enterprise', 'exists': True, 'failed': False}"]

which was handy. I could even access the value member directly ('Windows 10 Enterprise'), as seen above.

The new playbook using the Role/Task:

---
- name: Reads Windows registry entries
  hosts: all
  gather_facts: yes
  tasks:
    - name: Obtain information about a registry key property 
      ansible.builtin.include_role:
        name: read_registry
      vars:
        keys:
          - path: "HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
            name: "ProductName"
    - name: Summary info
      debug:
        msg:
          - "Windows version = {{ result }}"

And the new read_registry/tasks/main.yml:

---
# tasks file for read_registry

- name: Inspect registry entries of Windows machine
  ansible.builtin.include_tasks: read_registry.yml
  loop: "{{ keys }}"
  loop_control:
    loop_var: keys
  register: result

And the actual task file:

---

  - name: Read Windows registry key
    ansible.windows.win_reg_stat:
      path: "{{ keys.path }}"
      name: "{{ keys.name }}"

The playbook gets a value back in result, but it's not the same one as I got back with the original playbook. Instead of the value of the registry key, it gives me the parameters I called the module with and some other information:

["Windows version = {'results': [{'include': 'read_registry.yml', 'include_args': {}, 'keys': {'path': 'HKLM:\\\\SOFTWARE\\\\Microsoft\\\\Windows NT\\\\CurrentVersion', 'name': 'ProductName'}, 'ansible_loop_var': 'keys'}], 'skipped': False, 'msg': 'All items completed', 'changed': False}"]

While it's nice to know the task completed, that's not the info I need.

Eventually the new playbook will have a list of registry keys to inspect, such as:

keys:
  - path: "HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
    name: "ProductName"
  - path: "SomeOtherLongKeyPath"
    name: "OtherKeyName"
  - path: "AnotherLongKeyPath"
    name: "NextName"

So I want to report on all of the results at the end. I'm not sure how to get all the results with this approach, but right now I'm not even getting one correct one.


Solution

  • The playbook gets a value back in result, but it's not the same one as I got back with the original playbook.

    It's expected.

    In first case, you recorded the results of a specific ansible.windows.win_reg_stat task execution. In second case, you're recording the results of ansible.builtin.include_tasks instead, while the results of ansible.windows.win_reg_stat invoked from read_registry.yml don't get recorded anywhere.

    One of the options to solve this could be to define a list and append the results of ansible.windows.win_reg_stat invocations to it. Something like this:

    # read_registry.yml
    ---
    - name: Read Windows registry key
      ansible.windows.win_reg_stat:
        path: "{{ keys.path }}"
        name: "{{ keys.name }}"
      register: key_result
    
    - name: Add the result to the list
      set_fact:
        result: "{{ key_result + result | default([]) }}"
    

    There also could be other, maybe more elegant, solutions - for example, you could try to add the result values to the items of your initial keys list using combine filter - but I hope it gives you the understanding of what exactly went wrong initially.

    Also, I wouldn't recommend naming the loop_var the same as the actual variable you're iterating over to avoid confusion (I actually wasn't sure it will work but looks like it does).