简体   繁体   中英

Search IP adresses from nautobot and search inside dictionnary

I'm currently using networktocode's autobot module for ansible and i'm trying to get an ip from a registered DNS entry.

For that, i use the api to extract the list of all registered ip addresses and make a loop to search within the result.

- name: "Search existing IP"
  set_fact:
    nautobot_query: "{{ query('networktocode.nautobot.lookup', 'ip-addresses',
                  api_endpoint='https://nautobot-url.fr',
                  api_version='1.3',
                  token='12312312312312312313') }}"

- name: "Set_fact when hostname matches"
  ansible.builtin.set_fact:
    ip_query: "{{ item.value.address }}"
    ip_id: "{{ item.value.id }}"
  loop: "{{ nautobot_query }}"
  when: ("{{ item.value.dns_name }}" == "{{ vm_name }}")
  no_log: true 

- name: "Message info nautobot"
  debug:
    msg: "Got existing IP : {{ ip_query }}"

That's a very verbose and hazardous solution, is there a better way to do that?

Either by extracting the IP address or making a better search within the result.

Thanks in advance.

I would look to leverage the GraphQL interface and lookup to handle this for you. With Nautobot moving to GraphQL for any get request will help speed things along as well. This is the response with using Nautobot's demo page.

---
- name: "DEMO USING GRAPHQL ACTION MODULE"
  connection: local
  hosts: localhost
  gather_facts: no
  vars:
    querystr: |
        query {
          ip_addresses(dns_name: "ip-10-0-0-0.server.atl01.atc.nautobot.com") {
            address
          }
        }
  tasks:
    - name: "USE NAUTOBOT ACTION MODULE TO GET GRAPHQL RESPONSE"
      networktocode.nautobot.query_graphql:
        url: "{{ NAUTOBOT_URL }}"
        token: "{{ NAUTOBOT_TOKEN }}"
        query: "{{ querystr }}"
      register: graph_response

    - debug:
        msg: "{{ graph_response }}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM