'How to convert to valid JSON when running an Ansible command?

So, I have this list:

ip_range:
- "1.x.x.x/24"
- "2.x.x.x/24"

And I'm trying to pass it on to an AWS cli command as a valid JSON:

- name: list of IPs
  set_fact:
    allowed_cidrs: "{{ ip_range | ipaddr('network/prefix') }}"

- debug:
     msg: "{{ allowed_qualys_cidrs | to_json }}"

- name: send command
  command: >
    aws wafv2 update-ip-set
      --addresses "{{ allowed_cidrs | to_json }}"

That debug prints:

["1.x.x.x/24", "2.x.x.x/24"]

But what the command tries to send is:

"cmd": [
    "aws",
    "wafv2",
    "update-ip-set",
    "--addresses",
    "[1.x.x.x/24, 2.x.x.x/24]",
]

Which, because the double quotes are gone, is not a valid JSON. I already tried every possible approach with Ansible and Jinja2, but I can't figure out a way to send that command with a valid JSON.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source