1

I have an Ansible script where i am simply using junos_command module to get users list from Juniper switch, below is the snippet of my code. I keep getting the RuntimeWarning whenever i try to run this. Moreover I have been successfully able to run commands like 'show version' using the below code itself. Please help

Script:

name: / GET USERS / Get list of all the current users on switch

action: junos_command

args: { commands: 'show configuration system login',
          provider: "{{ netconf }}" }

register: curr_users_on_switch

Error:

TASK [/ GET USERS / Get list of all the current users on switch] ***************
fatal: [rlab-er1]: FAILED! => {"changed": false, "failed": true, "module_stderr": "/home/mbhadoria/.local/lib/python2.7/site-packages/jnpr/junos/device.py:429: RuntimeWarning: CLI command is for debug use only!
\n  warnings.warn(\"CLI command is for debug use only!\", RuntimeWarning)\nTraceback (most recent call last):
\n  File \"/tmp/ansible_lVOmPp/ansible_module_junos_command.py\", line 261, in <module>
\n    main()
\n  File \"/tmp/ansible_lVOmPp/ansible_module_junos_command.py\", line 233, in main
\n    xmlout.append(xml_to_string(response[index]))
\n  File \"/tmp/ansible_lVOmPp/ansible_modlib.zip/ansible/module_utils/junos.py\", line 79, in xml_to_string\n  File \"src/lxml/lxml.etree.pyx\", line 3350, in lxml.etree.tostring (src/lxml/lxml.etree.c:84534)\nTypeError: Type 'str' cannot be serialized.
\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false}
Cookie Ninja
  • 1,156
  • 15
  • 29
Manu
  • 11
  • 3

1 Answers1

2

junos_command only support operation junos commands. What you are trying to run is configurational command. Hence you see "show version" which is operational command working but not "show configuration system login".

For such configuration data you can should use rpc option (get-configuration) with junos_command.

junos_command:
rpcs:
  - "get_configuration

You can also use junos_get_config.

http://junos-ansible-modules.readthedocs.io/en/latest/junos_get_config.html

or junos_rpc

https://github.com/Juniper/ansible-junos-stdlib/blob/master/library/junos_rpc

ex:

- name: Junos OS version
  hosts: all
  connection: local
  gather_facts: no
  tasks:
    - name: Get rpc run
      junos_rpc:
        host={{ inventory_hostname }}
        user=xxxx
        passwd=xxx
        rpc=get-config
        dest=get_config.conf
        filter_xml="<configuration><system><login/></system></configuration>"
      register: junos

or

  tasks:
   - name: Get rpc run
     junos_get_config:
       host: "{{ inventory_hostname }}"
       user: xxxx
       passwd: xxxx
       logfile: get_config.log
       dest: "{{ inventory_hostname }}.xml"
       format: xml
       filter: "system/login"

TASK [Get rpc run] *************************************************************
......

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

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

Nitin Kr
  • 521
  • 2
  • 12
  • Thank you so much for your response, i have been playing around with junos_rpc and even junos_cli and i did the exact same thing as you mentioned but somehow my variable, in this case 'junos' never gets set. Please suggest, take a look at my example below. Much appreciated! - name: Get system config 23 junos_rpc: 24 host={{ inventory_hostname }} 27 rpc=get-config 28 filter_xml="" 29 dest="user-accounts.txt" 30 register: curr_users_on_switch – Manu Aug 31 '16 at 16:41
  • Share the ansible and junos galaxy version you are using. I will try accordingly. – Nitin Kr Aug 31 '16 at 16:45
  • Junos version Model: mx5-t Junos: 13.3R8.7 JUNOS Base OS boot [13.3R8.7] JUNOS Base OS Software Suite [13.3R8.7] ansible-galaxy info --version ansible-galaxy 2.1.0.0 – Manu Aug 31 '16 at 17:22
  • hopefully this is what you wanted. please let me know if you want some other info. thank you so much for your help, i am a total newbie to ansible – Manu Aug 31 '16 at 17:22
  • Will get back to you tomorrow. – Nitin Kr Aug 31 '16 at 17:23
  • Hi Nitin, just wondering if you have any updates for me. I really appreciate your help! – Manu Sep 01 '16 at 17:24
  • Updated the last answer itself. Its working with the given example. – Nitin Kr Sep 01 '16 at 17:31
  • Thanks Nitin, i'll test out the code and you were able to get the output in 'junos' register, right? – Manu Sep 01 '16 at 17:47
  • Hi Nitin, your above example works but the issue arises when i try to pint out var 'junos' and that doesn't have anything and i get below error: TASK [debug] ******************************************************************* ok: [rlab-er1] => { "": "VARIABLE IS NOT DEFINED!" } – Manu Sep 01 '16 at 18:14
  • I thin ki resolved it, i mean 'junos' variable still won';t have output of the execution of rpc but using the file i can read it and store it in a variable to get the users list. Thanks! – Manu Sep 01 '16 at 19:27