Iterating through hosts until a successful response is received

40 views
Skip to first unread message

Pete Ahearn

unread,
Aug 3, 2015, 4:24:21 PM8/3/15
to Ansible Project
Hi everyone:

I'm new to Ansible and am hoping there's an easy answer for this. I have a playbook which needs to iterate through a list of IP addresses POSTing a request to each via the uri module, but *only until* the first successful response is received. Once a 200 is returned I don't want it to continue. I've done the following but it hits all three endpoints even if the second one returns 200:

---
- hosts: 192.168.1.2
  vars:
    my_addrs:
      - "192.168.1.100"
      - "localhost"
      - "192.168.1.2"
  become: yes
  become_method: sudo
  tasks:
    - name: Query addresses until success
      with_items: my_addrs
      uri:
        register: uri_resp
        until: uri_resp.status == 200
        retries: 5
        url: "http://{{ item }}/_showSomething"
        method: POST
        body: "{\"foo\":\"bar\"}"
        body_format: json
        HEADER_Content-Type: "application/json"

...here I'm trying to iterate using "with_items", and trying to break using "until", but I haven't found an example of using these two together (let alone with the uri module), so I'm wondering what the right way of doing this is.

Thanks in advance for any insight!

-p
Reply all
Reply to author
Forward
0 new messages