How to pass information in a workflow and reuse it in a "summary play" (debug)

394 views
Skip to first unread message

Philipp Marti

unread,
May 23, 2023, 10:38:59 AM5/23/23
to AWX Project
Let's assume I have a hostgroup of 6 hosts and I need to run a playbook (play1) two times in sequence for different hosts in that group because of depenencies between some hosts.

I'd like to gather information within both plays and pass that information further.
In a last step I'd like to reuse the previously collected information in order to create a summary over all hosts (in both plays 1_a and 1_b); here for debugging (later I'd
like to use that information to create an html report with a Jinja2 templating).

The problem I have is the information in the debug messages are not per host. It seems it stores just the output from one host and reuses it for all others in the
inventory in the last play (see sample output of the report_comment variable below -> just one of those 6 servers had that exception).

I'm not sure if using the set_stats module is the best way to reach what I'd like?
Or would it be better to store the information in temp. files on localhost and read the information in the last play?

Any ideas or suggestions how to reach my goal would be much appreciated!

Workflow Template

|-------------|          |-------------|          |-------------|
| play1_a     | --->     | play1_b     | --->     | summary     |
|-------------|          |-------------|          |-------------|


play1_a:
---
hosts: server1
tasks:
  - name: test powershell
      block:
        - name: Search For Windows Udpates
          ansible.windows.win_updates:
            reboot: no
            state: searched
          register: list_of_updates
 
        - name: Pass list_of_updates to next job
      ansible.builtin.set_stats:
    data:
      listofupdates: "{{ list_of_updates }}"

      rescue:
    - name: Catch Exception
  ansible.builtin.set_fact:
            report_comment: "{{ list_of_updates.msg }}"
  ignore_errors: yes
  when: '"Exception" in list_of_updates.msg'

        - name: Pass Exception for Summary
      ansible.builtin.set_stats:
    data:
      reportcomment: "{{ report_comment }}"

play1_b:
---
hosts: hostgroup:!server1
tasks:
  - name: test powershell
      block:
        - name: Search For Windows Udpates
          ansible.windows.win_updates:
            reboot: no
            state: searched
          register: list_of_updates
 
        - name: Pass list_of_updates to next job
      ansible.builtin.set_stats:
    data:
      listofupdates: "{{ list_of_updates }}"

      rescue:
    - name: Catch Exception
  ansible.builtin.set_fact:
            report_comment: "{{ list_of_updates.msg }}"
  ignore_errors: yes
  when: '"Exception" in list_of_updates.msg'

        - name: Pass Exception for Summary
      ansible.builtin.set_stats:
    data:
      reportcomment: "{{ report_comment }}"


summary (Debugging):
---
- hosts: hostgroup
  vars:
        list_of_updates: "{{ listofupdates }}"
        report_comment: "{{ reportcomment }}"

      tasks:
        - name: print output List of Updates
          debug:
            msg: "{{ list_of_updates}}"

       - name: print output Report Comment
         debug:
           msg: "{{ report_comment }}"

Sample Output of report_comment variable:
TASK [print output Report Comment] *********************************************
ok: [server1] => {
    "msg": "Searching for updates: Exception from HRESULT: 0x80072EFD - Unknown WUA HRESULT 2147954429 (UNKNOWN 80072EFD)"
}
ok: [server2] => {
    "msg": "Searching for updates: Exception from HRESULT: 0x80072EFD - Unknown WUA HRESULT 2147954429 (UNKNOWN 80072EFD)"
}
ok: [server3] => {
    "msg": "Searching for updates: Exception from HRESULT: 0x80072EFD - Unknown WUA HRESULT 2147954429 (UNKNOWN 80072EFD)"
}
ok: [server4] => {
    "msg": "Searching for updates: Exception from HRESULT: 0x80072EFD - Unknown WUA HRESULT 2147954429 (UNKNOWN 80072EFD)"
}
ok: [server5] => {
    "msg": "Searching for updates: Exception from HRESULT: 0x80072EFD - Unknown WUA HRESULT 2147954429 (UNKNOWN 80072EFD)"
}
ok: [server6] => {
    "msg": "Searching for updates: Exception from HRESULT: 0x80072EFD - Unknown WUA HRESULT 2147954429 (UNKNOWN 80072EFD)"
}


Rowe, Walter P. (Fed)

unread,
May 23, 2023, 3:20:47 PM5/23/23
to awx-p...@googlegroups.com
I worked around this by creating a dictionary in set_stats. The dictionary consists of a key set to hostname and a value set to my per-host data (can be anything including another dictionary). Downstream plays get the dictionary and each host can find its own data using its hostname as the key into that dict.
- name: Create and Add items to dictionary
  set_stats: 
    data:
      my_dict: "{{ my_dict | default({}) | combine ({ item.key : item.value }) }}"
  with_items:
    - { "key": "{{ inventory_hostname_short }}" , "value": {
          "var_a" : "{{ var_a_val }}",
          "var_b" : "{{ var_b_val }}",
          "var_c" : "{{ var_c_val }}"
        }
      }

The downstream playbooks can use my_dict[inventory_hostname_short] to access their specific data.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "AWX Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to awx-project...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/awx-project/a5c27866-5d21-4734-b1bf-90add64869b5n%40googlegroups.com.

Philipp Marti

unread,
May 24, 2023, 10:45:51 AM5/24/23
to AWX Project
Hi Walter
Thanks a lot for your input! That seems to work for me as well - you made my day ;-)
Much appreciated!
Phil
Reply all
Reply to author
Forward
0 new messages