Repository: ansible/ansible
Pull request: Fix referenced before assignment in role requirements
##### SUMMARY
If the SCM provider is missing during role require, `run_scm_cmd()` throws an uncaught Unbound exception error:

```
ERROR! Unexpected Exception, this is probably a bug: local variable 'stdout' referenced before assignment
the full traceback was:

Traceback (most recent call last):
  File "/usr/bin/ansible-galaxy", line 118, in <module>
    exit_code = cli.run()
  File "/usr/lib/python2.7/site-packages/ansible/cli/galaxy.py", line 152, in run
    self.execute()
  File "/usr/lib/python2.7/site-packages/ansible/cli/__init__.py", line 155, in execute
    fn()
  File "/usr/lib/python2.7/site-packages/ansible/cli/galaxy.py", line 395, in execute_install
    installed = role.install()
  File "/usr/lib/python2.7/site-packages/ansible/galaxy/role.py", line 200, in install
    tmp_file = RoleRequirement.scm_archive_role(keep_scm_meta=self.options.keep_scm_meta, **self.spec)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/role/requirement.py", line 208, in scm_archive_role
    run_scm_cmd(clone_cmd, tempdir)
  File "/usr/lib/python2.7/site-packages/ansible/playbook/role/requirement.py", line 197, in run_scm_cmd
    display.debug("\tstdout: " + stdout)
UnboundLocalError: local variable 'stdout' referenced before assignment
```

##### ISSUE TYPE
Bugfix Pull Request

##### COMPONENT NAME
lib/ansible/playbook/role/requirement.py

##### ANSIBLE VERSION
```
2.6.1
```


##### ADDITIONAL INFORMATION
```

```
fixes #43312

Problem statement:
Misleading error from ansible-galaxy if 'git' is missing
##### SUMMARY

When using ansible galaxy with a missing git binary, the error message is very misleading:

```
ERROR! Unexpected Exception, this is probably a bug: local variable 'stdout' referenced before assignment
``` 

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
ansible-galaxy

##### ANSIBLE VERSION

```
2.6.1
```

##### CONFIGURATION

##### OS / ENVIRONMENT
N/A
##### STEPS TO REPRODUCE
use a system without git binary installed, need a proper `requirements` file
```
ansible-galaxy install -r requirements.txt
```

##### EXPECTED RESULTS
`git not found` or similar

##### ACTUAL RESULTS
```
ERROR! Unexpected Exception, this is probably a bug: local variable 'stdout' referenced before assignment
```

Hints from issue discussion:

Commit message: Fix referenced before assignment in role requirements

Patch to review:
diff --git a/lib/ansible/playbook/role/requirement.py b/lib/ansible/playbook/role/requirement.py
index f41bf8968ff584..862e4f00da15cc 100644
--- a/lib/ansible/playbook/role/requirement.py
+++ b/lib/ansible/playbook/role/requirement.py
@@ -189,6 +189,8 @@ def scm_archive_role(src, scm='git', name=None, version='HEAD', keep_scm_meta=Fa
 
         def run_scm_cmd(cmd, tempdir):
             try:
+                stdout = None
+                stderr = None
                 popen = Popen(cmd, cwd=tempdir, stdout=PIPE, stderr=PIPE)
                 stdout, stderr = popen.communicate()
             except Exception as e:


Code review comments:
diff_hunk: @@ -189,6 +189,8 @@ def scm_archive_role(src, scm='git', name=None, version='HEAD', keep_scm_meta=Fa
 
         def run_scm_cmd(cmd, tempdir):
             try:
+                stdout = None
+                stderr = None
path: lib/ansible/playbook/role/requirement.py
text: @user1:
this will create a concatenation error if they are still 'None' in the exception handling, initialize as '' (empty string)