Passing a var between tasks

Hi. Here is my problem. I dont know much about Ansible. I need to write a simple playbook that will perform the following:

Go to ServiceNow ticketing system, get a Change Request meeting specific parameters, pass its SYS_ID to the next task which will download the file that was uploaded to the Request by the user during opening.

This is only a P.O.C., so a lot of assumption are made here. For example, that there will be only one such Request (could have been many!) , and each such request has only one attachment (again, could be more than one script uploaded to one CR!). But this simplified, impractical solution will suffice for the P.O.C. purposes. I only need to show my management Ansible can handle the simplest form of the application for now.

My problem is that I can ONLY download the single attachment if I hardcode the attachment SYS_ID inside the playbook. But this is supposed to be dynamic.

Please point me in the right direction with corrections to my playbook.

---
    - name: Oracle database Data Change Request Process ID 2872.56
#      hosts: dbservers
      hosts: localhost
      gather_facts: true
      vars_files:
        - vars/main.yml

      tasks:

        - name: 0. Prepare. Display Ansible date_time fact and register | itsm_dcr
          delegate_to: localhost
          run_once: true
          set_fact: 
            currenttime: "{{ ansible_date_time.iso8601 }}"
          tags: linux

        - name: 1. Retrieve new DCR change requests
          servicenow.itsm.change_request_info:
             instance:
               host: "{{ sn_instance}}"
               username: "{{ sn_username }}"
               password:  "{{ sn_password }}"
             query:
              - state: = new
              - short_description: LIKE DCR
          register: dcr_new_cr_request_found_out
          delegate_to: localhost

        - name: 1.1 Debug the ServiceNow Change Request details to get the attachment SYS_ID | itsm_dcr
          debug: var=dcr_new_cr_request_found_out  

        - name: Cache the CR SYS_ID {{ ansible_date_time.iso8601 }} | itsm_dcr
          ansible.builtin.set_fact:
            sys_id: "{{ dcr_new_cr_request_found_out.records[0].sys_id }}"    

        - name: 2. Download the Change Request attachment with its hardcoded SYS_ID from Step 1 | itsm_dcr
          servicenow.itsm.attachment: 
            instance:
              host: "{{ sn_instance}}"
              username: "{{ sn_username }}"
              password:  "{{ sn_password }}"
            dest: /tmp/DCR_{{ currenttime }}_00000001.sql
#          sys_id: "{{ dcr_new_cr_request_found_out.records[0].attachments[0].sys_id }}"            
            sys_id: 003a3ef24ff1120031577d2ca310c74b
          register: dcr_new_cr_attachment_out  

        - name: 2.1 Debug the hardcoded-SYS-ID attachment download details | itsm_dcr
          debug: var=dcr_new_cr_attachment_out 

Output:

[ansible_admin@ctrl ansible]$ ansible-playbook ./playbooks/dcr_03.yml

PLAY [Oracle database Data Change Request Process ID 2872.56] *************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************
ok: [localhost]
[WARNING]: Using run_once with the free strategy is not currently supported. This task will still be executed for every host in the inventory list.

TASK [0. Prepare. Display Ansible date_time fact and register | itsm_dcr] *************************************************************************************************************
ok: [localhost]

TASK [1. Retrieve new DCR change requests] ********************************************************************************************************************************************
[WARNING]: Encountered unknown value  while mapping field risk.
[WARNING]: Encountered unknown value Software while mapping field category.
ok: [localhost]

TASK [1.1 Debug the ServiceNow Change Request details to get the attachment SYS_ID | itsm_dcr] ****************************************************************************************
ok: [localhost] => {
    "dcr_new_cr_request_found_out": {
        "changed": false,
        "failed": false,
        "records": [
            {
                "active": "true",
                "activity_due": "",
                "additional_assignee_list": "",
                "approval": "approved",
                "approval_history": "",
                "approval_set": "2026-01-27 19:29:50",
                "assigned_to": "3c589a008334f210f28198c6feaad375",
                "assignment_group": "db53580b0a0a0a6501aa37c294a2ba6b",
                "attachments": [
                    {
                        "average_image_color": "",
                        "chunk_size_bytes": "700000",
                        "compressed": "true",
                        "content_type": "text/x-sql",
                        "download_link": "https://dev276659.service-now.com/api/now/attachment/33c38e4483b6f210f28198c6feaad3c1/file",
                        "file_name": "DCR_001.sql",
                        "hash": "be2477f129a827a656c0b76392edbfa4b10568341786837b5f67ba7a25abb359",
                        "image_height": "",
                        "image_width": "",
                        "size_bytes": "141",
                        "size_compressed": "146",
                        "state": "available",
                        "sys_created_by": "admin",
                        "sys_created_on": "2026-01-27 19:29:41",
                        "sys_id": "33c38e4483b6f210f28198c6feaad3c1",
                        "sys_mod_count": "1",
                        "sys_tags": "",
                        "sys_updated_by": "system",
                        "sys_updated_on": "2026-01-27 19:29:43",
                        "table_name": "change_request",
                        "table_sys_id": "34a34e0883f6f210f28198c6feaad380"
                    }
                ],
                "backout_plan": "",
                "business_duration": "",
                "business_service": "281a4d5fc0a8000b00e4ba489a83eedc",
                "cab_date_time": "",
                "cab_delegate": "",
                "cab_recommendation": "",
                "cab_required": "false",
                "calendar_duration": "",
                "category": "Software",
                "change_plan": "",
                "chg_model": "007c4001c343101035ae3f52c1d3aeb2",
                "close_code": "",
                "close_notes": "",
                "closed_at": "",
                "closed_by": "",
                "cmdb_ci": "0c43d5e2c61122750182c132a8b9e2d9",
                "comments": "",
                "comments_and_work_notes": "",
                "company": "",
                "conflict_last_run": "",
                "conflict_status": "Not Run",
                "contact_type": "",
                "contract": "",
                "copied_from": "",
                "correlation_display": "",
                "correlation_id": "",
                "delivery_plan": "",
                "delivery_task": "",
                "description": "--- Managed by r-able.com hyperautomation (TurboCharged, V2.2.2). Do not change manually. Do not deviate. ---\r\n--- This Change Request adds a new deployment to an already installed GoldenGate hub binaries ---\r\n--- Enter the required parameters on the right side of the \":\" below ---\r\nRun dcr\r\n",
                "due_date": "",
                "end_date": "",
                "escalation": "0",
                "expected_start": "",
                "follow_up": "",
                "group_list": "",
                "impact": "low",
                "implementation_plan": "",
                "justification": "",
                "knowledge": "false",
                "location": "",
                "made_sla": "true",
                "number": "CHG0030971",
                "on_hold": false,
                "on_hold_reason": "",
                "on_hold_task": "",
                "opened_at": "2026-01-27 19:28:56",
                "opened_by": "6816f79cc0a8016401c5a33be04be441",
                "order": "",
                "outside_maintenance_schedule": "false",
                "parent": "",
                "phase": "requested",
                "phase_state": "open",
                "priority": "low",
                "production_system": "false",
                "reason": "",
                "reassignment_count": "0",
                "requested_by": "6816f79cc0a8016401c5a33be04be441",
                "requested_by_date": "",
                "review_comments": "",
                "review_date": "",
                "review_status": "",
                "risk": "",
                "risk_impact_analysis": "",
                "route_reason": "",
                "scope": "3",
                "service_offering": "04b89573835e7210f28198c6feaad3f0",
                "short_description": "DCR",
                "sla_due": "",
                "start_date": "",
                "state": "new",
                "std_change_producer_version": "",
                "sys_class_name": "change_request",
                "sys_created_by": "admin",
                "sys_created_on": "2026-01-27 19:29:50",
                "sys_domain": "global",
                "sys_domain_path": "/",
                "sys_id": "34a34e0883f6f210f28198c6feaad380",
                "sys_mod_count": "3",
                "sys_tags": "",
                "sys_updated_by": "admin",
                "sys_updated_on": "2026-01-27 21:11:45",
                "task_effective_number": "CHG0030971",
                "test_plan": "",
                "time_worked": "",
                "type": "normal",
                "unauthorized": "false",
                "universal_request": "",
                "upon_approval": "proceed",
                "upon_reject": "cancel",
                "urgency": "low",
                "user_input": "",
                "watch_list": "",
                "work_end": "",
                "work_notes": "",
                "work_notes_list": "",
                "work_start": ""
            }
        ],
        "warnings": [
            "Encountered unknown value  while mapping field risk.",
            "Encountered unknown value Software while mapping field category."
        ]
    }
}

TASK [Cache the CR SYS_ID 2026-02-01T01:04:45Z | itsm_dcr] ****************************************************************************************************************************
ok: [localhost]

TASK [2. Download the Change Request attachment with its hardcoded SYS_ID from Step 1 | itsm_dcr] *************************************************************************************
changed: [localhost]

TASK [2.1 Debug the hardcoded-SYS-ID attachment download details | itsm_dcr] **********************************************************************************************************
ok: [localhost] => {
    "dcr_new_cr_attachment_out": {
        "changed": true,
        "failed": false,
        "record": {
            "elapsed": 0.4,
            "msg": "OK",
            "size": 106879,
            "status_code": 200
        }
    }
}

PLAY RECAP ****************************************************************************************************************************************************************************
localhost                  : ok=7    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

and I can see the hardcoded-attachment-SYS_ID file downloaded to my /tmp:

image

Please help me make this single (for now) attachment SYS_ID passed as a VAR to the download module.
Thank you.
Nestor Kandinsky-Clerambeau.

What was the issue with the commented out line? That looks like what your asking for

1 Like

Hi @clerambeau370

It looks like you’re almost there! From the module JSON output you’ve posted you already have the line you need:

The indentation may be slightly off which might be what’s tripping you up here:

- name: 2. Download the Change Request attachment using SYS_ID from Step 1 | itsm_dcr
  servicenow.itsm.attachment: 
    instance:
      host: "{{ sn_instance }}"
      username: "{{ sn_username }}"
      password:  "{{ sn_password }}"
    dest: /tmp/DCR_{{ currenttime }}_00000001.sql
    sys_id: "{{ dcr_new_cr_request_found_out.records[0].attachments[0].sys_id }}"            
  register: dcr_new_cr_attachment_out  ok

Sorry I’m on mobile right now so the code block formatting might be slightly off too Did you try it like this and it was unsuccessful?

1 Like

@mikemorency thank you for the reply.

@dbrennand Holy mother of god. Looks like I did get it right the first time. I swear on my female Yorkies balls, I got a “variable not defined” on the SYS_ID the last time I used it not hardcoded. Otherwise, I wouldn’t have wasted your time. But looks I was right then. It did work! Wow! Thanks @dbrennand @mikemorency !

          sys_id: "{{ dcr_new_cr_request_found_out.records[0].attachments[0].sys_id }}"            
#            sys_id: 003a3ef24ff1120031577d2ca310c74b
[ansible_admin@ctrl ansible]$ ansible-playbook ./playbooks/dcr_03.yml

PLAY [Oracle database Data Change Request Process ID 2872.56] *************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************
ok: [localhost]
[WARNING]: Using run_once with the free strategy is not currently supported. This task will still be executed for every host in the inventory list.

TASK [0. Prepare. Display Ansible date_time fact and register | itsm_dcr] *************************************************************************************************************
ok: [localhost]

TASK [1. Retrieve new DCR change requests] ********************************************************************************************************************************************
[WARNING]: Encountered unknown value  while mapping field risk.
[WARNING]: Encountered unknown value Software while mapping field category.
ok: [localhost]

TASK [1.1 Debug the ServiceNow Change Request details to get the attachment SYS_ID | itsm_dcr] ****************************************************************************************
ok: [localhost] => {
    "dcr_new_cr_request_found_out": {
        "changed": false,
        "failed": false,
        "records": [
            {
                "active": "true",
                "activity_due": "",
                "additional_assignee_list": "",
                "approval": "approved",
                "approval_history": "",
                "approval_set": "2026-01-27 19:29:50",
                "assigned_to": "3c589a008334f210f28198c6feaad375",
                "assignment_group": "db53580b0a0a0a6501aa37c294a2ba6b",
                "attachments": [
                    {
                        "average_image_color": "",
                        "chunk_size_bytes": "700000",
                        "compressed": "true",
                        "content_type": "text/x-sql",
                        "download_link": "https://dev276659.service-now.com/api/now/attachment/33c38e4483b6f210f28198c6feaad3c1/file",
                        "file_name": "DCR_001.sql",
                        "hash": "be2477f129a827a656c0b76392edbfa4b10568341786837b5f67ba7a25abb359",
                        "image_height": "",
                        "image_width": "",
                        "size_bytes": "141",
                        "size_compressed": "146",
                        "state": "available",
                        "sys_created_by": "admin",
                        "sys_created_on": "2026-01-27 19:29:41",
                        "sys_id": "33c38e4483b6f210f28198c6feaad3c1",
                        "sys_mod_count": "1",
                        "sys_tags": "",
                        "sys_updated_by": "system",
                        "sys_updated_on": "2026-01-27 19:29:43",
                        "table_name": "change_request",
                        "table_sys_id": "34a34e0883f6f210f28198c6feaad380"
                    }
                ],
                "backout_plan": "",
                "business_duration": "",
                "business_service": "281a4d5fc0a8000b00e4ba489a83eedc",
                "cab_date_time": "",
                "cab_delegate": "",
                "cab_recommendation": "",
                "cab_required": "false",
                "calendar_duration": "",
                "category": "Software",
                "change_plan": "",
                "chg_model": "007c4001c343101035ae3f52c1d3aeb2",
                "close_code": "",
                "close_notes": "",
                "closed_at": "",
                "closed_by": "",
                "cmdb_ci": "0c43d5e2c61122750182c132a8b9e2d9",
                "comments": "",
                "comments_and_work_notes": "",
                "company": "",
                "conflict_last_run": "",
                "conflict_status": "Not Run",
                "contact_type": "",
                "contract": "",
                "copied_from": "",
                "correlation_display": "",
                "correlation_id": "",
                "delivery_plan": "",
                "delivery_task": "",
                "description": "--- Managed by r-able.com hyperautomation (TurboCharged, V2.2.2). Do not change manually. Do not deviate. ---\r\n--- This Change Request adds a new deployment to an already installed GoldenGate hub binaries ---\r\n--- Enter the required parameters on the right side of the \":\" below ---\r\nRun dcr\r\n",
                "due_date": "",
                "end_date": "",
                "escalation": "0",
                "expected_start": "",
                "follow_up": "",
                "group_list": "",
                "impact": "low",
                "implementation_plan": "",
                "justification": "",
                "knowledge": "false",
                "location": "",
                "made_sla": "true",
                "number": "CHG0030971",
                "on_hold": false,
                "on_hold_reason": "",
                "on_hold_task": "",
                "opened_at": "2026-01-27 19:28:56",
                "opened_by": "6816f79cc0a8016401c5a33be04be441",
                "order": "",
                "outside_maintenance_schedule": "false",
                "parent": "",
                "phase": "requested",
                "phase_state": "open",
                "priority": "low",
                "production_system": "false",
                "reason": "",
                "reassignment_count": "0",
                "requested_by": "6816f79cc0a8016401c5a33be04be441",
                "requested_by_date": "",
                "review_comments": "",
                "review_date": "",
                "review_status": "",
                "risk": "",
                "risk_impact_analysis": "",
                "route_reason": "",
                "scope": "3",
                "service_offering": "04b89573835e7210f28198c6feaad3f0",
                "short_description": "DCR",
                "sla_due": "",
                "start_date": "",
                "state": "new",
                "std_change_producer_version": "",
                "sys_class_name": "change_request",
                "sys_created_by": "admin",
                "sys_created_on": "2026-01-27 19:29:50",
                "sys_domain": "global",
                "sys_domain_path": "/",
                "sys_id": "34a34e0883f6f210f28198c6feaad380",
                "sys_mod_count": "3",
                "sys_tags": "",
                "sys_updated_by": "admin",
                "sys_updated_on": "2026-01-27 21:11:45",
                "task_effective_number": "CHG0030971",
                "test_plan": "",
                "time_worked": "",
                "type": "normal",
                "unauthorized": "false",
                "universal_request": "",
                "upon_approval": "proceed",
                "upon_reject": "cancel",
                "urgency": "low",
                "user_input": "",
                "watch_list": "",
                "work_end": "",
                "work_notes": "",
                "work_notes_list": "",
                "work_start": ""
            }
        ],
        "warnings": [
            "Encountered unknown value  while mapping field risk.",
            "Encountered unknown value Software while mapping field category."
        ]
    }
}

TASK [Cache the CR SYS_ID 2026-02-01T10:15:08Z | itsm_dcr] ****************************************************************************************************************************
ok: [localhost]

TASK [2. Download the Change Request attachment with its hardcoded SYS_ID from Step 1 | itsm_dcr] *************************************************************************************
changed: [localhost]

TASK [2.1 Debug the hardcoded-SYS-ID attachment download details | itsm_dcr] **********************************************************************************************************
ok: [localhost] => {
    "dcr_new_cr_attachment_out": {
        "changed": true,
        "failed": false,
        "record": {
            "elapsed": 0.29,
            "msg": "OK",
            "size": 141,
            "status_code": 200
        }
    }
}

PLAY RECAP ****************************************************************************************************************************************************************************
localhost                  : ok=7    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
1 Like

image

Nice one! @clerambeau370

Glad you got it working, please could you mark it as the solution so others know this is resolved. :slightly_smiling_face: Thanks!

1 Like