Repository: ansible/ansible

Issue:
Upper Case Host on ios_config and ios_command fail to connect
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
ios_command
ios_config

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.4.0.0
  config file = /mnt/c/jv/Ansible/ansible.cfg
  configured module search path = [u'/etc/ansible/library']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]

```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"
DEFAULT_DEBUG(env: ANSIBLE_DEBUG) = False
DEFAULT_HOST_LIST(/mnt/c/jv/Ansible/ansible.cfg) = [u'/mnt/c/jv/Ansible/hosts']
DEFAULT_LOG_PATH(env: ANSIBLE_LOG_PATH) = /home/joshv/ansible.log
DEFAULT_MODULE_PATH(/mnt/c/jv/Ansible/ansible.cfg) = [u'/etc/ansible/library']

Otherwise, 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.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
Windows Subsystem Linux & Ubuntu 16.04.3

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:        14.04
Codename:       trusty

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.3 LTS
Release:        16.04
Codename:       xenial

##### SUMMARY
<!--- Explain the problem briefly -->
In my inventory file I have the following:

[switches]
COMSW0001
comsw0002

ios_command & ios_config connect just fine when the characters are all lower case. When they are all upper case Ansible is unable to connect to host. If I swap the upper/lower case around, the issue follows the upper case host. 


##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
# Ansible playbook to copy and change the boot statement on a Cisco IOS switch
# Leverages NTC-Ansible: https://github.com/networktocode/ntc-ansible
# Requires Python PyNTC
- name: Copy code to machine and change boot statement
  connection: local
  hosts: switches
  gather_facts: no
  serial:
    - 5%
    - 10%
    - 30%
  vars_files:
    - /path/to/cred/file
  vars:
    cli:
      username: "{{ u }}"
      password: "{{ p }}"

    code_version:
      WS-C3560CX-8PC-S: c3560cx-universalk9-mz.152-4.E4.bin
      WS-C3560C-8PC-S: c3560c405-universalk9-mz.152-2.E7.bin
      WS-C3560CG-8PC-S: c3560c405-universalk9-mz.152-2.E7.bin
      WS-C3560C-12PC-S: c3560c405-universalk9-mz.152-2.E7.bin

  tasks:
    - name: Determine OS Version
      ntc_show_command:
        connection: ssh
        platform: cisco_ios_ssh
        command: show version
        template_dir: "/etc/ansible/library/ntc-ansible/ntc-templates/templates"
        host: "{{ inventory_hostname }}"
        username: "{{ u }}"
        password: "{{ p }}"
        secret: "{{ p }}"
      register: ntc_result

    - name: Debug NTC
      debug:
        msg: "{{ ntc_result }}"

    - name: Get SNMP information
      ios_command:
        provider: "{{ cli }}"
        commands:
          - show ip int brief
        timeout: 40
      register: run_config

    - name: Get the details
      set_fact:
        output: "{{ run_config.stdout_lines[0] }}"

    - name: Verify SCP is configured
      ios_config:
        provider: "{{ cli }}"
        lines:
          - ip scp server enable


    - name: Copy File
      ntc_file_copy:
        local_file: images/{{ code_version[ntc_result['response'][0]['hardware'][0]] }}
        platform: cisco_ios_ssh
        username: "{{ u }}"
        password: "{{ p }}"
        host: "{{ inventory_hostname }}"
      register: output
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
First task connects fine and works.
Tasks with ios_* fail to connect.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
Failed to connect on the ios_* - I do have the log file. Looks to be an Invalid Argument
<!--- Paste verbatim command output between quotes below -->
```
2017-11-02 16:03:24,692 p=7253 u=joshv |    7262 1509656604.69279: Loading ShellModule 'sh' from /usr/local/lib/python2.7/dist-packages/ansible/plugins/shell/sh.py
  7262 1509656604.69401: Loading ShellModule 'sh' from /usr/local/lib/python2.7/dist-packages/ansible/plugins/shell/sh.py (found_in_cache=True, class_only=False)
2017-11-02 16:03:24,694 p=7253 u=joshv |    7262 1509656604.69401: Loading ShellModule 'sh' from /usr/local/lib/python2.7/dist-packages/ansible/plugins/shell/sh.py (found_in_cache=True, class_only=False)
  7262 1509656604.69887: assigned :doc
2017-11-02 16:03:24,699 p=7253 u=joshv |    7262 1509656604.69887: assigned :doc
  7262 1509656604.69952: Loaded config def from plugin (connection/network_cli)
2017-11-02 16:03:24,699 p=7253 u=joshv |    7262 1509656604.69952: Loaded config def from plugin (connection/network_cli)
2017-11-02 16:03:24,786 paramiko.transport starting thread (client mode): 0x742ed150L
2017-11-02 16:03:24,786 paramiko.transport Local version/idstring: SSH-2.0-paramiko_2.3.1
2017-11-02 16:03:24,787 paramiko.transport Remote version/idstring: SSH-2.0-Cisco-1.25
2017-11-02 16:03:24,787 paramiko.transport Connected (version 2.0, client Cisco-1.25)
2017-11-02 16:03:24,795 paramiko.transport kex algos:[u'diffie-hellman-group-exchange-sha1', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-rsa'] client encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'aes128-cbc', u'3des-cbc', u'aes192-cbc', u'aes256-cbc'] server encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'aes128-cbc', u'3des-cbc', u'aes192-cbc', u'aes256-cbc'] client mac:[u'hmac-sha1', u'hmac-sha1-96'] server mac:[u'hmac-sha1', u'hmac-sha1-96'] client compress:[u'none'] server compress:[u'none'] client lang:[u''] server lang:[u''] kex follows?False
2017-11-02 16:03:24,796 paramiko.transport Kex agreed: diffie-hellman-group-exchange-sha1
2017-11-02 16:03:24,796 paramiko.transport HostKey agreed: ssh-rsa
2017-11-02 16:03:24,796 paramiko.transport Cipher agreed: aes128-ctr
2017-11-02 16:03:24,796 paramiko.transport MAC agreed: hmac-sha1
2017-11-02 16:03:24,796 paramiko.transport Compression agreed: none
2017-11-02 16:03:24,807 paramiko.transport Got server p (2048 bits)
2017-11-02 16:03:25,075 paramiko.transport kex engine KexGex specified hash_algo <built-in function openssl_sha1>
2017-11-02 16:03:25,076 paramiko.transport Switch to new keys ...
2017-11-02 16:03:25,090 p=7253 u=joshv |  connecting to host SYCOMSW0002 returned an error
2017-11-02 16:03:25,090 p=7253 u=joshv |  (22, 'Invalid argument')
2017-11-02 16:03:25,177 paramiko.transport EOF in transport thread
2017-11-02 16:04:04,477 p=7211 u=joshv |    7252 1509656644.47682: handler run complete
2017-11-02 16:04:04,478 p=7211 u=joshv |    7252 1509656644.47803: attempt loop complete, returning result
2017-11-02 16:04:04,478 p=7211 u=joshv |    7252 1509656644.47853: _execute() done
2017-11-02 16:04:04,479 p=7211 u=joshv |    7252 1509656644.47894: dumping result to json
2017-11-02 16:04:04,479 p=7211 u=joshv |    7252 1509656644.47929: done dumping result, returning

```



Discussion:
cc @Qalthos @ganeshrn @gundalow @kedarX @privateip @rcarrillocruz @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
in `/etc/hosts` if I add
`10.8.38.66 IOS02`
and have an inventory with
```
[ios]
IOS02 ansible_connection=local ansible_network_os=ios ansible_user=myuser ansible_ssh_pass=mysecretpw
```

`ansible 2.4.2.0 (stable-2.4 466e4eb892)` fails with:

```
2017-11-15 15:07:13,279 p=28439 u=johnb |  creating new control socket for host IOS02:22 as user ansible
2017-11-15 15:07:13,279 p=28439 u=johnb |  control socket path is /home/johnb/.ansible/pc/5f7110b964
2017-11-15 15:07:13,279 p=28439 u=johnb |  current working directory is /home/johnb/git/ansible-inc/gundalows-test-cases/net-usecase-1-facts-v4
2017-11-15 15:07:13,280 p=28439 u=johnb |  using connection plugin network_cli

...
', u'hmac-sha1-96'] client compress:[u'none'] server compress:[u'none'] client lang:[u''] server lang:[u''] kex follows?False
2017-11-15 15:07:13,735 paramiko.transport Kex agreed: diffie-hellman-group1-sha1
2017-11-15 15:07:13,735 paramiko.transport Cipher agreed: aes128-ctr
2017-11-15 15:07:13,736 paramiko.transport MAC agreed: hmac-sha1-96
2017-11-15 15:07:13,736 paramiko.transport Compression agreed: none
2017-11-15 15:07:14,117 paramiko.transport kex engine KexGroup1 specified hash_algo <built-in function openssl_sha1>
2017-11-15 15:07:14,119 paramiko.transport Switch to new keys ...
2017-11-15 15:07:14,136 p=28439 u=johnb |  connecting to host IOS02 returned an error
```



`devel` hangs
I'll retest this and update

Resolving patch:
diff --git a/lib/ansible/plugins/connection/paramiko_ssh.py b/lib/ansible/plugins/connection/paramiko_ssh.py
--- a/lib/ansible/plugins/connection/paramiko_ssh.py
+++ b/lib/ansible/plugins/connection/paramiko_ssh.py
@@ -324,7 +324,7 @@ def _connect_uncached(self):
                 key_filename = os.path.expanduser(self._play_context.private_key_file)
 
             ssh.connect(
-                self._play_context.remote_addr,
+                self._play_context.remote_addr.lower(),
                 username=self._play_context.remote_user,
                 allow_agent=allow_agent,
                 look_for_keys=self.get_option('look_for_keys'),


Test patch: