Juniper / jsnapy

Python version of Junos Snapshot Administrator
Apache License 2.0
115 stars 58 forks source link

format of config file for js.check js.snap modules ? #79

Closed dgarros closed 8 years ago

dgarros commented 8 years ago

Hi

I'm trying to create a module jsnapy for ansible and I'm facing an issue I'm following this doc : https://github.com/Juniper/jsnapy/wiki/4.-Module

I prefer to initiate the ssh connection outside of Jsnapy and pass the object to the module. Also I called js.snap and js.check independently.

Snap is not reporting an error but I do not see where is the snapshot being save locally And I get an error regarding the syntax of the configuration file for js.check

"Exception during check - ('config file not defined properly', AttributeError(\"'str' object has no attribute 'get'\",))"

Please do you have an example of configuration file for js.snap & js.check when the device object is provided and when we don't want to use the database.

I looked here https://github.com/Juniper/jsnapy/tree/master/lib/jnpr/jsnapy/samples but all config_* file include hosts information which doesn't apply in my case.

Here is my code

        js = SnapAdmin()
        config_file = '/Users/dgarros/projects/lab-evpn-vxlan/jsnap_tests/mytest.yml'

        try:
            js.snap(config_file, "mytestforjsnap", dev=dev)
        except Exception as err:
            msg = 'Unable to get snap from device - {0}'.format(str(err))
            [..]

        try:
            snapvalue = js.check(config_file, "mytestforjsnap")
        except Exception as err:
            msg = 'Exception during check - {0}'.format(str(err))
            [..]

Below my configuration file and my test file

# /Users/dgarros/projects/lab-evpn-vxlan/jsnap_tests/mytest.yml
tests:
  - test_exists.yml
# /Users/dgarros/projects/lab-evpn-vxlan/jsnap_tests/test_exists.yml
tests_include:
  - test_version_check

test_version_check:
  - command: show version
  - iterate:
      id: host-name
      xpath: //software-information
      tests:
        - exists: //package-information/name
          info: "Test Succeeded!! node //package-information/name exists with name <{{pre['//package-information/name']}}> and hostname: <{{id_0}} > "
          err: "Test Failed!!! node //package-information/name does not exists in  hostname: <{{id_0}}> !! "
Jainpriyal commented 8 years ago

@dgarros: Is dev object defined in your python file. Looks like it is getting string instead of Device object.

dev should be object of PyEz Device class, and it should have established connection with device.

from jnpr.junos import Device

dev_obj = Device(host='10.209.1.1', user='foo', password='bar')
dev_obj.open()

I tried with this file:

from jnpr.jsnapy import SnapAdmin
from pprint import pprint
from jnpr.junos import Device

dev_obj = Device(host='10.209.16.204', user='demo', password='demo123')
dev_obj.open()

js = SnapAdmin()
config_file = "/Users/jpriyal/Desktop/developement/demo/module_demo/demo_snapcheck.yml"

# can pass device object
# it will not create new connection with device
snapchk = js.snapcheck(config_file, "snap", dev=dev_obj)

for val in snapchk:
    print "Tested on", val.device
    print "Final result: ", val.result
    print "Total passed: ", val.no_passed
    print "Total failed:", val.no_failed
    pprint(dict(val.test_details))

It is working fine:

Tested on 10.209.16.204
Final result:  Passed
Total passed:  1
Total failed: 0
{'show version': [{'actual_node_value': ['junos',
                                         'jbase',
                                         'jplatform',
                                         'jweb',
                                         'jruntime',
                                         'jdocs',
                                         'jservices-aacl',
                                         'jservices-alg',
                                         'jservices-appid',
                                         'jservices-bgf',
                                         'jservices-cpcd',
                                         'jservices-hcm',
                                         'jservices-idp',
                                         ..........,
                                         'jpfe-common',
                                         'jroute'],
                   'node_name': '//package-information/name',
                   'result': True,
                   'testoperation': 'exists',
                   'xpath': '//software-information'}]}
(venv)sh-3.2# 
(venv)sh-3.2# 
(venv)sh-3.2#
dgarros commented 8 years ago

Hi Yes dev is a pyez object in my case

try:
        dev = Device(m_args['host'],
                        user=m_args['user'],
                        password=m_args['passwd'],
                        port=m_args['port'],
                        gather_facts=False).open()

    except Exception as err:
        msg = 'Unable to connect to {0}: {1}'.format(args['host'], str(err))
        logging.error(msg)
        module.fail_json(msg=msg)
        # --- UNREACHABLE ---

Please can you share the content of your config file demo_snapcheck.yml ? Thanks

Jainpriyal commented 8 years ago

Its the same file that you are using:

(venv)sh-3.2# cat demo_snapcheck.yml 
tests:
  - test_exists.yml
dgarros commented 8 years ago

Thanks Any idea why it's not working for me right now ?

Where is supposed to be store the first "snap", I couldn't find it in /etc/jsnapy/snapshots Thanks

Jainpriyal commented 8 years ago

As my logic get hit at https://github.com/Juniper/jsnapy/blob/5ea8b1b6a7b8a747a8153027715554f7e40e5378/lib/jnpr/jsnapy/jsnapy.py#L820 Can you check if below return True for you isinstance(dev, Device)

dgarros commented 8 years ago

@Jainpriyal

If you want to give it a try yourself I uploaded the module I'm working on here Be careful, I hardcoded the path to the file inside the module right now for troubleshooting

And you can find my ansible project here

Playbook : https://git.juniper.net/jdi-tme/lab-evpn-vxlan/blob/jsnapy/pb.jsnapy.yaml Tests file : https://git.juniper.net/jdi-tme/lab-evpn-vxlan/tree/jsnapy/jsnap_tests

Let me know if you have questions

dgarros commented 8 years ago

Manage to make it work, thanks for your help