New UCS Ansible Module Based on UCS Python SDK Logout Issue

Hello Everyone,

I’m working on my first Ansible module to interact with UCS and will be hopefully creating more of these to do new builds of UCS servers. I have the following code already https://github.com/btotharye/ansible-ucs and the login process is working fine but for some reason I’m having issues with the logout. I’m not sure what is going on yet but as far as the UCS Python SDK goes UcsHandle.logout() should work and as far as I can tell I’m getting the handle right but having issues.

If anyone could offer links or suggestions on how to debug the logout part that is failing I would appreciate it, right now I’m hitting line 19 of the logout script ucs_logout.py and its generating the module.fail_json message but I’m not sure whats causing the exception. Login process is working fine and I would think it would just be a matter of passing in the handle and logging it out but for some reason that isn’t working.

Documentation to the UCS Python SDK: https://ciscoucs.github.io/ucsmsdk_docs/ucsmsdk_ug.html#getting-started

Thanks

I’m guessing that the workflow is to login, which returns a jsonpickle string and then pass that into the logout module?

If so, you seem to be missing some jsonpickle decode functionality in the logout module to turn it back into a UcsHandle.

I don’t have any familiarity with UCS, but it seems weird to have a login/logout module. Wouldn’t a login without a logout just disconnect when python exits and leave you logged out? Unless you are trying to destroy whatever session may be stored on the remote side?

I’d probably just have each individual module do a login/logout, and not try passing around a UcsModule through pickling.

Yea I was thinking the same thing, I’ve tried a few different things and right now I’ve got this output as the error it almost seems as if its something to do with the json object of the handle on the logout:

ansible-ucs brihopki$ ansible-playbook callhome.yml -vvv

Using /Users/brihopki/ansible-ucs/ansible.cfg as config file

PLAYBOOK: callhome.yml *********************************************************

1 plays in callhome.yml

PLAY [ucs] *********************************************************************

TASK [Login 14.17.106.164] *****************************************************

task path: /Users/brihopki/ansible-ucs/callhome.yml:7

Using module file /Users/brihopki/ansible-ucs/library/ucs_login.py

ESTABLISH LOCAL CONNECTION FOR USER: brihopki

EXEC /bin/sh -c ‘( umask 77 && mkdir -p “echo $HOME/.ansible/tmp/ansible-tmp-1483965963.97-212442284630795” && echo ansible-tmp-1483965963.97-212442284630795=“echo $HOME/.ansible/tmp/ansible-tmp-1483965963.97-212442284630795” ) && sleep 0’

PUT /var/folders/nb/1422r72n7qz_gq0knfzd6n200000gn/T/tmpEoezdU TO /Users/brihopki/.ansible/tmp/ansible-tmp-1483965963.97-212442284630795/ucs_login.py

EXEC /bin/sh -c ‘chmod u+x /Users/brihopki/.ansible/tmp/ansible-tmp-1483965963.97-212442284630795/ /Users/brihopki/.ansible/tmp/ansible-tmp-1483965963.97-212442284630795/ucs_login.py && sleep 0’

EXEC /bin/sh -c ‘/usr/bin/python /Users/brihopki/.ansible/tmp/ansible-tmp-1483965963.97-212442284630795/ucs_login.py; rm -rf “/Users/brihopki/.ansible/tmp/ansible-tmp-1483965963.97-212442284630795/” > /dev/null 2>&1 && sleep 0’

ok: [ucspe] => {

“changed”: false,

“cookie”: “1483965469/94631f41-341b-402c-827d-12b24e7707af”,

“handle”: “{\n "_UcsHandle__commit_buf": {}, \n "_UcsSession__domains": "org-root", \n "_UcsSession__refresh_period": 600, \n "_UcsSession__name": "", \n "_UcsSession__evt_channel": "noencssl", \n "_UcsSession__uri": "https://14.17.106.164:443", \n "py/object": "ucsmsdk.ucshandle.UcsHandle", \n "_UcsSession__ip": "14.17.106.164", \n "_UcsSession__priv": "aaa,admin,ext-lan-config,ext-lan-policy,ext-lan-qos,ext-lan-security,ext-san-config,ext-san-policy,ext-san-security,fault,operations,pod-config,pod-policy,pod-qos,pod-security,read-only", \n "_UcsSession__last_update_time": "Mon Jan 9 07:46:04 2017", \n "_UcsHandle__commit_buf_tagged": {}, \n "_UcsSession__virtual_ipv4_address": "14.17.106.164", \n "_UcsSession__force": false, \n "_UcsHandle__threaded": false, \n "_UcsSession__ucs": "UCSPE-14-17-106-164", \n "_UcsSession__version": {\n "py/object": "ucsmsdk.ucscoremeta.UcsVersion", \n "_UcsVersion__major": "3", \n "_UcsVersion__patch": "c", \n "_UcsVersion__mr": "2", \n "_UcsVersion__version": "3.0(2c)", \n "_UcsVersion__minor": "0"\n }, \n "_UcsSession__dump_xml": false, \n "_UcsSession__channel": "noencssl", \n "_UcsSession__session_id": "", \n "_UcsSession__redirect": false, \n "_UcsSession__cookie": "1483965469/94631f41-341b-402c-827d-12b24e7707af", \n "_UcsSession__proxy": null, \n "_UcsSession__refresh_timer": null, \n "_UcsSession__password": "ucspe", \n "_UcsSession__driver": {\n "py/object": "ucsmsdk.ucsdriver.UcsDriver", \n "_UcsDriver__handlers": [\n {\n "py/type": "ucsmsdk.ucsdriver.SmartRedirectHandler"\n }, \n {\n "py/type": "ucsmsdk.ucsdriver.TLS1Handler"\n }\n ], \n "_UcsDriver__headers": {}, \n "_UcsDriver__redirect_uri": null, \n "_UcsDriver__proxy": null\n }, \n "_UcsSession__username": "ucspe"\n}”,

“invocation”: {

“module_args”: {

“ip”: “14.17.106.164”,

“password”: “ucspe”,

“username”: “ucspe”

},

“module_name”: “ucs_login”

},

“logged_in”: true

}

TASK [Logout 14.17.106.164] ****************************************************

task path: /Users/brihopki/ansible-ucs/callhome.yml:14

Using module file /Users/brihopki/ansible-ucs/library/ucs_logout.py

ESTABLISH LOCAL CONNECTION FOR USER: brihopki

EXEC /bin/sh -c ‘( umask 77 && mkdir -p “echo $HOME/.ansible/tmp/ansible-tmp-1483965965.23-176363209450610” && echo ansible-tmp-1483965965.23-176363209450610=“echo $HOME/.ansible/tmp/ansible-tmp-1483965965.23-176363209450610” ) && sleep 0’

PUT /var/folders/nb/1422r72n7qz_gq0knfzd6n200000gn/T/tmp39d_hb TO /Users/brihopki/.ansible/tmp/ansible-tmp-1483965965.23-176363209450610/ucs_logout.py

EXEC /bin/sh -c ‘chmod u+x /Users/brihopki/.ansible/tmp/ansible-tmp-1483965965.23-176363209450610/ /Users/brihopki/.ansible/tmp/ansible-tmp-1483965965.23-176363209450610/ucs_logout.py && sleep 0’

EXEC /bin/sh -c ‘/usr/bin/python /Users/brihopki/.ansible/tmp/ansible-tmp-1483965965.23-176363209450610/ucs_logout.py; rm -rf “/Users/brihopki/.ansible/tmp/ansible-tmp-1483965965.23-176363209450610/” > /dev/null 2>&1 && sleep 0’

fatal: [ucspe]: FAILED! => {

“changed”: false,

“failed”: true,

“invocation”: {

“module_args”: {

“handle”: “{‘_UcsHandle__commit_buf’: {}, ‘_UcsSession__domains’: ‘org-root’, ‘_UcsSession__refresh_period’: 600, ‘_UcsSession__name’: ‘’, ‘_UcsSession__evt_channel’: ‘noencssl’, ‘_UcsSession__uri’: ‘https://14.17.106.164:443’, ‘py/object’: ‘ucsmsdk.ucshandle.UcsHandle’, ‘_UcsSession__ip’: ‘14.17.106.164’, ‘_UcsSession__priv’: ‘aaa,admin,ext-lan-config,ext-lan-policy,ext-lan-qos,ext-lan-security,ext-san-config,ext-san-policy,ext-san-security,fault,operations,pod-config,pod-policy,pod-qos,pod-security,read-only’, ‘_UcsSession__last_update_time’: ‘Mon Jan 9 07:46:04 2017’, ‘_UcsHandle__commit_buf_tagged’: {}, ‘_UcsSession__virtual_ipv4_address’: ‘14.17.106.164’, ‘_UcsSession__force’: False, ‘_UcsHandle__threaded’: False, ‘_UcsSession__ucs’: ‘UCSPE-14-17-106-164’, ‘_UcsSession__version’: {‘py/object’: ‘ucsmsdk.ucscoremeta.UcsVersion’, ‘_UcsVersion__major’: ‘3’, ‘_UcsVersion__patch’: ‘c’, ‘_UcsVersion__mr’: ‘2’, ‘_UcsVersion__version’: ‘3.0(2c)’, ‘_UcsVersion__minor’: ‘0’}, ‘_UcsSession__dump_xml’: False, ‘_UcsSession__channel’: ‘noencssl’, ‘_UcsSession__session_id’: ‘’, ‘_UcsSession__redirect’: False, ‘_UcsSession__cookie’: ‘1483965469/94631f41-341b-402c-827d-12b24e7707af’, ‘_UcsSession__proxy’: None, ‘_UcsSession__refresh_timer’: None, ‘_UcsSession__password’: ‘ucspe’, ‘_UcsSession__driver’: {‘py/object’: ‘ucsmsdk.ucsdriver.UcsDriver’, ‘_UcsDriver__handlers’: [{‘py/type’: ‘ucsmsdk.ucsdriver.SmartRedirectHandler’}, {‘py/type’: ‘ucsmsdk.ucsdriver.TLS1Handler’}], ‘_UcsDriver__proxy’: None, ‘_UcsDriver__redirect_uri’: None, ‘_UcsDriver__headers’: {}}, ‘_UcsSession__username’: ‘ucspe’}”

},

“module_name”: “ucs_logout”

},

“msg”: “‘str’ object has no attribute ‘logout’”

}

to retry, use: --limit @/Users/brihopki/ansible-ucs/callhome.retry

PLAY RECAP *********************************************************************

ucspe : ok=1 changed=0 unreachable=0 failed=1

The current code in the logout script is this:

#!/usr/bin/python
# -*- mode: python -*-

from ucsmsdk.ucshandle import UcsHandle
import json
import jsonpickle

def ucs_logout(module):
    handle = module.params.get('handle')

    results = {}
    ucs_handle = handle

    try:
        ucs_handle.logout()
        results['logged_out'] = True
    except  Exception as e:
        #module.fail_json(msg="logout failed")
        module.fail_json(msg = str(e))

    return results

def main():
    module = AnsibleModule(
        argument_spec = dict(
            handle    = dict(required=True)
        )
    )

    results = ucs_logout(module)
    module.exit_json(**results)

from ansible.module_utils.basic import *
if __name__ == '__main__':
    main()

So I was just trying now with the plain handle that was already there from the login but its complaining about not having a logout attribute. Do you have any recommendations on ways to error handle this and get better debugging on whats really happening? This is my first time creating an ansible module and I’ve been reading through the doc’s but not sure the best way to debug to get more.

I reached out to the guys in Cisco where I work to see if they know more about this cause I’m wondering if I should really even care about the logout but it appears that anything after the login I have this issue when trying to use the handle for some reason, I noticed testing last night if I didn’t try logging out and instead tried to query a dn on the UCS that it had issues too so might be something with this handle.

BTW seems as if its something to do with the handle because if I put in the IP, user, and password into the handle for the logout it works so I’ll just update my playbook to have the login credentials on the logout as well.

Thanks

Seems like its some issue with the encoding of the handle still looking into it more, thanks for the help thus far.