Collin Clark
Collin Clark

Reputation: 36

Missing variable in jinja2, no output with Ansible

I have an Ansible playbook that gathers facts from Cisco switches.

---
- hosts: switches
  gather_facts: False
  connection: network_cli

  vars:
    backup_root: ./configs

    cli:
      host: "{{ inventory_hostname }}"

  tasks:

    - name: ensure device folder is created
      file:
        path: "{{ backup_root }}/{{ inventory_hostname }}"
        state: directory

    - name: Gather all facts 
      cisco.ios.ios_facts:
        gather_subset: all

    - name: Serial Number
      debug: var=ansible_net_serialnum

    - name: Model
      debug: var=ansible_net_model

    - name: Hostname 
      debug: var=ansible_net_hostname

    - name: Version 
      debug: var=ansible_net_version

    - name: CDP 
      debug: var=ansible_net_neighbors

    - name: Config file
      debug: var=ansible_net_config

    - name: Stack SW Model Numbs
      debug: var=ansible_net_stacked_models
 
    - name: Stack SW Model Numbs
      debug: var=ansible_net_stacked_serialnums

    - name: Get VLAN Info
      cisco.ios.ios_command:
        commands: show vlan brief
      register: show_vlan

    - name: get timestamp
      command: date +%Y%m%d
      register: timestamp

    - name: Generate configuration files
      template:
         src=roles/discovery/templates/ios_switches.j2
         dest="{{ backup_root }}/{{ inventory_hostname }}/{{ inventory_hostname }}.txt" `

Here is the jinja file.

    Hostname: {{ ansible_net_hostname }}
    Model: {{ansible_net_model}}
    Serial Number: {{ansible_net_serialnum}}
    IOS Version: {{ansible_net_version}}
    IOS Image: {{ansible_net_image}}
    Switch Stack Models:
    {{ansible_net_stacked_models | to_nice_yaml(indent=2)}}
    
    Switch Stack Serials: 
    {{ansible_net_stacked_serialnums | to_nice_yaml(indent=2)}}
    
    CDP Neighbors:
    {{ansible_net_neighbors | to_nice_yaml(indent=2)}}
    
    Configuration:
    {{ansible_net_config}}
    
    VLAN:
    {{show_vlan.stdout[0] | to_nice_yaml(indent=2)}}

This all works fine until it hits a switch that cannot stack (e.g. chassis or VSS). When I run the playbook, I get the following-

msg: 'AnsibleUndefinedVariable: ''ansible_net_stacked_models'' is undefined

I've tried using if in Jinja2 like the following

...
Switch Stack Models:
{% if ansible_net_stacked_models is not defined %}
  NOT A STACKABLE SWITCH
{% else %}
    {{ansible_net_stacked_models | to_nice_yaml(indent=2)}}
{% endif %}

however it fails in the Jinja rendering and does not produce any output.

Is there a way to ignore missing variables in jinja or is there a better way to do this?

Upvotes: 0

Views: 1144

Answers (2)

Collin Clark
Collin Clark

Reputation: 36

Come to find out the error was stopping Ansible from even calling the jinja file. I dug around some more and found that I could set the variable to a default value in the inventory.

    [switch:vars]
    ansible_net_stacked_models='NOT A STACKABLE SWITCH'

When running the playbook if the device has valid info it will overwrite the default variable we defined in inventory. If the device doesn't have valid info, Ansible will simply pass the default variable we set in inventory, over to jinja2.

Upvotes: 0

sadok-f
sadok-f

Reputation: 1457

you can set a default value to your variable if is not defined

{{ ansible_net_stacked_models|default("NOT A STACKABLE SWITCH", true) | to_nice_yaml(indent=2) }}

Upvotes: 1

Related Questions