Repository: ansible/ansible

Issue:
command module  fails when used with integer list element in argv
##### SUMMARY
Using module command with an integer as a list element of argv fails, probably while trying to serialize the integer.

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
module command

##### ANSIBLE VERSION

```
$ ansible --version
ansible 2.6.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/******/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/******/somedir/ansible/lib/ansible
  executable location = /home/******/somedir/ansible/bin/ansible
  python version = 2.7.13 (default, Nov 24 2017, 17:33:09) [GCC 6.3.0 20170516]
```

##### CONFIGURATION
None

##### OS / ENVIRONMENT
Controller: Debian 9 ("stretch")
Servers configured via ansible are CentOS 7.5.

##### STEPS TO REPRODUCE

```yaml
- name: set global options for all gluster volumes
  run_once: true
  command:
  args:
    argv:
      - gluster
      - volume
      - set
      - all
      - cluster.server-quorum-ratio
      - 51
```

##### EXPECTED RESULTS
Integer 51 serialized as a string argument.

##### ACTUAL RESULTS
Module fails with python exception.

```
fatal: [host1.example.invalid]: FAILED! => {"changed": false, "module_stderr": "Shared connection to host1.example.invalid closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_Nv59ki/ansible_module_command.py\", line 271, in <module>\r\n    main()\r\n  File \"/tmp/ansible_Nv59ki/ansible_module_command.py\", line 248, in main\r\n    rc, out, err = module.run_command(args, executable=executable, use_unsafe_shell=shell, encoding=None, data=stdin)\r\n  File \"/tmp/ansible_Nv59ki/ansible_modlib.zip/ansible/module_utils/basic.py\", line 2774, in run_command\r\n  File \"/usr/lib64/python2.7/posixpath.py\", line 293, in expandvars\r\n    if '$' not in path:\r\nTypeError: argument of type 'int' is not iterable\r\n", "msg": "MODULE FAILURE", "rc": 1}
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/commands/command.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/commands/command.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->

Resolving patch:
diff --git a/lib/ansible/modules/commands/command.py b/lib/ansible/modules/commands/command.py
--- a/lib/ansible/modules/commands/command.py
+++ b/lib/ansible/modules/commands/command.py
@@ -133,6 +133,8 @@
 import shlex
 
 from ansible.module_utils.basic import AnsibleModule
+from ansible.module_utils._text import to_native
+from ansible.module_utils.common.collections import is_iterable
 
 
 def check_command(module, commandline):
@@ -215,6 +217,10 @@ def main():
 
     args = args or argv
 
+    # All args must be strings
+    if is_iterable(args, include_strings=False):
+        args = [to_native(arg, errors='surrogate_or_strict', nonstring='simplerepr') for arg in args]
+
     if chdir:
         chdir = os.path.abspath(chdir)
         os.chdir(chdir)


Test patch: