Repository: ansible/ansible-modules-core

Issue:
Unable to deploy config to junos
<!--- Verify first that your issue/request is not already reported in GitHub -->
##### ISSUE TYPE

<!--- Pick one below and delete the rest: -->
- Bug Report
##### COMPONENT NAME

<!--- Name of the plugin/module/task -->

modules/core/network/junos/junos_template.py
module_utils/junos.py
##### ANSIBLE VERSION

<!--- Paste verbatim output from “ansible --version” between quotes below -->

```
ansible 2.1.0.0
  config file = /home/mcdowell/git/stamnet-automasjon-ansible/ansible.cfg
  configured module search path = Default w/o overrides
```
##### CONFIGURATION

<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
##### OS / ENVIRONMENT

<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->

Ubunut 14.04
##### SUMMARY

<!--- Explain the problem briefly -->

Unable to deploy a junos template to a router
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->

Run the playbook under, does not need actual configuration file.

<!--- Paste example playbooks or commands between quotes below -->

```
 - name: juniper device
   hosts:  all
   gather_facts: no
   connection: local

   tasks:
     - name: push config to host
       junos_template:
         host: "{{ host }}"
         port: 830
         password: "{{ default_pw }}"
         username: "{{ default_user }}"
         src: "{{ build_dir }}{{ ansible_host }}/system_confg"

```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

<!--- What did you expect to happen when running the steps above? -->

Expected the config to be deployed to router.
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

Failed with ascii error.

<!--- Paste verbatim command output between quotes below -->

```
The error was: UnicodeEncodeError: 'ascii' codec can't encode characters in position 13559-13560: ordinal not in range(128)
fatal: [fwbuilder-gw2]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_onhFr8/ansible_module_junos_template.py\", line 151, in <module>\n    main()\n  File \"/tmp/ansible_onhFr8/ansible_module_junos_template.py\", line 135, in main\n    results['_backup'] = str(module.get_config()).strip()\n  File \"/tmp/ansible_onhFr8/ansible_modlib.zip/ansible/module_utils/junos.py\", line 331, in get_config\n  File \"/tmp/ansible_onhFr8/ansible_modlib.zip/ansible/module_utils/junos.py\", line 264, in get_config\nUnicodeEncodeError: 'ascii' codec can't encode characters in position 13559-13560: ordinal not in range(128)\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false}
```
##### Potential fix?

Changed the str parsing on line 135 in junos_template.py and on line 264 in junos.py to unicode instead. This seems to have fixed the issue, but not sure if it's a good fix or not, if it is, feel free to take the credits for it.



Discussion:
<!-- @Akvarium: Thanks for filing this bug report! -->

@privateip ping, this issue is waiting for your response.
[click here for bot help](https://github.com/jctanner/ansibullbot/blob/ISSUE_TRIAGE_CLASS/ISSUE_HELP.md)

<!--- boilerplate: issue_bug_new --->

@privateip I have also run into this. I believe I have found a concrete example of it in the config. set `snmp location "“West Building”"` Notice the "extra" quotes on the string. These are not ascii and most likely a result of copying config out of a rich text editor. I guess Juniper has some use case for allowing unicode chars in these text fields. I also second changing to support unicode chars.

Also facing this issue, have just removed the str() for now so I can use the modules.

vim /usr/local/lib/python2.7/site-packages/ansible/module_utils/junos.py
before:
return str(ele.text).strip()
after:
return ele.text.strip()

vim /usr/local/lib/python2.7/site-packages/ansible/modules/core/network/junos/junos_template.py
before:
results['_backup'] = str(module.get_config()).strip()
after:
results['_backup'] = module.get_config().strip()

@privateip, @gundalow, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->


Resolving patch:
diff --git a/network/junos/_junos_template.py b/network/junos/_junos_template.py
--- a/network/junos/_junos_template.py
+++ b/network/junos/_junos_template.py
@@ -144,7 +144,7 @@ def main():
             "set per junos-pyez documentation")
 
     results = dict(changed=False)
-    results['_backup'] = str(module.config.get_config()).strip()
+    results['_backup'] = unicode(module.config.get_config()).strip()
 
     try:
         diff = module.config.load_config(src, commit=commit, replace=replace,
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
@@ -210,7 +210,7 @@ def config_to_commands(config):
     return commands
 
 def diff_commands(commands, config):
-    config = [str(c).replace("'", '') for c in config]
+    config = [unicode(c).replace("'", '') for c in config]
 
     updates = list()
     visited = set()


Test patch: