# fetching the right output

**URL:** <https://forum.ansible.com/t/fetching-the-right-output/29480>\
**Category:** Ansible Project\
**Created:** [January 21, 2019, 9:08am UTC](https://forum.ansible.com/t/fetching-the-right-output/29480 "2019-01-21T09:08:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vino\_Thiyagu](https://avatars.discourse-cdn.com/v4/letter/v/5f9b8f/32.png) [@Vino\_Thiyagu](https://forum.ansible.com/u/Vino_Thiyagu)\
**Post date:** [January 21, 2019, 9:08am UTC](https://forum.ansible.com/t/fetching-the-right-output/29480/1 "2019-01-21T09:08:31Z")

</div>

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}}”

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

```

output is:

ok: [localhost] =\> {  
“msg”: “\<generator object \_select\_or\_reject at 0x7f11a00bf2d0\>”

full output:

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

```

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

```

```auto
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](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](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](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”,

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 21, 2019, 3:04pm UTC](https://forum.ansible.com/t/fetching-the-right-output/29480/2 "2019-01-21T15:04:58Z")

</div>

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 }}

---

<div class="post-metadata">

**Author:** ![Vino\_Thiyagu](https://avatars.discourse-cdn.com/v4/letter/v/5f9b8f/32.png) [@Vino\_Thiyagu](https://forum.ansible.com/u/Vino_Thiyagu)\
**Post date:** [March 8, 2019, 8:17am UTC](https://forum.ansible.com/t/fetching-the-right-output/29480/3 "2019-03-08T08:17:13Z")

</div>

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](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](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](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](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](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](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](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]#

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 9, 2019, 10:06pm UTC](https://forum.ansible.com/t/fetching-the-right-output/29480/4 "2019-03-09T22:06:00Z")

</div>

'^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 $.
