0

I want to filter this variable hp but it getting print as square bracket with "". how do i remove square bracket with "" just to get the only value. can someone please help here ? I was looking as regex but not able to find the exact syntax.

srv_make1: '{{ basic_params | from_json | json_query("servers.server_details[*].srv_make") }}'

Thanks

dejanualex
  • 3,872
  • 6
  • 22
  • 37
Bharat Gupta
  • 127
  • 1
  • 10

2 Answers2

2

I had something similar. Was getting

["abc"]

to overcome it, had to do 2 things:

  • append | [0] to the json query
  • use replace to get rid of "

so in your case instead of

srv_make1: '{{ basic_params | from_json | json_query("servers.server_details[*].srv_make") }}'

it will look something like

srv_make1: '{{ basic_params | from_json | json_query("servers.server_details[*].srv_make | [0]") | replace('\"','') }}'
Ilya
  • 591
  • 5
  • 6
0

Q: "How to remove square bracket & double quote?"

json_query always returns a list. It depends on the debug task how a list is displayed. For example

  vars:
    srv_make1: [a,b,c]
  tasks:
    - debug:
        var: srv_make1
    - debug:
        msg: "{{ srv_make1|to_yaml }}"

give

TASK [debug] ***
ok: [localhost] => {
    "srv_make1": [
        "a", 
        "b", 
        "c"
    ]
}

TASK [debug] ***
ok: [localhost] => {
    "msg": "[a, b, c]\n"
}

It's possible to use template and write the list into a file without brackets and quotes. For example the template

shell> cat srv_make1.conf.j2
{% for item in srv_make1 %}{{ item }} {% endfor %}

with the task

    - template:
        src: srv_make1.conf.j2
        dest: srv_make1.conf

gives

shell> cat srv_make1.conf
a b c
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63