# Help with loop tasks Ansible

**URL:** <https://forum.ansible.com/t/help-with-loop-tasks-ansible/37586>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [January 27, 2023, 10:49am UTC](https://forum.ansible.com/t/help-with-loop-tasks-ansible/37586 "2023-01-27T10:49:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![voishunter](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@voishunter](https://forum.ansible.com/u/voishunter)\
**Post date:** [January 27, 2023, 10:49am UTC](https://forum.ansible.com/t/help-with-loop-tasks-ansible/37586/1 "2023-01-27T10:49:04Z")

</div>

I have a trouble with my tasks and can’t find solution. Maybe u can help me) I need to loop this tasks if

**“{{country\_info\_loop}} != {{region}}”**

but if

**“{{country\_info\_loop}} == {{region}}”**

then stop it.

**-**  **name: Start Check…**  
**block:**  
**- name: Create a new Droplet**  
**community.digitalocean.digital\_ocean\_droplet:**  
**state: present**  
**oauth\_token: “{{oauth\_token}}”**  
**name: “{{drop\_name}}”**  
**size: s-1vcpu-1gb**  
**region: “{{region}}”**  
**image: “ubuntu-18-04-x64”**  
**wait\_timeout: 500**  
**ssh\_keys:**  
**- “{{ ssh\_public\_key.data.ssh\_key.id }}”**  
**project: project01**  
**tags: “{{tag}}”**  
**register: my\_droplet**

**# 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 (

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 30, 2023, 12:07pm UTC](https://forum.ansible.com/t/help-with-loop-tasks-ansible/37586/2 "2023-01-30T12:07:12Z")

</div>

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 }}”**

**TASK [show country\_info] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\***  
**ok: [localhost] =\> {**  
**“msg”: {**  
**“changed”: true,**  
**“cmd”: “whois ‘152.2.22.62’ | grep Country”,**  
**“delta”: “0:00:00.209917”,**  
**“end”: “2023-01-30 06:53:46.436137”,**  
**“failed”: false,**  
**“msg”: “”,**  
**“rc”: 0,**  
**“start”: “2023-01-30 06:53:46.226220”,**  
**“stderr”: “”,**  
**“stderr\_lines”: [],**  
**“stdout”: “Country: US”,**  
**“stdout\_lines”: [**  
**“Country: US”**  
**]**  
**}**

**}**

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.
