junos_install_config - Is it possible to get a diff without committing?

249 views
Skip to first unread message

erezed

unread,
Sep 13, 2017, 4:29:23 PM9/13/17
to Junos Python EZ
Hi,

I'm currently using napalm_install_config but I'd like to switch to the Juniper one since it offers more features. However, with the napalm installation module I have the option to receive a diff without committing the configuration. Is this option available within junos_install_config or do I have to cobble something together? 

Thanks,

Stacy Smith

unread,
Sep 13, 2017, 10:22:32 PM9/13/17
to Junos Python EZ

On Wednesday, September 13, 2017 at 2:29:23 PM UTC-6, erezed wrote:
I'm currently using napalm_install_config but I'd like to switch to the Juniper one since it offers more features. However, with the napalm installation module I have the option to receive a diff without committing the configuration. Is this option available within junos_install_config or do I have to cobble something together?

If you use junos_install_config in check mode, it will load the configuration and produce a diff (assuming diffs_file is set), but it will not commit the config. If you are using Ansible >= 2.2, then you can specify check_mode: yes on the junos_install_config task without having to enable check mode for the entire playbook. See this link for more info.

Here's a playbook which produces a small config change (set system host-name current_host_name.foo) using a Jinja2 template. It then loads the config change and produces a diff (but doesn't commit). It then loads the diff file into an Ansible variable and prints the result. Again, the main key is simply check_mode: yes on the junos_install_config task.

---
# This playbook requires Ansible >= 2.2.
- name: "Demonstrate diff from junos_install_config without commit"
  hosts: junos-all
  connection: local
  gather_facts: no
  roles:
    - Juniper.junos
  tasks:
    - name: "Cleanup the ./output directory"
      file:
        path: "./output"
        state: absent
      run_once: True
      delegate_to: localhost
    - name: "Create the ./output directory"
      file:
        path: "./output"
        state: directory
      run_once: True
      delegate_to: localhost
    - name: "Verify the ./input directory exists"
      file:
        path: "./input"
        state: directory
      run_once: True
      delegate_to: localhost
    - name: "Verify the template exists"
      file:
        path: "./input/template-host-name.j2"
        state: file
      run_once: True
      delegate_to: localhost
    - name: "Build configuration files from template"
      template:
        src: "./input/template-host-name.j2"
        dest: "./input/{{ inventory_hostname }}-host-name.conf"
    - name: "Produce config diff"
      junos_install_config:
        host: "{{ inventory_hostname }}"
        user: "{{ jaccess.user }}"
        passwd: "{{ jaccess.passwd }}"
        file: "./input/{{ inventory_hostname }}-host-name.conf"
        diffs_file: "./output/{{ inventory_hostname }}-host-name.diff"
      check_mode: yes
      register: response
    - name: "Cleanup the config file"
      file:
        path: "./input/{{ inventory_hostname }}-host-name.conf"
        state: absent
    - name: "Read diff file into diffs variable"
      set_fact:
        diffs: "{{ lookup('file','./output/{{ inventory_hostname }}-host-name.diff') }}"
    - name: "Print diffs"
      debug:
        var: diffs
    - name: "Cleanup the ./output directory"
      file:
        path: "./output"
        state: absent
      run_once: True
      delegate_to: localhost


Reply all
Reply to author
Forward
0 new messages