Automation

 View Only
last person joined: yesterday 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Question on an Ansible playbook creation

    Posted 02-20-2025 10:43

    I am having issues automating something with ansible,
    Every month juniper requests us to give them a full inventory list of all of our devices, by running this command on the box and saving it in a file:
    "show chassis hardware detail | display xml | no-more"

    At first i tried something like this:

    ---
    - name: Grab Junipers Monlthy Request
      hosts: test
      gather_facts: no
      connection: netconf
      
      vars_files:
        - ../../../Credentials/credentials.yml
    
      vars:
        ansible_network_os: junos
    
      tasks:
            
        - name: Run Command On Juniper Device
          junipernetworks.junos.junos_config:
            lines:
              - show chassis hardware detail | display xml | no-more
    

    That obviously does not work because ansible doesn't like the "|" within the command.
    Does anyone have a playbook handy that will grab information like this:

    user@devicename> show chassis hardware detail | display xml | no-more 
    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/24.4R1.9/junos">
        <chassis-inventory xmlns="http://xml.juniper.net/junos/24.4R0/junos-chassis">
    
           --All chassis information from the output of the command---
    
                </chassis-module>
            </chassis>
        </chassis-inventory>
        <cli>
            <banner>{master:0}</banner>
        </cli>
    </rpc-reply>


    They need it formated "exactly how it is outputted" for their automation on their end that they use.



    ------------------------------
    JORDAN ATHERTON
    ------------------------------


  • 2.  RE: Question on an Ansible playbook creation

    Posted 02-20-2025 13:52

    I got this figured out, was a bit more complex than i was hoping for

    ---
    - name: Grab Juniper's Monthly Request
      hosts: supported
      gather_facts: no
      connection: netconf
    
      vars_files:
        - ../../../Credentials/credentials.yml
    
      vars:
        ansible_network_os: junos
        inventory_directory: "SavedData"  # Change this to the directory you want to save files to
    
      tasks:
        - name: Create directory for inventory files
          delegate_to: localhost
          run_once: true
          file:
            path: "./{{ inventory_directory }}"
            state: directory
            mode: '0755'
    
        - name: Get Junos version
          junipernetworks.junos.junos_command:
            commands:
              - show version | display xml | no-more
          register: version_output
    
        - name: Extract Junos version from output
          set_fact:
            junos_version: "{{ version_output.stdout[0] | regex_search('<junos-version>(.*?)</junos-version>', '\\1') | first }}"
    
        - name: Run "show chassis hardware detail" command
          junipernetworks.junos.junos_command:
            commands:
              - show chassis hardware detail | display xml | no-more
          register: chassis_output
    
        - name: Remove the unwanted <rpc-reply> message-id line
          set_fact:
            cleaned_xml: "{{ chassis_output.stdout[0] | regex_replace('<rpc-reply message-id=.*?>', '') | replace('><', '>\n<') }}"
    
        - name: Remove last two lines from output
          set_fact:
            trimmed_xml: "{{ cleaned_xml.splitlines()[:-2] | join('\n') }}"
    
        - name: Build Final Output
          set_fact:
            formatted_output: |
              User@{{ inventory_hostname }}> show chassis hardware detail | display xml | no-more
              <rpc-reply xmlns:junos="http://xml.juniper.net/junos/{{ junos_version }}/junos">
                  <chassis-inventory xmlns="http://xml.juniper.net/junos/24.4R0/junos-chassis">
              {{ trimmed_xml }}
                  </chassis-inventory>
                  </chassis-inventory>
                  <cli>
                      <banner>{master:0}</banner>
                  </cli>
              </rpc-reply>
    
        - name: Save formatted output to a file 
          delegate_to: localhost
          copy:
            content: "{{ formatted_output }}"
            dest: "./{{ inventory_directory }}/{{ inventory_hostname }}.txt"
    

    Here is the playbook in case anyone in the future runs across this issue



    ------------------------------
    JORDAN ATHERTON
    ------------------------------



  • 3.  RE: Question on an Ansible playbook creation

    Posted 02-21-2025 10:29

    try this as well,

    - name: get chassis hardware detail
      junos_command:
        commands: show chassis hardware detail
        display: xml
    


    ------------------------------
    FRANCOIS VAN HEERDEN
    ------------------------------



  • 4.  RE: Question on an Ansible playbook creation

    Posted 02-28-2025 01:57

    It is not necessary to go via 'show' commands. The data you are looking for is probably available under 'junos_facts'.

    https://www.juniper.net/documentation/us/en/software/junos-ansible/ansible/topics/task/junos-ansible-device-facts-retrieving.html
    https://galaxy.ansible.com/ui/repo/published/junipernetworks/junos/content/module/junos_facts/

    So, something like

    ---
    - name: Collect Junos versions and models and save to CSV
      hosts: all
      gather_facts: no
      tasks:
        - name: Gather Junos facts
          junipernetworks.junos.junos_facts:
          register: device_facts

        - name: Add Junos version and model to a list
          set_fact:
            device_info_list: "{{ device_info_list | default([]) + [{'hostname': inventory_hostname, 'model': device_facts.facts.model, 'version': device_facts.facts.version}] }}"

        - name: Write results to CSV
          local_action:
            module: copy
            content: |
              hostname,model,version
              {% for device in device_info_list %}
              {{ device.hostname }},{{ device.model }},{{ device.version }}
              {% endfor %}
            dest: junos_device_info.csv
          run_once: yes






    ------------------------------
    PER GRANATH
    ------------------------------