If I understand your question correctly you want to pass in a domain to find what could be a physical location or data center. I hope this is helps you!
roles/test/vars/main.yml
---
domains:
gh4: [127.0.0.1,127.0.0.2,127.0.0.3,127.0.0.4,127.0.0.5]
gh5: [127.0.0.7,127.0.0.8]
gh6: [127.0.0.10,127.0.0.11,127.0.0.12]
roles/test/tasks/main.yml
---
# Make sure we have a domain passed in
- name: Ensure domain is defined
assert: { that: domain is defined }
# Obtain the IP
- name: Simple A record (IPV4 address) lookup for {{domain}}
set_fact:
IP: "{{ lookup('dig', '+short', '{{domain}}')}}"
register: output
tags: test
# Check if the IP belongs to any location
- name: Checking location of IP
set_fact:
domain_location: "{{item}}"
when:
- IP in domains[item]
with_items: "{{domains}}"
- debug:
msg: "domain.com is in gh4.allserevers.com{{domain_location}}"
when: domain_location is defined
Example Run
With domain.com pointing to 127.0.0.12
root@DESKTOP-U4VN2CS:/mnt/c/ansible/roles# ansible-playbook ../playbooks/test.yml -e domain=domain.com
PLAY [127.0.0.1] ****************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
ok: [localhost]
TASK [test : Ensure domain is defined] ******************************************************************************
ok: [localhost] => {
"changed": false,
"msg": "All assertions passed"
}
TASK [test : Simple A record (IPV4 address) lookup for domain.com] **************************************************
ok: [localhost]
TASK [test : Checking location of IP] *******************************************************************************
skipping: [localhost] => (item=gh4)
skipping: [localhost] => (item=gh5)
ok: [localhost] => (item=gh6)
TASK [test : debug] *************************************************************************************************
ok: [localhost] => {
"msg": "domain.com is in gh6.allserevers.com"
}
PLAY RECAP **********************************************************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0
hosts
is a reserved variable name. – Konstantin Suvorov Apr 11 '18 at 13:50