Cant Get a Part of a long VAR in an Ansible Playbook

Hello. I need to get a Change Request number from a ServiceNow ticketing system. The playbook connects to ServiceNow, looks for a Change Request with the word “GoldenGate” in it. It finds it fine. It debugs the whole long record to the screen. But I only need the Change Request number, nothing else.

My guess is the name of the partial variable is “{{ new_ogg_cr_custom_out.records.number }}”.

But that fails:


TASK [INC, Cache the master CR for all playbooks to re-use | 2026-01-15T21:07:33Z] ***********************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => 
  msg: |-
    The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'number'
  
    The error appears to be in '/etc/ansible/playbooks/itsm_ogg_get_cr_info_004.yml': line 31, column 11, but may
    be elsewhere in the file depending on the exact syntax problem.
  
    The offending line appears to be:
  
  
            - name: INC, Cache the master CR for all playbooks to re-use | {{ ansible_date_time.iso8601 }}
              ^ here
    We could be wrong, but this one looks like it might be an issue with
    missing quotes. Always quote template expression brackets when they
    start a value. For instance:
  
        with_items:
          - {{ foo }}
  
    Should be written as:
  
        with_items:
          - "{{ foo }}"

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

[ansible_admin@lnx000 ansible]$ 

Here is the Playbook:

---
    - name: r-able.com Oracle GoldenGate add deployment process ID 2772.86
      hosts: localhost
      gather_facts: true
      vars_files:
        - vars/main.yml

      tasks:

        - name: Retrieve Change Requests that contain GoldenGate in its short description by using field query
          servicenow.itsm.change_request_info:
            instance:
              host: "{{ sn_instance}}"
              username: "{{ sn_username }}"
              password:  "{{ sn_password }}"
            query:
              - short_description: LIKE GoldenGate
#              - short_description: LIKE SAP
          register: new_ogg_cr_custom_out

########################### WRITE THE CR #######################################

#        - name: Debug the GoldenGate Change Request data new_ogg_cr_custom_out
#          ansible.builtin.debug:
#            msg: "new_ogg_cr_custom_out has content: {{ new_ogg_cr_custom_out | to_nice_json }}"

        - name: Debug the GoldenGate Change Request data new_ogg_cr_custom_out
          ansible.builtin.debug:
            var: new_ogg_cr_custom_out
  
        - name: INC, Cache the master CR for all playbooks to re-use | {{ ansible_date_time.iso8601 }} 
          ansible.builtin.set_fact:
            cr_number: "{{ new_ogg_cr_custom_out.records.number }}"
            cacheable: yes

        - name: INC, Write the main Incident {{ new_ogg_cr_custom_out.records.number }} VAR to CTRL+ node | itsm_ogg_resync.yml {{ ansible_date_time.iso8601 }}
          copy:
            content: "{{ new_ogg_cr_custom_out.records.number }}"
            dest: "{{ scratchpad }}/ogg_Add_Deploymnet_CR_sid.txt"
        #        ignore_errors: true
          delegate_to: localhost 

Here is the full output, including the debugged, full variable with the Change Request number: CHG0030884.

[ansible_admin@lnx000 ansible]$ ansible-playbook ./playbooks/itsm_ogg_get_cr_info_004.yml  -kK 
[WARNING]: Ansible is being run in a world writable directory (/etc/ansible), ignoring it as an ansible.cfg source. For more information see https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
SSH password: 
BECOME password[defaults to SSH password]: 
/usr/lib/python3.6/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.26.20) or chardet (3.0.4) doesn't match a supported version!
  RequestsDependencyWarning)

PLAY [r-able.com Oracle GoldenGate add deployment process ID 2772.86] ************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Retrieve Change Requests that contain GoldenGate in its short description by using field query] ****************************************************************************************************************************************************************
[WARNING]: Encountered unknown value  while mapping field risk.
[WARNING]: Encountered unknown value Software while mapping field category.
ok: [localhost]

TASK [Debug the GoldenGate Change Request data new_ogg_cr_custom_out] ************************************************************************************************************************************************************************************************
ok: [localhost] => 
  new_ogg_cr_custom_out:
    changed: false
    failed: false
    records:
    - active: 'true'
      activity_due: ''
      additional_assignee_list: ''
      approval: not requested
      approval_history: ''
      approval_set: ''
      assigned_to: 3c589a008334f210f28198c6feaad375
      assignment_group: db53580b0a0a0a6501aa37c294a2ba6b
      attachments: []
      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: |-
        Asset ID:
        Client:
        Application:
        Environment:
        Business Unit:
        Approval:
        Billing:
      due_date: ''
      end_date: ''
      escalation: '0'
      expected_start: ''
      follow_up: ''
      group_list: ''
      impact: low
      implementation_plan: ''
      justification: ''
      knowledge: 'false'
      location: ''
      made_sla: 'true'
      number: CHG0030884
      on_hold: false
      on_hold_reason: ''
      on_hold_task: ''
      opened_at: '2026-01-14 10:23:49'
      opened_by: 6816f79cc0a8016401c5a33be04be441
      order: ''
      outside_maintenance_schedule: 'false'
      parent: ''
      phase: requested
      phase_state: open
      priority: low
      production_system: 'false'
      reason: ''
      reassignment_count: '1'
      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: Add an Oracle GoldenGate Deployment
      sla_due: ''
      start_date: ''
      state: new
      std_change_producer_version: ''
      sys_class_name: change_request
      sys_created_by: admin
      sys_created_on: '2026-01-14 10:28:21'
      sys_domain: global
      sys_domain_path: /
      sys_id: b03a4d7f83da7210f28198c6feaad3a4
      sys_mod_count: '2'
      sys_tags: ''
      sys_updated_by: admin
      sys_updated_on: '2026-01-14 11:28:00'
      task_effective_number: CHG0030884
      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 [INC, Cache the master CR for all playbooks to re-use | 2026-01-15T21:07:33Z] ***********************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => 
  msg: |-
    The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'number'
  
    The error appears to be in '/etc/ansible/playbooks/itsm_ogg_get_cr_info_004.yml': line 31, column 11, but may
    be elsewhere in the file depending on the exact syntax problem.
  
    The offending line appears to be:
  
  
            - name: INC, Cache the master CR for all playbooks to re-use | {{ ansible_date_time.iso8601 }}
              ^ here
    We could be wrong, but this one looks like it might be an issue with
    missing quotes. Always quote template expression brackets when they
    start a value. For instance:
  
        with_items:
          - {{ foo }}
  
    Should be written as:
  
        with_items:
          - "{{ foo }}"

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

[ansible_admin@lnx000 ansible]$ 

Please help me with deriving ONLY the Change Request number to use as variable and writing it to disk for other plays to re-use.
Thank you in advance.
Nestor.

The records key is a list/array, and thus must be indexed. There are multiple ways to do it, but if you know there will always only be a single entry, you can do something quick like:

new_ogg_cr_custom_out.records.0.number

With the addition of the .0 in there to access the first record. Otherwise you would need to account for the ptoential of multiple records.

1 Like

That worked great, thank you, Sivel. This is just a POC, so I can do away with just one Change Request for the time being. But since you mentioned there are many ways to take care of multiple CRs, can you post some examples, please?
Thank you again!
Nestor.