<p>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!</p>

<p><strong>roles/test/vars/main.yml</strong></p>

<pre><code>---
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]
</code></pre>

<p><strong>roles/test/tasks/main.yml</strong></p>

<pre><code>---
# 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
</code></pre>

<p><strong>Example Run</strong>
With domain.com pointing to 127.0.0.12</p>

<pre><code>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] =&gt; {
    "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] =&gt; (item=gh4)
skipping: [localhost] =&gt; (item=gh5)
ok: [localhost] =&gt; (item=gh6)

TASK [test : debug] *************************************************************************************************
ok: [localhost] =&gt; {
    "msg": "domain.com is in gh6.allserevers.com"
}

PLAY RECAP **********************************************************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=0
</code></pre>

CC BY-SA 4.0
2018-05-03T05:43:17.380Z
Thanks. yes I am looking for same. Will it handle ip which is not defined in gh4,gh5 and gh6? For example what if we pass 127.0.0.6. I will test it once I get free time.
Prakash
CC BY-SA 4.0
2018-05-03T16:29:06.973Z
I'm not at my workstation, but domain_location wouldn't be set so you shouldn't get the last debug statement.
Ghost
CC BY-SA 4.0
2018-05-16T15:25:42.047Z
If the answer solved your problem, please consider accepting it to show others that your question has been answered.
Ghost
CC BY-SA 4.0
2018-05-16T15:51:37.720Z
not actually. I tried it but it didn't worked as expected. may be needs modification. I will try it again and will mark as answer if it works.
Prakash
CC BY-SA 4.0
2018-05-03T02:57:14.020Z
2018-05-03T02:57:14.020Z
Ghost
<p>Following is the desire output:</p>

<pre><code>domain.com is in gh4.allserevers.com
</code></pre>

<p>The idea is to pass domain name ie <strong>domain.com</strong> in playbook and playbook will lookup its <strong>A record</strong> then check it against the predefined variable. If ip is found in variable then it will display the corresponding key.</p>

<p>Here is my ansible-playbook role:</p>

<p><strong>roles/test/tasks/main.yml</strong></p>

<pre><code>    ---
    - name: Simple A record (IPV4 address) lookup for domain.com
      set_fact:
         IP: "{{ lookup('dig', 'domain.com.')}}"
      register: output
      tags: test

    - name: checking if ip belongs to gh4
      debug:
        msg: "domain.com is in gh4"
      when:
        - IP in hosts.gh4
      tags: test
</code></pre>

<p><strong>roles/test/vars/main.yml</strong></p>

<pre><code>    ---
    hosts:
      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]
</code></pre>

CC BY-SA 3.0
2018-04-11T13:50:22.580Z
What is the problem? Apart from the fact that `hosts` is a reserved variable name.
Konstantin Suvorov
CC BY-SA 3.0
2018-04-11T16:46:05.493Z
oh, I want to pass domain name then playbook will resolve its ip and show in which server that domain belongs to.
Prakash
CC BY-SA 3.0
2018-04-11T18:32:34.557Z
Why do you want this?
030
CC BY-SA 3.0
2018-04-12T02:04:17.530Z
it is just a small portion of a big playbook. You can think it as a small project or also think as I am practicing.
Prakash
<ansible>
How do I get dict key from ansible playbook variable?
Prakash
# How do I get dict key from ansible playbook variable?

(Asked by: username_0 on 2018-04-11)

Following is the desire output:
domain.com is in gh4.allserevers.com

The idea is to pass domain name ie domain.com in playbook and playbook will lookup its A record then check it against the predefined variable. If ip is found in variable then it will display the corresponding key.
Here is my ansible-playbook role:
roles/test/tasks/main.yml
    ---
    - name: Simple A record (IPV4 address) lookup for domain.com
      set_fact:
         IP: "{{ lookup('dig', 'domain.com.')}}"
      register: output
      tags: test

    - name: checking if ip belongs to gh4
      debug:
        msg: "domain.com is in gh4"
      when:
        - IP in hosts.gh4
      tags: test

roles/test/vars/main.yml
    ---
    hosts:
      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]

**Tags:** ansible

> **Comments:**
> **username_1**: What is the problem? Apart from the fact that `hosts` is a reserved variable name.  
> **username_0**: oh, I want to pass domain name then playbook will resolve its ip and show in which server that domain belongs to.  
> **username_2**: Why do you want this?  
> **username_0**: it is just a small portion of a big playbook. You can think it as a small project or also think as I am practicing.  

---

## 1 answers

### Answer by username_3 on 2018-05-03. Score: 2

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

> **Comments:**
> **username_0**: Thanks. yes I am looking for same. Will it handle ip which is not defined in gh4,gh5 and gh6? For example what if we pass 127.0.0.6. I will test it once I get free time.  
> **username_3**: I'm not at my workstation, but domain_location wouldn't be set so you shouldn't get the last debug statement.  
> **username_3**: If the answer solved your problem, please consider accepting it to show others that your question has been answered.  
> **username_0**: not actually. I tried it but it didn't worked as expected. may be needs modification. I will try it again and will mark as answer if it works.  

---

