Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-Fixes JSON parsing(use JSON object instead of string) for facts module #31818

Merged
merged 1 commit into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ansible/modules/network/junos/junos_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
notes:
- Ensure I(config_format) used to retrieve configuration from device
is supported by junos version running on device.
- With I(config_format = json), configuration in the results will be a dictionary(and not a JSON string)
- This module requires the netconf system service be enabled on
the remote device being managed.
- Tested against vSRX JUNOS version 15.1X49-D15.4, vqfx-10000 JUNOS Version 15.1X53-D60.4.
Expand Down Expand Up @@ -155,7 +156,7 @@ def populate(self):
config = self.get_text(reply, 'configuration-text')

elif config_format == 'json':
config = str(reply.text).strip()
config = self.module.from_json(reply.text.strip())

elif config_format == 'set':
config = self.get_text(reply, 'configuration-set')
Expand Down Expand Up @@ -370,7 +371,6 @@ def main():
else:
warnings += ['junos-eznc is required to gather old style facts but does not appear to be installed. '
'It can be installed using `pip install junos-eznc`']

module.exit_json(ansible_facts=ansible_facts, warnings=warnings)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
- assert:
that:
- "result.changed == false"
- "'\"data\" : \"{{ inventory_hostname_short }}\"' in result['ansible_facts']['ansible_net_config']"
- "'{{ inventory_hostname_short }}' == '{{ result['ansible_facts']['ansible_net_config']['configuration'][0]['system'][0]['host-name'][0]['data'] }}' "

- name: Collect config facts from device in text format
junos_facts:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?xml version="1.0" encoding="UTF-8"?><rpc-reply message-id="urn:uuid:72c481b8">
{
"configuration" : [
{
"system" : [
{
"host-name" : [
{
"data" : "vsrx01"
}
],
"domain-name" : [
{
"data" : "junos.com"
}
],
"name-server" : [
{
"name" :
{
"data" : "172.26.1.1"
}
},
{
"name" :
{
"data" : "8.8.8.8"
}
}
],
"services" : [
{
"ssh" : [
{
}
],
"telnet" : [
{
}
],
"netconf" : [
{
"ssh" : [
{
"port" : [
{
"data" : "830"
}
]
}
],
"traceoptions" : [
{
"file" : [
{
"filename" : [
{
"data" : "netconf-ops.log"
}
]
}
],
"flag" : [
{
"name" :
{
"data" : "all"
}
}
]
}
]
}
],
"web-management" : [
{
"http" : [
{
"interface" : [
{
"data" : "fxp0.0"
}
]
}
]
}
]
}
],
"syslog" : [
{
"user" : [
{
"name" :
{
"data" : "*"
},
"contents" : [
{
"name" :
{
"data" : "any"
},
"emergency" : [
{
"data" : null
}
]
}
]
}
],
"file" : [
{
"name" :
{
"data" : "messages"
},
"contents" : [
{
"name" :
{
"data" : "any"
},
"any" : [
{
"data" : null
}
]
},
{
"name" :
{
"data" : "authorization"
},
"info" : [
{
"data" : null
}
]
}
]
},
{
"name" :
{
"data" : "interactive-commands"
},
"contents" : [
{
"name" :
{
"data" : "interactive-commands"
},
"any" : [
{
"data" : null
}
]
}
]
},
{
"name" :
{
"data" : "test1"
},
"contents" : [
{
"name" :
{
"data" : "any"
},
"any" : [
{
"data" : null
}
]
}
]
}
]
}
]
}
]
}
]
}
</rpc-reply>
11 changes: 11 additions & 0 deletions test/units/modules/network/junos/test_junos_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ def test_junos_get_facts_subset_config_set(self):
self.assertEqual(facts['ansible_net_hostname'], 'vsrx01')
self.assertTrue('ansible_net_interfaces' not in facts)

def test_junos_get_facts_subset_config_json(self):
self.get_config.return_value = load_fixture('get_configuration_rpc_reply_json.txt')
set_module_args(dict(gather_subset='config', config_format='json'))
result = self.execute_module(format='xml')
facts = result['ansible_facts']

self.assertTrue('ansible_net_config' in facts)
self.assertTrue('configuration' in facts['ansible_net_config'])
self.assertEqual(facts['ansible_net_hostname'], 'vsrx01')
self.assertTrue('ansible_net_interfaces' not in facts)

def test_junos_get_facts_subset_list(self):
set_module_args(dict(gather_subset=['hardware', 'interfaces']))
result = self.execute_module(format='xml')
Expand Down