繁体   English   中英

从 nautobot 搜索 IP 地址并在字典中搜索

[英]Search IP adresses from nautobot and search inside dictionnary

我目前正在为 ansible 使用 networktocode 的 autobot 模块,并且我正在尝试从已注册的 DNS 条目中获取 ip。

为此,我使用 api 提取所有已注册 ip 地址的列表,并在结果中进行循环搜索。

- 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 }}"

这是一个非常冗长和危险的解决方案,有更好的方法吗?

通过提取 IP 地址或在结果中进行更好的搜索。

提前致谢。

我希望利用 GraphQL 接口和查找来为您处理这个问题。 随着 Nautobot 针对任何获取请求转移到 GraphQL 也将有助于加快速度。 这是使用 Nautobot 的演示页面的响应。

---
- 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 }}"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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