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

1,683 views
Skip to first unread message

Alberto Jimenez Lozano

unread,
Oct 29, 2018, 3:36:28 AM10/29/18
to Ansible Project
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:
       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 }}"

Foss Geek

unread,
Oct 29, 2018, 7:06:48 AM10/29/18
to Ansible Project

It works to me.

---

- 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

Kai Stian Olstad

unread,
Oct 29, 2018, 9:23:15 AM10/29/18
to ansible...@googlegroups.com
On Monday, 29 October 2018 12:06:47 CET Foss Geek wrote:
>
> It works to me.

Because you use double quotes.
You need to use double quotes instead of single quotes.


--
Kai Stian Olstad


Mohan L

unread,
Oct 31, 2018, 1:48:02 PM10/31/18
to Ansible Project

It works to me.  


---

- 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




On Monday, October 29, 2018 at 1:06:28 PM UTC+5:30, Alberto Jimenez Lozano wrote:
Reply all
Reply to author
Forward
0 new messages