Repository: ansible/ansible

Issue:
Module File - touch in check mode end up in with error
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->
When using the File module to touch a file, and run the playbook in check mode, the module will raise the following error "UnboundLocalError: local variable 'diff' referenced before assignment"

This seems to have been introduced by commit 8bd245a6a2853c30d6cce5e21f5421b9758ddfa0

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
file module

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/wada/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->
ANSIBLE_PIPELINING(/etc/ansible/ansible.cfg) = True
DEFAULT_JINJA2_EXTENSIONS(/etc/ansible/ansible.cfg) = jinja2.ext.with_

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, 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.-->
N/A

##### 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. -->
Create a test playbook test.yml
<!--- Paste example playbooks or commands between quotes below -->
```yaml
---

- hosts: 127.0.0.1
  connection: local
  tasks:
    - name: Touch test
      file:
        path: "/tmp/random_item"
        state: touch
```
and run it with check mode enabled => ansible-playbook --check test.yml

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Not having the error when using the check mode x)

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
The task fails with a "UnboundLocalError: local variable 'diff' referenced before assignment" error
<!--- Paste verbatim command output between quotes below -->
```
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_mz6qwS/ansible_module_file.py", line 739, in <module>
    main()
  File "/tmp/ansible_mz6qwS/ansible_module_file.py", line 731, in main
    result = execute_touch(path, follow)
  File "/tmp/ansible_mz6qwS/ansible_module_file.py", line 373, in execute_touch
    return {'dest': path, 'changed': True, 'diff': diff}
UnboundLocalError: local variable 'diff' referenced before assignment

```



Discussion:
Files identified in the description:
* [lib/ansible/modules/files/file.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/files/file.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/files/file.py b/lib/ansible/modules/files/file.py
--- a/lib/ansible/modules/files/file.py
+++ b/lib/ansible/modules/files/file.py
@@ -322,6 +322,9 @@ def execute_touch(path, follow):
     b_path = to_bytes(path, errors='surrogate_or_strict')
     prev_state = get_state(b_path)
 
+    # Unfortunately, touch always changes the file because it updates file's timestamp
+    result = {'dest': path, 'changed': True}
+
     if not module.check_mode:
         if prev_state == 'absent':
             # Create an empty file if the filename did not already exist
@@ -369,8 +372,8 @@ def execute_touch(path, follow):
                     os.remove(b_path)
             raise
 
-    # Unfortunately, touch always changes the file because it updates file's timestamp
-    return {'dest': path, 'changed': True, 'diff': diff}
+        result['diff'] = diff
+    return result
 
 
 def ensure_file_attributes(path, follow):


Test patch: