fetching the right output

HI team,

I have a output as below. but still I need to pick the right value fro this output. I need only the change no as in record field, it working fine for single run but it failing when in loop. can some one help me. needed filed in “number”: “CHG0030428”, I will have multiple change no through out the debug module.
I tried with selectattr and map to it was not working

msg: “{{test.results.record| selectattr(‘number’,‘match’,‘^CH$’)}}”
and
msg: “{{test.record| selectattr(‘number’,‘match’,‘^CH$’)|list}}”

msg: "{{test.results.record| selectattr('number','match','^CH$')}}"

output is:

ok: [localhost] => {
“msg”: “<generator object _select_or_reject at 0x7f11a00bf2d0>”

full output:

ok: [localhost] => {
    "msg": "<generator object _select_or_reject at 0x7f11a00bf2d0>"
ok: [localhost] => {
    "msg": "<generator object _select_or_reject at 0x7f11a00bf2d0>"
ok: [localhost] => {
    "msg": "<generator object _select_or_reject at 0x7f11a00bf2d0>"

TASK [servicenow : debug] *****************************************************************************************************************************************************
ok: [localhost] => {
“msg”: {
“changed”: true,
“msg”: “All items completed”,
“results”: [
{
“_ansible_ignore_errors”: null,
“_ansible_item_label”: [
“DEV_4”,
“Acrobat”,
“2”,
“3”,
“”,
“2018-01-02 14:00:00”,
“Network”,
“David Loo”
],
“_ansible_item_result”: true,
“_ansible_no_log”: false,
“_ansible_parsed”: true,
“changed”: true,
“failed”: false,
“instance”: “dev71301”,
“invocation”: {
“module_args”: {
“attachment”: null,
“data”: {
“assigned_to”: “David Loo”,
“assignment_group”: “Network”,
“cmdb_ci”: “Acrobat”,
“end_date”: “2018-01-02 14:00:00”,
“impact”: “3”,
“priority”: “2”,
“short_description”: “DEV_4 change has ben created succesfully with Acrobat configuration item via ansible”,
“start_date”: “”,
“work_notes”: “Testing with change creation”
},
“instance”: “dev71301”,
“lookup_field”: “number”,
“number”: null,
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“state”: “present”,
“table”: “change_request”,
“username”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”
}
},
“item”: [
“DEV_4”,
“Acrobat”,
“2”,
“3”,
“”,
“2018-01-02 14:00:00”,
“Network”,
“David Loo”
],
“lookup_field”: “number”,
“number”: null,
“record”: {
“active”: “true”,
“activity_due”: “”,
“additional_assignee_list”: “”,
“approval”: “not requested”,
“approval_history”: “”,
“approval_set”: “”,
“assigned_to”: {
“link”: “https://dev71301.service-now.com/api/now/table/sys_user/5137153cc611227c000bbd1bd8cd2007”,
“value”: “5137153cc611227c000bbd1bd8cd2007”
},
“assignment_group”: {
“link”: “https://dev71301.service-now.com/api/now/table/sys_user_group/287ebd7da9fe198100f92cc8d1d2154e”,
“value”: “287ebd7da9fe198100f92cc8d1d2154e”
},
“backout_plan”: “”,
“business_duration”: “”,
“business_service”: “”,
“cab_date”: “”,
“cab_delegate”: “”,
“cab_recommendation”: “”,
“change_plan”: “”,
“close_code”: “”,
“close_notes”: “”,
“closed_at”: “”,
“closed_by”: “”,
“cmdb_ci”: {
“link”: “https://dev71301.service-now.com/api/now/table/cmdb_ci/0c43d67fc61122750182c13265645d9d”,
“value”: “0c43d67fc61122750182c13265645d9d”
},
“comments”: “”,
“comments_and_work_notes”: “”,
“company”: “”,
“conflict_last_run”: “”,
“conflict_status”: “Not Run”,
“contact_type”: “”,
“correlation_display”: “”,
“correlation_id”: “”,
“delivery_plan”: “”,
“delivery_task”: “”,
“description”: “”,
“due_date”: “”,
“end_date”: “2018-01-02 14:00:00”,
“escalation”: “0”,
“expected_start”: “”,
“follow_up”: “”,
“group_list”: “”,
“impact”: “3”,
“implementation_plan”: “”,
“justification”: “”,
“knowledge”: “false”,
“location”: “”,
“made_sla”: “true”,
“number”: “CHG0030428”,
“on_hold”: “false”,
“on_hold_reason”: “”,
“on_hold_task”: “”,
“opened_at”: “2019-01-21 08:49:56”,

I don't understand what you are searching for but, to match in the number you need to user this

{{ test.results | selectattr('record.number','match','^CH$') | list }}

HI Kai,

sorry for the late reply,

I have tried above one, still its not working to me. It gives the empty string in output.

my playbook has below tasks:
register: test

  • debug:
    msg: “{{test}}”
  • debug:
    msg: "{{ test.results | selectattr(‘record.number’,‘match’,‘^CH$’) | list }}

where this is not fetching the correct chg no. could you please recheck it if possible. I just need to fetch this output “number”: “CHG0030052” even If I run it in loop.

TASK [servicenow : debug] *****************************************************************************************************************************************************
ok: [localhost] => {
“msg”: {
“changed”: true,
“msg”: “All items completed”,
“results”: [
{
“_ansible_ignore_errors”: null,
“_ansible_item_label”: [
“Dev123”,
“Acrobat”,
“3”,
“3”,
“2019-03-11 13:55:25”,
“2019-03-11 16:55:25”,
“Network”,
“David Loo”
],
“_ansible_item_result”: true,
“_ansible_no_log”: false,
“_ansible_parsed”: true,
“changed”: true,
“failed”: false,
“instance”: “dev56435”,
“invocation”: {
“module_args”: {
“attachment”: null,
“data”: {
“assigned_to”: “David Loo”,
“assignment_group”: “Network”,
“cmdb_ci”: “Acrobat”,
“end_date”: “2019-03-11 16:55:25”,
“impact”: “3”,
“priority”: “3”,
“short_description”: “Dev123 change has ben created succesfully with Acrobat configuration item via ansible”,
“start_date”: “2019-03-11 13:55:25”,
“work_notes”: “Testing with change creation”
},
“instance”: “dev56435”,
“lookup_field”: “number”,
“number”: null,
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“state”: “present”,
“table”: “change_request”,
“username”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”
}
},
“item”: [
“Dev123”,
“Acrobat”,
“3”,
“3”,
“2019-03-11 13:55:25”,
“2019-03-11 16:55:25”,
“Network”,
“David Loo”
],
“lookup_field”: “number”,
“number”: null,
“record”: {
“active”: “true”,
“activity_due”: “”,
“additional_assignee_list”: “”,
“approval”: “not requested”,
“approval_history”: “”,
“approval_set”: “”,
“assigned_to”: {
“link”: “https://dev56435.service-now.com/api/now/table/sys_user/5137153cc611227c000bbd1bd8cd2007”,
“value”: “5137153cc611227c000bbd1bd8cd2007”
},
“assignment_group”: {
“link”: “https://dev56435.service-now.com/api/now/table/sys_user_group/287ebd7da9fe198100f92cc8d1d2154e”,
“value”: “287ebd7da9fe198100f92cc8d1d2154e”
},
“backout_plan”: “”,
“business_duration”: “”,
“business_service”: “”,
“cab_date”: “”,
“cab_delegate”: “”,
“cab_recommendation”: “”,
“cab_required”: “false”,
“calendar_duration”: “”,
“category”: “Other”,
“change_plan”: “”,
“close_code”: “”,
“close_notes”: “”,
“closed_at”: “”,
“closed_by”: “”,
“cmdb_ci”: {
“link”: “https://dev56435.service-now.com/api/now/table/cmdb_ci/0c43d67fc61122750182c13265645d9d”,
“value”: “0c43d67fc61122750182c13265645d9d”
},
“comments”: “”,
“comments_and_work_notes”: “”,
“company”: “”,
“conflict_last_run”: “”,
“conflict_status”: “Not Run”,
“contact_type”: “”,
“correlation_display”: “”,
“correlation_id”: “”,
“delivery_plan”: “”,
“delivery_task”: “”,
“description”: “”,
“due_date”: “”,
“end_date”: “2019-03-11 16:55:25”,
“escalation”: “0”,
“expected_start”: “”,
“follow_up”: “”,
“group_list”: “”,
“impact”: “3”,
“implementation_plan”: “”,
“justification”: “”,
“knowledge”: “false”,
“location”: “”,
“made_sla”: “true”,
“number”: “CHG0030052”,
“on_hold”: “false”,
“on_hold_reason”: “”,
“on_hold_task”: “”,
“opened_at”: “2019-03-08 08:10:26”,
“opened_by”: {
“link”: “https://dev56435.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441”,
“value”: “6816f79cc0a8016401c5a33be04be441”
},
“order”: “”,
“outside_maintenance_schedule”: “false”,
“parent”: “”,
“phase”: “requested”,
“phase_state”: “open”,
“priority”: “3”,
“production_system”: “false”,
“reason”: “”,
“reassignment_count”: “0”,
“requested_by”: {
“link”: “https://dev56435.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441”,
“value”: “6816f79cc0a8016401c5a33be04be441”
},
“requested_by_date”: “”,
“review_comments”: “”,
“review_date”: “”,
“review_status”: “”,
“risk”: “3”,
“risk_impact_analysis”: “”,
“scope”: “3”,
“short_description”: “Dev123 change has ben created succesfully with Acrobat configuration item via ansible”,
“sla_due”: “”,
“start_date”: “2019-03-11 13:55:25”,
“state”: “-5”,
“std_change_producer_version”: “”,
“sys_class_name”: “change_request”,
“sys_created_by”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“sys_created_on”: “2019-03-08 08:10:26”,
“sys_domain”: {
“link”: “https://dev56435.service-now.com/api/now/table/sys_user_group/global”,
“value”: “global”
},
“sys_domain_path”: “/”,
“sys_domain”: {
“link”: “https://dev56435.service-now.com/api/now/table/sys_user_group/global”,
“value”: “global”
},
“sys_domain_path”: “/”,
“sys_id”: “01be59794f0033008fbde321a310c7b0”,
“sys_mod_count”: “0”,
“sys_tags”: “”,
“sys_updated_by”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“sys_updated_on”: “2019-03-08 08:10:26”,
“test_plan”: “”,
“time_worked”: “”,
“type”: “normal”,
“upon_approval”: “proceed”,
“upon_reject”: “cancel”,
“urgency”: “3”,
“user_input”: “”,
“watch_list”: “”,
“work_end”: “”,
“work_notes”: “”,
“work_notes_list”: “”,
“work_start”: “”
},
“table”: “change_request”
}
]
}
}
TASK [servicenow : debug] *****************************************************************************************************************************************************
ok: [localhost] => {
“msg”:
}
PLAY RECAP ********************************************************************************************************************************************************************
localhost : ok=13 changed=10 unreachable=0 failed=0
[root@instance-2 playbooks]#

'^CH$' is only matching two characert that start with C and end with H aka CH,
so if you are trying to match something that start with CH you will need to remove the $.