# Retrieve geolocation data of a host’s IP address - name: Get IP geolocation data ansible.builtin.shell: whois {{ (my_droplet.data.droplet.networks.v4 | selectattr(‘type’, ‘equalto’, ‘public’)).0.ip_address | default(‘’, true) }} | grep Country register: country_info - set_fact: country_info_loop: “{{country_info}}”
- name: Delete if not our region community.digitalocean.digital_ocean_droplet: state: absent oauth_token: “{{oauth_token}}” id: “{{ my_droplet.data.droplet.id }}” project: project01 wait_timeout: 500 when: “{{country_info_loop}} != {{region}}”
include_tasks didn’t works with loop/until etc. Because loop can’t find my variables. And i don’t know how i can loop this all (
You’re making us assume a lot because we don’t know what ‘{{ my_droplet }}’ or '{{ region }}" looks like.
If I throw a public IP address that’s from near me (I’m in the United States) into ‘whois’, grep for ‘Country’ and register the result, I get the following. - name: Get IP geolocation data ansible.builtin.shell: whois ‘152.2.22.62’ | grep Country register: country_info # Drop this useless ‘set_fact’ task: # - set_fact: # country_info_loop: “{{country_info}}”
- name: show country_info ansible.builtin.debug: msg: “{{ country_info }}”
Down below, you compare when: “{{country_info_loop}} != {{region}}”. As I said, we have no idea what region look like, but it’s extremely unlikely to look like the ‘msg’ above. At the very least, you’ll need to examine specific fields in your registered variable, something like “country_info[‘stdout_lines’]|first”. But that’s unlikely to be sufficient unless your ‘{{region}}’ also came from ‘whois’.
include_tasks can be made to work, but first you need to understand your own data.