HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How do I get dict key from ansible playbook variable?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
dictplaybookgethowfromvariableansiblekey

Problem

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]

Solution

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

Code Snippets

---
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]
---
# 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
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

Context

StackExchange DevOps Q#3859, answer score: 2

Revisions (0)

No revisions yet.