Repository: ansible/ansible-modules-core

Issue:
junos_config: obj, end = self.raw_decode(s, idx=_w(s, 0).end()) -> TypeError: expected string or buffer
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
junos_config

##### ANSIBLE VERSION
```

```

##### CONFIGURATION

##### OS / ENVIRONMENT

##### SUMMARY

##### STEPS TO REPRODUCE
```
Run Junos test cases, or manually:

- name: configure device with config
  junos_config:
    src: basic/config.j2
    provider: "{{ netconf }}"
  register: result

templates/basic/config.j2
interfaces {
    lo0 {
        unit 0 {
            family inet {
                address 1.1.1.1/32;
            }
        }
    }
}

```


##### EXPECTED RESULTS


##### ACTUAL RESULTS
```yaml
TASK [junos_config : configure device with config] *****************************
task path: /tmp/run-network-test/junos/devel/ansible/test/integration/targets/junos_config/tests/netconf/src_basic.yaml:11
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/core/network/junos/junos_config.py
<vsrx01> ESTABLISH LOCAL CONNECTION FOR USER: johnb
<vsrx01> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1479324113.09-83039427345774 `" && echo ansible-tmp-1479324113.09-83039427345774="` echo $HOME/.ansible/tmp/ansible-tmp-1479324113.09-83039427345774 `" ) && sleep 0'
<vsrx01> PUT /tmp/tmpj7mocy TO /home/johnb/.ansible/tmp/ansible-tmp-1479324113.09-83039427345774/junos_config.py
<vsrx01> EXEC /bin/sh -c 'chmod u+x /home/johnb/.ansible/tmp/ansible-tmp-1479324113.09-83039427345774/ /home/johnb/.ansible/tmp/ansible-tmp-1479324113.09-83039427345774/junos_config.py && sleep 0'
<vsrx01> EXEC /bin/sh -c 'python /home/johnb/.ansible/tmp/ansible-tmp-1479324113.09-83039427345774/junos_config.py; rm -rf "/home/johnb/.ansible/tmp/ansible-tmp-1479324113.09-83039427345774/" > /dev/null 2>&1 && sleep 0'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_5AgCUV/ansible_module_junos_config.py", line 343, in <module>
    main()
  File "/tmp/ansible_5AgCUV/ansible_module_junos_config.py", line 334, in main
    run(module, result)
  File "/tmp/ansible_5AgCUV/ansible_module_junos_config.py", line 292, in run
    return load_config(module, result)
  File "/tmp/ansible_5AgCUV/ansible_module_junos_config.py", line 245, in load_config
    config_format = module.params['src_format'] or guess_format(candidate)
  File "/tmp/ansible_5AgCUV/ansible_module_junos_config.py", line 176, in guess_format
    json.loads(config)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

fatal: [vsrx01]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_args": {
            "provider": {
                "host": "vsrx01", 
                "password": "Ansible", 
                "transport": "netconf", 
                "username": "ansible"
            }, 
            "src": "interfaces {\n    lo0 {\n        unit 0 {\n            family inet {\n                address 1.1.1.1/32;\n            }\n        }\n    }\n}\n\n"
        }, 
        "module_name": "junos_config"
    }, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_5AgCUV/ansible_module_junos_config.py\", line 343, in <module>\n    main()\n  File \"/tmp/ansible_5AgCUV/ansible_module_junos_config.py\", line 334, in main\n    run(module, result)\n  File \"/tmp/ansible_5AgCUV/ansible_module_junos_config.py\", line 292, in run\n    return load_config(module, result)\n  File \"/tmp/ansible_5AgCUV/ansible_module_junos_config.py\", line 245, in load_config\n    config_format = module.params['src_format'] or guess_format(candidate)\n  File \"/tmp/ansible_5AgCUV/ansible_module_junos_config.py\", line 176, in guess_format\n    json.loads(config)\n  File \"/usr/lib/python2.7/json/__init__.py\", line 339, in loads\n    return _default_decoder.decode(s)\n  File \"/usr/lib/python2.7/json/decoder.py\", line 364, in decode\n    obj, end = self.raw_decode(s, idx=_w(s, 0).end())\nTypeError: expected string or buffer\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE"

```



Discussion:
@gundalow I am looking into this issue. The root cause is 'config' passed in 'guess_format' [function](https://github.com/ansible/ansible-modules-core/blob/devel/network/junos/junos_config.py#L176)   is list object


Resolving patch:
diff --git a/network/junos/junos_config.py b/network/junos/junos_config.py
--- a/network/junos/junos_config.py
+++ b/network/junos/junos_config.py
@@ -242,7 +242,7 @@ def load_config(module, result):
     kwargs['commit'] = not module.check_mode
 
     if module.params['src']:
-        config_format = module.params['src_format'] or guess_format(candidate)
+        config_format = module.params['src_format'] or guess_format(str(candidate))
     elif module.params['lines']:
         config_format = 'set'
     kwargs['config_format'] = config_format


Test patch: