Use python output from file within ansible

Hello,

If I wanted to use the output from some script - chronic.py i.e
result_dict = {‘Incident_number’: , ‘Event_Name’: , ‘Hostname’: , ‘_time’: };

I can add in main.yml like this -

  • name: execute info. fetch script
    script: chronic.py
    register: result_dict

  • debug:
    msg: “{{ result_dict }}”

but how do I get the variables out from result_that for further processing next in a different file within my playbook?

Thanks

I added some fake values to your result_dict so there would be something for the demo. Here’s a playbook with data like you describe.

---
- name: Use python output from file within ansible
  hosts: localhost
  gather_facts: false
  vars:
    result_dict: {'Incident_number': [1,2,3],
                  'Event_Name': [a,b,c] ,
                  'Hostname': [h1,h2,h3],
                  '_time': [t1,t2,t3] }
  tasks:
    - name: loop over result_dict
      ansible.builtin.debug:
        msg: |
          Incident_number: {{ result_dict["Incident_number"][item] }}
          Event_Name: {{ result_dict["Event_Name"][item] }}
          Hostname: {{ result_dict["Hostname"][item] }}
          _time:  {{ result_dict["_time"][item] }}
      loop: '{{ range(0, result_dict["Incident_number"] | length) }}'

And here is the relevant part of its output.

TASK [loop over result_dict] ***
ok: [localhost] => (item=0) => 
  msg: |-
    Incident_number: 1
    Event_Name: a
    Hostname: h1
    _time:  t1
ok: [localhost] => (item=1) => 
  msg: |-
    Incident_number: 2
    Event_Name: b
    Hostname: h2
    _time:  t2
ok: [localhost] => (item=2) => 
  msg: |-
    Incident_number: 3
    Event_Name: c
    Hostname: h3
    _time:  t3
1 Like