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

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’, u’11’, u’12’, u’13’, u’14’, u’15’, u’16’, u’9’]'”}

`

Thoughts?

`

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

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’

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

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

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:

`

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

HTH, Werner

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
   [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:

---
  - name: Set facts
    set_fact:
      hostgroup_l:
      hostgroupnum_l:

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

  - name: create list of hostgroups
    set_fact:
      hostgroup_l: "{{hostgroup_l}} + ['{{item.hostGroup.hostGroupName}}']"
    with_items: "{{hostgroups.json.data}}"

This is a lot faster

   - name: create list of hostgroups
     set_fact:
       hostgroup_l: "{{ hostgroups.json.data | map(attribute='hostGroup.hostGroupName') | list }}"

  - name: create list of hostgroups ids
    set_fact:
      hostgroupnum_l: "{{hostgroupnum_l}} +
['{{item.hostGroup.hostGroupNumber}}']"
    with_items: "{{hostgroups.json.data}}"

   - name: create list of hostgroups ids
     set_fact:
       hostgroupnum_l: "{{ hostgroups.json.data | map(attribute='hostGroup.hostGroupNumber' | list }}"

So to get this to a string you can do this

   - name: String
     set_fact:
       hostgrpnum_s: >-
         {{ "'" ~ 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.

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

Try this:

        - name: create list of hostgroups ids
          set_fact:
            hostgroupnum_l: "{{ hostgroupnum_l|default() +
                                [item.hostGroup.hostGroupNumber] }}"
          loop: "{{ hostgroups.json.data }}"

Cheers,

  -vlado

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.
   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:

    test1: "'10','11','12','13','14','15','16','9'"
    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] => {
    "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("','") ~ "'" }}

The space is at the end of this line.

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