Skip to content

Instantly share code, notes, and snippets.

@orx57
Created March 15, 2022 13:29
Show Gist options
  • Select an option

  • Save orx57/89d67111167ae3901d1b6972f827fc63 to your computer and use it in GitHub Desktop.

Select an option

Save orx57/89d67111167ae3901d1b6972f827fc63 to your computer and use it in GitHub Desktop.
Ansible Query Netbox
---
# Exemple of installation for collection:
# ansible-galaxy collection install netbox.netbox
#
# Tested with collections:
# - name: netbox.netbox
# source: https://galaxy.ansible.com/
# type: galaxy
# version: 3.5.1
#
# Example of execution of this playbook:
# ansible-playbook netbox-query.yml \
# -e netbox_url=https://netbox.lan \
# -e netbox_api_token=********
- name: Netbox Query test
hosts: localhost
gather_facts: false
vars:
netbox_url: https://netbox.lan
netbox_api_url: "{{ netbox_url }}/api"
netbox_api_token: ''
tasks:
- name: Check if Netbox API variable is defined and not empty
ansible.builtin.assert:
that:
- (netbox_url is defined) and (netbox_url|length > 0)
- (netbox_api_url is defined) and (netbox_api_url|length > 0)
- (netbox_api_token is defined) and (netbox_api_token|length > 0)
fail_msg: "Netbox API credential must be valid"
success_msg: "Netbox API credential seems to be valid"
- name: Fetch information about all IP addresses (from netbox collection)
ansible.builtin.set_fact:
ipam_ipaddr: "{{ query('netbox.netbox.nb_lookup', 'ip-addresses',
api_endpoint=netbox_url,
token=netbox_api_token,
validate_certs=false) }}"
- name: Print ipam_ipaddr variable
ansible.builtin.debug:
var: ipam_ipaddr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment