Use a loop with_items into a body command of uri ansible module

I use a loop with_items, but I want to put the variable into a body of a json and it doesn´t works.

  • name: Associate subscriptions ak-{{tenant}}-RHEL_7
    uri:
    url: “https://xxxxxxxx/katello/api/activation_keys/{{ak_id[0]}}/add_subscriptions”
    user: “{{user}}”
    password: “{{password}}”
    method: PUT
    return_content: yes
    force_basic_auth: yes
    validate_certs: no
    body_format: json
    body: ‘{“subscription_id”: “{{item}}” }’
    with_items:
  • 340
  • 343
    status_code: [200,201,202,204,301,401]

What is it wrong? → body: ‘{“subscription_id”: “{{item}}” }’

The task includes an option with an undefined variable. The error was: ‘item’ is undefined

The error appears to have been in ‘/awx/scripts/scm/ca/satellite/ak/ca_sat_ak_put_subscriptions.yml’: line 48, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  • name: Associate subscriptions ak-{{tenant}}-RHEL_7-{{tecnologia}}_LB
    ^ here
    We could be wrong, but this one looks like it might be an issue with
    missing quotes. Always quote template expression brackets when they
    start a value. For instance:

with_items:

  • {{ foo }}

Should be written as:

with_items:

  • “{{ foo }}”

It works to me.

It works to me.

It works to me.

Because you use double quotes.

---

- hosts: all
  tasks:
    - name: Generate responce with given status code
      uri:
        url: "https://httpbin.org/post"
        method: POST
        body_format: json
        body: "{ \"codes\": {{ item }} }"
        return_content: yes
      with_items:
        - 100
        - 200
      register: apiresponce

>
> I use a loop with_items, but I want to put the variable into a body of a
> json and it doesn´t works.
>
>
> - name: Associate subscriptions ak-{{tenant}}-RHEL_7
> uri:
> url: "
> https://xxxxxxxx/katello/api/activation_keys/\{\{ak\_id\[0\]\}\}/add_subscriptions
> "
> user: "{{user}}"
> password: "{{password}}"
> method: PUT
> return_content: yes
> force_basic_auth: yes
> validate_certs: no
> body_format: json
> body: '{"subscription_id": "{{item}}" }'

You need to use double quotes instead of single quotes.