Skip to content

netops.ansible — Ansible Integration

Dynamic inventory script, custom modules, and Ansible roles for netops-toolkit integration.


netops.ansible.dynamic_inventory

Ansible dynamic inventory provider.

Builds an Ansible-compatible inventory from the netops YAML inventory file, auto-generating vendor_*, site_*, and role_* groups. Supports CredentialVault integration and file-based caching.

CLI usage:

ansible-playbook -i netops/ansible/dynamic_inventory.py site.yml
python netops/ansible/dynamic_inventory.py --list
python netops/ansible/dynamic_inventory.py --host core-rtr-01
python netops/ansible/dynamic_inventory.py --refresh-cache

dynamic_inventory

Ansible dynamic inventory script backed by a netops inventory file.

Usage (as a standalone script)::

# List all hosts and groups
python -m netops.ansible.dynamic_inventory --list

# Get host variables for a specific host
python -m netops.ansible.dynamic_inventory --host router1

# Specify inventory file (default: inventory.yaml)
python -m netops.ansible.dynamic_inventory --list --inventory /path/to/inv.yaml

# Use a vault file for per-device credentials
python -m netops.ansible.dynamic_inventory --list --vault ~/.netops/vault.yaml

# Control the on-disk cache
python -m netops.ansible.dynamic_inventory --list --cache-ttl 600
python -m netops.ansible.dynamic_inventory --list --no-cache
python -m netops.ansible.dynamic_inventory --list --refresh-cache

Configure via environment variables:

  • NETOPS_INVENTORY — path to the inventory file
  • NETOPS_VAULT — path to the vault file
  • NETOPS_INVENTORY_CACHE — path to the JSON cache file

When used as an Ansible inventory source pass the script path with -i::

ansible-playbook -i path/to/dynamic_inventory.py site.yml

The _meta.hostvars structure is always populated so Ansible does not need to issue individual --host calls.

Auto-generated groups

In addition to explicit device groups defined in the inventory file the inventory builder automatically creates the following groups from device metadata:

  • vendor_<vendor> — e.g. vendor_cisco_ios, vendor_nokia_sros
  • site_<site> — e.g. site_dc1
  • role_<role> — e.g. role_spine, role_leaf, role_core
Cache

Results are cached in a JSON file (default: ~/.netops/inventory_cache.json) with a configurable TTL (default 300 s). Pass --no-cache to skip caching entirely or --refresh-cache to force a rebuild.

Functions:
build_inventory
build_inventory(inventory_path: str, vault_path: str | None = None, cache_path: str | None = None, cache_ttl: int = _DEFAULT_CACHE_TTL, no_cache: bool = False, refresh_cache: bool = False) -> dict

Return an Ansible JSON inventory dict from a netops inventory file.

Parameters:

Name Type Description Default
inventory_path str

Path to the netops YAML/JSON inventory file.

required
vault_path str | None

Optional path to a :class:~netops.core.vault.CredentialVault file. When provided and NETOPS_VAULT_PASSWORD is set, per-device credentials are injected into the host vars.

None
cache_path str | None

Path for the JSON cache file. Defaults to ~/.netops/inventory_cache.json (or $NETOPS_INVENTORY_CACHE).

None
cache_ttl int

Cache time-to-live in seconds (default 300).

_DEFAULT_CACHE_TTL
no_cache bool

When True, skip reading from and writing to the cache.

False
refresh_cache bool

When True, ignore the existing cache and always rebuild.

False
get_host_vars
get_host_vars(inventory_path: str, hostname: str, vault_path: str | None = None, cache_path: str | None = None, cache_ttl: int = _DEFAULT_CACHE_TTL, no_cache: bool = False, refresh_cache: bool = False) -> dict

Return variables for a single host.

main
main(argv: list[str] | None = None) -> int

CLI entry point for the Ansible dynamic inventory script.


netops.ansible.modules.netops_command

Ansible module: run arbitrary commands on network devices via netops transport.

netops_command

Ansible module: netops_command.

Thin Ansible wrapper around netops utilities. Sends one or more commands to a network device using the netmiko connection backend and returns the raw output.

Options

host IP address or FQDN of the target device. vendor Device vendor/OS type (netmiko device_type). username SSH username. password SSH password. port TCP port, default 22. commands List of CLI commands to execute. wait_for Optional list of output strings to wait for before returning (passed to netmiko expect_string).

Return values

output List of raw command output strings, one per command. stdout Concatenated output of all commands.

Examples:

.. code-block:: yaml

- name: Run show commands
  netops_command:
    host: "{{ ansible_host }}"
    vendor: cisco_ios
    username: admin
    password: "{{ vault_password }}"
    commands:
      - show version
      - show interfaces status

- name: Capture BGP summary
  netops_command:
    host: "{{ ansible_host }}"
    vendor: cisco_ios
    username: admin
    password: "{{ vault_password }}"
    commands:
      - show bgp summary
  register: bgp_raw

- name: Parse BGP output
  set_fact:
    bgp_peers: "{{ bgp_raw.output[0] | netops_parse_bgp }}"
Functions:
run_module
run_module() -> None

Entry point called by Ansible.


netops.ansible.modules.netops_facts

Ansible module: gather structured facts from network devices.

Returns ansible_facts.netops with categories: health, interfaces, bgp, vlans.

Module options: - gather_subsetall | health | interfaces | bgp | vlans (default: all)

netops_facts

Ansible module: netops_facts.

Collect structured device facts from a network device using netops utilities and return them as Ansible facts (ansible_facts).

Options

host IP address or FQDN of the target device. Defaults to {{ inventory_hostname }} when omitted. vendor Device vendor/OS type (e.g. cisco_ios, nokia_sros). Maps to netmiko device_type. username SSH username. password SSH password (mark no_log: true in your playbook). port TCP port. Default 22. transport ssh (default) or telnet. gather List of fact categories to collect. Supported values: health, interfaces, bgp, vlans, all. Default is all. inventory Path to a netops inventory YAML/JSON file. When provided, device connection details are read from it (host must still name the inventory hostname).

Return values

ansible_facts.netops Dict with a key per gathered category, e.g.::

    ansible_facts:
      netops:
        health:
          cpu_percent: 12
          memory_percent: 45
        interfaces:
          - name: GigabitEthernet0/0
            status: up
            protocol: up

Examples:

.. code-block:: yaml

- name: Collect device facts
  netops_facts:
    host: "{{ ansible_host }}"
    vendor: cisco_ios
    username: admin
    password: "{{ vault_password }}"
    gather:
      - health
      - interfaces

- name: Show CPU usage
  debug:
    var: ansible_facts.netops.health.cpu_percent
Functions:
run_module
run_module() -> None

Entry point called by Ansible.