ansibledig

Ansible - How to 'wait_for' DNS to be available or How using lookup('dig') with 'until'


I made something that is working but I look for something more 'elegant' ;)

- name: Wait for DNS propagation
  ansible.builtin.shell:
    cmd: host "{{ vm_fqdn }}"
  register: dns_result
  until: dns_result.rc == 0
  retries: 30
  delay: 10

But previously I tried with a lookup and the community.general.dig

- name: Wait for DNS propagation
  ansible.builtin.set_fact:
    dns_result: "{{ lookup('community.general.dig', vm_fqdn)}}"
  until: dns_result == vm_ip
  retries: 30
  delay: 10

Unfortunately, even I added a register I couldn't get the dns_result variable updated after multiple tries. It's like the lookup happens only once on the first iteration, but is not 're-triggered' on the next try. So maybe it is the behavior of lookup or something else, but I'm curious to know.


Solution

  • You're using the lookup at the wrong point. It should be directly in the until, so that it is evaluated each time.

    - name: Wait for DNS propagation
      debug:
        msg: waiting
      until: lookup('community.general.dig', vm_fqdn) == vm_ip
      retries: 30
      delay: 10