Repository: ansible/molecule

Issue:
ansible-lint called from Molecule fails when no Ansible-installed-with-pip is present
# Issue Type

- Bug report

# Molecule and Ansible details

```
# ansible --version
ansible 2.2.1.0 (stable-2.2 acad2ba246) last updated 2016/12/11 20:27:02 (GMT +900)
  lib/ansible/modules/core: (detached HEAD 8139278530) last updated 2016/12/11 20:30:10 (GMT +900)
  lib/ansible/modules/extras: (detached HEAD f5f1fc934a) last updated 2016/12/11 20:30:10 (GMT +900)
  config file =
  configured module search path = Default w/o overrides
# molecule --version
molecule, version 1.16.1
```

- Molecule installation method: pip
- Ansible installation method: source

# Desired Behaviour

```
# molecule verify
--> Executing ansible-lint...
[ANSIBLE0002] Trailing whitespace
playbook.yml:7
    - ansible-unix-python-environment
```

# Actual Behaviour (Bug report only)

```
# pip uninstall ansible
((( cut )))
  Successfully uninstalled ansible-2.2.0.0

# . /usr/local/src/ansible/hacking/env-setup
((( cut )))
PYTHONPATH=/usr/local/src/ansible/lib:
((( cut )))

# ansible --version
ansible 2.2.1.0 (stable-2.2 acad2ba246) last updated 2016/12/11 20:27:02 (GMT +900)
  lib/ansible/modules/core: (detached HEAD 8139278530) last updated 2016/12/11 20:30:10 (GMT +900)
  lib/ansible/modules/extras: (detached HEAD f5f1fc934a) last updated 2016/12/11 20:30:10 (GMT +900)
  config file =
  configured module search path = Default w/o overrides

# molecule --debug verify
--> Executing ansible-lint...
DEBUG: COMMAND
/usr/local/bin/ansible-lint playbook.yml --exclude .git --exclude .vagrant --exclude .molecule
Traceback (most recent call last):
  File "/usr/local/bin/ansible-lint", line 30, in <module>
    import ansiblelint
  File "/usr/local/lib/python2.7/site-packages/ansiblelint/__init__.py", line 26, in <module>
    import ansiblelint.utils
  File "/usr/local/lib/python2.7/site-packages/ansiblelint/utils.py", line 25, in <module>
    import ansible.constants as C
ImportError: No module named ansible.constants

# /usr/local/bin/ansible-lint playbook.yml --exclude .git --exclude .vagrant --exclude .molecule
[ANSIBLE0002] Trailing whitespace
playbook.yml:7
    - ansible-unix-python-environment
```

# Further tests

With Ansible 2.2.0 installed with `pip` (regardless if the one from source configured or not; configured in the example below):

```
# pip install ansible
((( cut )))
Successfully installed ansible-2.2.0.0

# . /usr/local/src/ansible/hacking/env-setup
((( cut )))

# ansible --version
ansible 2.2.1.0 (stable-2.2 acad2ba246) last updated 2016/12/11 20:27:02 (GMT +900)
  lib/ansible/modules/core: (detached HEAD 8139278530) last updated 2016/12/11 20:30:10 (GMT +900)
  lib/ansible/modules/extras: (detached HEAD f5f1fc934a) last updated 2016/12/11 20:30:10 (GMT +900)
  config file =
  configured module search path = Default w/o overrides

# molecule verify
--> Executing ansible-lint...
[ANSIBLE0002] Trailing whitespace
playbook.yml:7
    - ansible-unix-python-environment
```


Discussion:
I don't think there is anything I can do about this.  I don't really recommend running projects from source, or mixing pip and source.  I really don't see what I can do about this.  This looks more like a python issue than molecule.

What would I do to solve this?  Molecule is a standard python project.
Ansible **is** installed (and configured) all the time.

The problem is if **Ansible-installed-with-pip** is present or not.

If it's not a Molecule problem, then why `ansible-lint` singlehandedly works ok? Only called from Molecule fails...

Great that you don't recommend running from source, but latest stable Ansible version is 2.2.1 and latest in pip repo is 2.2.0 which has bugs.
Not sure what to tell you.  No idea how you would like this fixed.
The fix seems pretty simple: take out the reference that you somehow, somewhere hardcoded and use environment variables as received from the shell.

There is no reason the same command (which Molecule prints it executes):
```
/usr/local/bin/ansible-lint playbook.yml --exclude .git --exclude .vagrant --exclude .molecule
```
succeeds from shell, but fails from Molecule.

I don't know why it is so difficult to understand that if the command fails **only** when called from Molecule, then something is wrong with Molecule, not the whole world except for Molecule.
Nothing is hard coded.

`/usr/local/bin/ansible-lint` is determined by your path by the [sh module](https://github.com/metacloud/molecule/blob/ba5aca1f86c85432ba3f35031937eadbbaf07383/molecule/verifier/ansible_lint.py#L60).

It is a simple PATH lookup performed by the sh module.  Again, I've done no hardcoding.
**Kind request**: read before you respond.

No one ever said Molecule fails to execute `ansible-lint`.

I repeat for the third time:

- Molecule executes `ansible-lint` and **then** `ansible-lint` fails.

- Molecule with debug option prints out the exact command it ran

- When executed using **the same** command call from shell `ansible-lint` **does not** fail

Considering the command is the same, the executable is the same and `ansible-lint`  fails (its operations fail) **only** with Molecule, the only difference is the environment in which you run it (but I'm open to hearing of other possibilities).

Now please go back and **do read** my posts. Your response with the path to the executable only proves you did not take time read them.

I even included the most probable environment variable which might be causing the problem (but I'm classless about it) in my initial request - did you notice at all? Or did you stop reading after the subject line?
I'm not fixing it.  Nor do I see a problem.  I read your issue many times, and it makes no sense to me.  If you are unhappy with the project or my attention to your issue, open a PR.

You should also pay attention to your tone in your issues.  This isn't the first issue you have sent our way, with rather salty wording.

Resolving patch:
diff --git a/molecule/verifier/ansible_lint.py b/molecule/verifier/ansible_lint.py
--- a/molecule/verifier/ansible_lint.py
+++ b/molecule/verifier/ansible_lint.py
@@ -49,6 +49,7 @@ def execute(self):
         env = {
             'ANSIBLE_CONFIG':
             self._molecule.config.config['ansible']['config_file'],
+            'PYTHONPATH': os.environ.get('PYTHONPATH'),
             'HOME': os.environ.get('HOME')
         }
 


Test patch: