# list seen as unicode, not sure why or how to correct

**URL:** <https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146>\
**Category:** Ansible Project\
**Created:** [August 27, 2019, 8:32pm UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146 "2019-08-27T20:32:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_Britton](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nicholas\_Britton](https://forum.ansible.com/u/Nicholas_Britton)\
**Post date:** [August 27, 2019, 8:32pm UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146/1 "2019-08-27T20:32:11Z")

</div>

I am building a list with output stored from a uri call:

`

- name: Get hostgroup info  
uri:  
url: “{{baseurl}}views/host-groups?$query=hostGroup.storageDeviceId%20eq%20’{{ui}}'%20and%20hostGroup.hostGroupName%20in%20[{{vmc02}}]”  
method: get  
validate\_certs: no  
headers:  
Authorization: “Session {{login.json.token}}”  
Accept: “application/json”  
Content-Type: “application/json”  
register: hostgroups

- name: create list of hostgroups ids  
set\_fact:  
hostgroupnum\_l: “{{hostgroupnum\_l}} + [‘{{item.hostGroup.hostGroupNumber}}’]”  
with\_items: “{{hostgroups.json.data}}”  
`

But when the data is debuged it gives me this difference if i look at one item or multiple:

`  
TASK [debug hostgroup list object] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: [  
“10”,  
“11”,  
“12”,  
“13”,  
“14”,  
“15”,  
“16”,  
“9”  
]  
}

TASK [debug hostgroupnumber output] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “u’10’”  
}

And this error when the next uri call attempts to use it, notice, how at the end i puts the list in unicode format, and the url does not know how to interpret that.

`  
TASK [list sessions for 886000428027 - ams] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“changed”: false, “connection”: “close”, “content”: “\n\n400 Bad Request\n\n

# Bad Request
\n

Your browser sent a request that this server could not understand.  
\n

\n\n”, “content\_length”: “226”, “content\_type”: “text/html; charset=iso-8859-1”, “date”: “Tue, 27 Aug 2019 20:26:28 GMT”, “elapsed”: 0, “msg”: “Status code was 400 and not [200]: HTTP Error 400: Bad Request”, “redirected”: false, “server”: “Apache”, “status”: 400, “url”: “[https://ip/ConfigurationManager/v1/views/lun-paths?$query=ldev.storageDeviceId%20eq%20’886000428027’%20and%20hostGroup.hostGroupNumber%20in%20’[u’10](https://ip/ConfigurationManager/v1/views/lun-paths?$query=ldev.storageDeviceId%20eq%20'886000428027'%20and%20hostGroup.hostGroupNumber%20in%20'%5Bu'10)’, u’11’, u’12’, u’13’, u’14’, u’15’, u’16’, u’9’]'”}

`

Thoughts?

`

---

<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:** [August 27, 2019, 8:51pm UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146/2 "2019-08-27T20:51:26Z")

</div>

You can use the to\_json filter to get "clean" json output.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Britton](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nicholas\_Britton](https://forum.ansible.com/u/Nicholas_Britton)\
**Post date:** [August 27, 2019, 9:46pm UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146/3 "2019-08-27T21:46:04Z")

</div>

That provides me this:

TASK [debug number on array after unuiqe] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “‘"10"’”  
}

how do i clean that up to only get the value 10 out of it?

The end result i am expecting is for the list to look like:

‘10’,‘11’,‘15’

---

<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:** [August 28, 2019, 7:19am UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146/4 "2019-08-28T07:19:54Z")

</div>

It's hard to give a solution since you have not provided all the information we need.  
But you can create a comma separated string with the join filter.

[https://jinja.palletsprojects.com/en/2.10.x/templates/#join](https://jinja.palletsprojects.com/en/2.10.x/templates/#join)

---

<div class="post-metadata">

**Author:** ![Nicholas\_Britton](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nicholas\_Britton](https://forum.ansible.com/u/Nicholas_Britton)\
**Post date:** [August 28, 2019, 11:50am UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146/5 "2019-08-28T11:50:32Z")

</div>

I tried to play with that some yesterday but did not have much luck. What can I provide you to help you help me?

---

<div class="post-metadata">

**Author:** ![Nicholas\_Britton](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nicholas\_Britton](https://forum.ansible.com/u/Nicholas_Britton)\
**Post date:** [August 28, 2019, 3:36pm UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146/6 "2019-08-28T15:36:06Z")

</div>

So i am trying to pull the hostgroupnumber since i need to use the list with a uri query that will use a in statement. The api documentation shows that the the variable to search in the in statement, needs to look like: “‘value1’,‘value2’,‘value3’”

Here is the set of tasks that i am using to generate the list variable:

`

---

<div class="post-metadata">

**Author:** ![Werner\_Flamme](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@Werner\_Flamme](https://forum.ansible.com/u/Werner_Flamme)\
**Post date:** [August 29, 2019, 9:07am UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146/7 "2019-08-29T09:07:43Z")

</div>

does it work if you use ...in%20'{{facttest|pow(1)}}'" ? This should  
translate facttest into a number.

HTH, Werner

---

<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:** [August 29, 2019, 11:46am UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146/8 "2019-08-29T11:46:02Z")

</div>

> So i am trying to pull the hostgroupnumber since i need to use the list  
> with a uri query that will use a in statement. The api documentation  
> shows that the the variable to search in the in statement, needs to look  
> like: "'value1','value2','value3'"

After reading thru everything I think it boils down to this.

You have list on int's  
&nbsp;&nbsp;&nbsp;[10, 11, 12, 13, 14, 15, 16, 9]

that you need to be a string looking like

'10','11','12','13','14','15','16','9'

And since it's going to be used in a url you probably need to url encode it too.

> Here is the set of tasks that i am using to generate the list variable:
> 
> ---  
> &nbsp;&nbsp;- name: Set facts  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostgroup\_l:   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostgroupnum\_l:

You don't need this if you do my proposed changes bellow.

> &nbsp;&nbsp;- name: create list of hostgroups  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostgroup\_l: "{{hostgroup\_l}} + ['{{item.hostGroup.hostGroupName}}']"  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{hostgroups.json.data}}"

This is a lot faster

&nbsp;&nbsp;&nbsp;- name: create list of hostgroups  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostgroup\_l: "{{ hostgroups.json.data | map(attribute='hostGroup.hostGroupName') | list }}"

> &nbsp;&nbsp;- name: create list of hostgroups ids  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostgroupnum\_l: "{{hostgroupnum\_l}} +  
> ['{{item.hostGroup.hostGroupNumber}}']"  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{hostgroups.json.data}}"

&nbsp;&nbsp;&nbsp;- name: create list of hostgroups ids  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostgroupnum\_l: "{{ hostgroups.json.data | map(attribute='hostGroup.hostGroupNumber' | list }}"

So to get this to a string you can do this

&nbsp;&nbsp;&nbsp;- name: String  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostgrpnum\_s: \>-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ "'" ~ hostgroupnum\_l | join("','") ~ "'" }}

Here I'm using muliline YAML so I avoid problem with qoutes since you don't need qoutes at the start and end.

And you can use urlencode filter to get proper url format.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Britton](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nicholas\_Britton](https://forum.ansible.com/u/Nicholas_Britton)\
**Post date:** [August 29, 2019, 1:48pm UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146/9 "2019-08-29T13:48:04Z")

</div>

Thank you. I see what your doing there and why. That makes since to me.

The part i dont follow 100% is the string part. Would you mind breaking that down for me? I also notice when i put that in my task list that the coloars for the next task change, does that indicate a problem?

 ![2019-08-29_8-31-34.png](https://us1.discourse-cdn.com/ansible/original/2X/d/dcfedaf4ec668ce7c55603e707bd2e15719bac7d.png)

I also notice the string puts a space at the end, and if i create two test stings one without the space and one with, it works without but not with:

`test1: "'10','11','12','13','14','15','16','9'" test2: "'10','11','12','13','14','15','16','9' "`

`  
TASK [String] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [debug - string] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: "‘10’,‘11’,‘12’,‘13’,‘14’,‘15’,‘16’,‘9’ "  
}

`

I also tried to add a replace filter in a couple of different ways, but they all seem to have a quoting problem that i am not seeing:

`

- name: String  
set\_fact:  
hostgrpnum\_s: \>-  
{{ “'” ~ hostgroupnum\_l | join(“‘,’”) ~ “'” }}

- name: debug - string  
debug:  
msg: “{{hostgrpnum\_s | replace(” “, “”) }}”

`

The offending line appears to be:

debug:  
msg: “{{hostgrpnum\_s | replace(” “, “”) }}”  
^ here

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 29, 2019, 2:13pm UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146/10 "2019-08-29T14:13:37Z")

</div>

Try this:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: create list of hostgroups ids  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostgroupnum\_l: "{{ hostgroupnum\_l|default() +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[item.hostGroup.hostGroupNumber] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ hostgroups.json.data }}"

Cheers,

&nbsp;&nbsp;-vlado

---

<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:** [August 29, 2019, 2:23pm UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146/11 "2019-08-29T14:23:38Z")

</div>

> Thank you. I see what your doing there and why. That makes since to me.
> 
> The part i dont follow 100% is the string part. Would you mind breaking  
> that down for me? I also notice when i put that in my task list that  
> the coloars for the next task change, does that indicate a problem?

I can try, hopefully is understandable.

The join filter take a list and join each element in the list with a string.  
So the list is joined by the string ','

The list  
[10, 11, 12]

becomes with join("','")

10','11','12

Tilde, ~ , is string concatenation in Jinja so it will be

"'" ~ "10','11','12" ~ "'"

That becomes

'10','11','12'

The color change is because the syntax highlighting is confused by all the quotes and multiline YAML, but there is no problem.  
It looks like you are using vim, if you install this plugin it will handle this without problem.  
&nbsp;&nbsp;&nbsp;[https://github.com/pearofducks/ansible-vim](https://github.com/pearofducks/ansible-vim)

> [image: 2019-08-29\_8-31-34.png]  
> I also notice the string puts a space at the end, and if i create two test  
> stings one without the space and one with, it works without but not with:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;test1: "'10','11','12','13','14','15','16','9'"  
> &nbsp;&nbsp;&nbsp;&nbsp;test2: "'10','11','12','13','14','15','16','9' "

The space I guess is because you have a space after }} in the set\_fact task.  
You can turn on highlighting of trailing whitespace in vim so they are more easily spotted.

> TASK [String]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [localhost]
> 
> TASK [debug - string]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [localhost] =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"msg": "'10','11','12','13','14','15','16','9' "  
> }
> 
> I also tried to add a replace filter in a couple of different ways, but  
> they all seem to have a quoting problem that i am not seeing:
> 
> &nbsp;&nbsp;- name: String  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostgrpnum\_s: \>-  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ "'" ~ hostgroupnum\_l | join("','") ~ "'" }}

The space is at the end of this line.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Britton](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nicholas\_Britton](https://forum.ansible.com/u/Nicholas_Britton)\
**Post date:** [August 29, 2019, 3:48pm UTC](https://forum.ansible.com/t/list-seen-as-unicode-not-sure-why-or-how-to-correct/31146/12 "2019-08-29T15:48:00Z")

</div>

Thank you! That helped me connect the dots. I found the extra space and its working like a champ. Thank you again!
