Repository: ansible/ansible

Issue:
`groups['groupname']|length` in group_vars evaluates incorrectly in some contexts
Given this inventory:

```
# inv
[group]
localhost
```

And this `group_vars/all`:

```

---
intermediate_value: "{{ groups['group']|length }}"
shard_size: "{{ 256 // intermediate_value|int }}"
```

I would expect this to happen (and it does!)

```
$ ansible all -i inv -m debug -a var=shard_size
localhost | success >> {
    "var": {
        "shard_size": "256"
    }
}
```

However, if I do this (which, does not really involve templating):

```
ansible all -i inv -m copy -a "content=foo dest=/tmp/bar"
```

I get this beauty:

```
localhost | FAILED => Traceback (most recent call last):
  File "/home/zzz/src/ansible/lib/ansible/runner/__init__.py", line 590, in _executor
    exec_rc = self._executor_internal(host, new_stdin)
  File "/home/zzz/src/ansible/lib/ansible/runner/__init__.py", line 792, in _executor_internal
    return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=complex_args)
  File "/home/zzz/src/ansible/lib/ansible/runner/__init__.py", line 1025, in _executor_internal_inner
    result = handler.run(conn, tmp, module_name, module_args, inject, complex_args)
  File "/home/zzz/src/ansible/lib/ansible/runner/action_plugins/copy.py", line 181, in run
    remote_checksum = self.runner._remote_checksum(conn, tmp_path, dest_file, inject)
  File "/home/zzz/src/ansible/lib/ansible/runner/__init__.py", line 1248, in _remote_checksum
    python_interp = inject['hostvars'][host].get('ansible_python_interpreter', 'python')
  File "/home/zzz/src/ansible/lib/ansible/runner/__init__.py", line 105, in __getitem__
    self.lookup[host] = template.template('.', result, self.vars_cache)
  File "/home/zzz/src/ansible/lib/ansible/utils/template.py", line 132, in template
    d[k] = template(basedir, v, templatevars, lookup_fatal, depth, expand_lists, convert_bare, fail_on_undefined, filter_fatal)
  File "/home/zzz/src/ansible/lib/ansible/utils/template.py", line 118, in template
    varname = template_from_string(basedir, varname, templatevars, fail_on_undefined)
  File "/home/zzz/src/ansible/lib/ansible/utils/template.py", line 364, in template_from_string
    res = jinja2.utils.concat(rf)
  File "<template>", line 9, in root
ZeroDivisionError: integer division or modulo by zero
```

This is true in 1.8.2-1.9 (devel 5ed7a55990), this was not true in 1.7 or 1.6 or 1.8.0



Discussion:
a little git bisect, and I've found the culprit: 4cd87ec93ec1f5266e5afaaa8e6734d642284afe, a fix for #9684


Resolving patch:
diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py
--- a/lib/ansible/runner/__init__.py
+++ b/lib/ansible/runner/__init__.py
@@ -678,6 +678,7 @@ def _executor_internal(self, host, new_stdin):
         temp_vars = utils.merge_hash(temp_vars, self.play_vars)
         temp_vars = utils.merge_hash(temp_vars, self.play_file_vars)
         temp_vars = utils.merge_hash(temp_vars, self.extra_vars)
+        temp_vars = utils.merge_hash(temp_vars, {'groups': inject['groups']})
 
         hostvars = HostVars(temp_vars, self.inventory, vault_password=self.vault_pass)
 


Test patch: