user1858059
user1858059

Reputation: 103

Ansible to loop until result is not empty

Im trying to create a simple Ansible script that loops through a command until it returns an IP. Here is what I have

- name: Get Shipa API address
  shell: kubectl --namespace=shipa-system get svc shipa-ingress-nginx -o jsonpath="{.status.loadBalancer.ingress[0].ip}"
  environment:
    TERM: dumb      
  register: shipa_api 
  until: shipa_api.stdout | lenght > 3
  retries: 5
  delay: 90

- set_fact:
    shipa_apiaddr: '{{ shipa_api.stdout | trim }}' 

This is surely not working and one point to note is that the command above might return a simple % when it is empty, so I need to make sure Ansible only moves on to the next task when there is a real IP or more than X amount of characters

How can I do this? Any help is much appreciated

Upvotes: 1

Views: 1557

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39099

There is a filter called ipaddr for this.

So you could go:

- name: Get Shipa API address
  shell: kubectl --namespace=shipa-system get svc shipa-ingress-nginx -o jsonpath="{.status.loadBalancer.ingress[0].ip}"
  environment:
    TERM: dumb      
  register: shipa_api 
  until: shipa_api.stdout | ipaddr
  retries: 5
  delay: 90

Upvotes: 1

Related Questions