1

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]
  • 2
    What is the problem? Apart from the fact that hosts is a reserved variable name. – Konstantin Suvorov Apr 11 '18 at 13:50
  • oh, I want to pass domain name then playbook will resolve its ip and show in which server that domain belongs to. – Err0rr Apr 11 '18 at 16:46
  • 1
    Why do you want this? – 030 Apr 11 '18 at 18:32
  • 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. – Err0rr Apr 12 '18 at 2:04
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
  • 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. – Err0rr May 3 '18 at 5:43
  • I'm not at my workstation, but domain_location wouldn't be set so you shouldn't get the last debug statement. – Ghost May 3 '18 at 16:29
  • 1
    If the answer solved your problem, please consider accepting it to show others that your question has been answered. – Ghost May 16 '18 at 15:25
  • 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. – Err0rr May 16 '18 at 15:51

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.