<p dir="auto">[Note: My primary motivation for creating this issue is to:</p>
<ol dir="auto">
<li>Create a link between the potentially impacted projects (because the symptoms make it difficult to ascertain where the problem lies) i.e. Ansible + virtualenv (+ Homebrew); and,</li>
<li>Document a workaround/solution where it might be found.]</li>
</ol>
<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Documentation Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">Installation of Ansible into a virtual environment (optionally, via Homebrew)</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.3.2.0
  config file = 
  configured module search path = Default w/o overrides
  python version = 2.7.2 (default, Oct 11 2012, 20:14:37) [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]"><pre class="notranslate"><code class="notranslate">ansible 2.3.2.0
  config file = 
  configured module search path = Default w/o overrides
  python version = 2.7.2 (default, Oct 11 2012, 20:14:37) [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<p dir="auto">N/A</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">Ansible was installed via Homebrew which uses virtualenv. Output of <code class="notranslate">brew --version</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Homebrew 1.3.2-25-g914378c
Homebrew/homebrew-core (git revision 9a5c8; last commit 2017-09-09)"><pre class="notranslate"><code class="notranslate">Homebrew 1.3.2-25-g914378c
Homebrew/homebrew-core (git revision 9a5c8; last commit 2017-09-09)
</code></pre></div>
<p dir="auto">Output of <code class="notranslate">brew info ansible</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible: stable 2.3.2.0, HEAD
Automate deployment, configuration, and upgrading
https://www.ansible.com/
/usr/local/Cellar/ansible/2.3.2.0_1 (17,942 files, 187.8MB) *
  Built from source on 2017-09-10 at 18:00:29
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/ansible.rb
==&gt; Dependencies
Build: pkg-config ✔
Required: libyaml ✔, openssl@1.1 ✔"><pre class="notranslate"><code class="notranslate">ansible: stable 2.3.2.0, HEAD
Automate deployment, configuration, and upgrading
https://www.ansible.com/
/usr/local/Cellar/ansible/2.3.2.0_1 (17,942 files, 187.8MB) *
  Built from source on 2017-09-10 at 18:00:29
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/ansible.rb
==&gt; Dependencies
Build: pkg-config ✔
Required: libyaml ✔, openssl@1.1 ✔
</code></pre></div>
<p dir="auto">Disclosure: This is being run on an (unsupported by Homebrew and pretty much everyone else) Mac OS X 10.8 machine but AFAICT that is not connected to the issue.</p>
<p dir="auto">The issue occurs before any control machine is contacted.</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">Attempting to use the <code class="notranslate">ec2.py</code> inventory script (<a href="http://docs.ansible.com/ansible/latest/intro_dynamic_inventory.html#example-aws-ec2-external-inventory-script" rel="nofollow">via</a>) results in an error when used with an Ansible instance installed into a virtualenv (via Homebrew, in my specific case) that is not activated.</p>
<p dir="auto">The error occurs because the <code class="notranslate">ec2.py</code> script cannot find the <code class="notranslate">ansible</code> Python module it expects because it is not being executed by the same Python interpreter binary/configuration as the <code class="notranslate">ansible</code> tool.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">As far as I recall (modulo a compilation issue I've not yet reported to Homebrew):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="brew install ansible
# Download https://raw.github.com/ansible/ansible/devel/contrib/inventory/ec2.py
ansible -i ec2.py --list-hosts all"><pre class="notranslate"><code class="notranslate">brew install ansible
# Download https://raw.github.com/ansible/ansible/devel/contrib/inventory/ec2.py
ansible -i ec2.py --list-hosts all
</code></pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">Expected either:</p>
<ul dir="auto">
<li>
<p dir="auto">A list of hosts; or,</p>
</li>
<li>
<p dir="auto">an error message <code class="notranslate">boto.exception.NoAuthHandlerFound: No handler was ready to authenticate</code> which occurs when the script is executed correctly but no authorisation credentials are provided.</p>
</li>
</ul>
<h5 dir="auto">ACTUAL RESULTS</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    ERROR! Attempted to execute &quot;ec2.py&quot; as inventory script: Inventory script (ec2.py) had an execution error: Traceback (most recent call last):
      File &quot;/&lt;path&gt;/ec2.py&quot;, line 135, in &lt;module&gt;
        from ansible.module_utils import ec2 as ec2_utils
    ImportError: No module named ansible.module_utils"><pre class="notranslate"><code class="notranslate">    ERROR! Attempted to execute "ec2.py" as inventory script: Inventory script (ec2.py) had an execution error: Traceback (most recent call last):
      File "/&lt;path&gt;/ec2.py", line 135, in &lt;module&gt;
        from ansible.module_utils import ec2 as ec2_utils
    ImportError: No module named ansible.module_utils
</code></pre></div>
<h5 dir="auto">Explanation</h5>
<p dir="auto">There is much more relevant detail/context given in the associated issues in the other projects (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="257930613" data-permission-text="Title is private" data-url="https://github.com/pypa/virtualenv/issues/1080" data-hovercard-type="pull_request" data-hovercard-url="/pypa/virtualenv/pull/1080/hovercard" href="https://github.com/pypa/virtualenv/pull/1080">pypa/virtualenv#1080</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="257936128" data-permission-text="Title is private" data-url="https://github.com/pypa/virtualenv/issues/1081" data-hovercard-type="issue" data-hovercard-url="/pypa/virtualenv/issues/1081/hovercard" href="https://github.com/pypa/virtualenv/issues/1081">pypa/virtualenv#1081</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="258353856" data-permission-text="Title is private" data-url="https://github.com/Homebrew/brew/issues/3167" data-hovercard-type="issue" data-hovercard-url="/Homebrew/brew/issues/3167/hovercard" href="https://github.com/Homebrew/brew/issues/3167">Homebrew/brew#3167</a>) which for the sake of brevity I won't repeat here but the summary is:</p>
<ul dir="auto">
<li>virtualenv intentionally distinguishes between "using" and "activating" a virtualenv.</li>
<li>Executing the Python interpreter binary within the virtualenv directly (without "activating" the virtualenv) only "uses" the virtualenv, it doesn't automatically "activate" it.</li>
<li>There is a potential difference in behaviour between "using" a virtualenv and "activating and then using" a virtualenv if a Python script with an <code class="notranslate">env</code>-based shebang line is executed as a child process of a script executed by the Python interpreter located within the virtualenv.</li>
<li>Specifically:
<ul dir="auto">
<li>Manually "activating" a virtualenv changes the value of <code class="notranslate">PATH</code> so that the Python interpreter binary located within the virtualenv is used and this change is propagated to child processes and affects the action of <code class="notranslate">/usr/bin/env</code>.</li>
<li>Merely "using" an unactivated virtualenv does not modify <code class="notranslate">PATH</code> so in this situation any child process relying on <code class="notranslate">env</code> uses the default Python interpreter binary (and associated libraries).</li>
</ul>
</li>
<li>Homebrew installs Ansible in a virtualenv and the <code class="notranslate">ansible</code> command is configured to use the Python interpreter located within the virtualenv directly:</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ head -1 `which ansible`
#!/usr/local/Cellar/ansible/2.3.2.0_1/libexec/bin/python"><pre class="notranslate"><code class="notranslate">$ head -1 `which ansible`
#!/usr/local/Cellar/ansible/2.3.2.0_1/libexec/bin/python
</code></pre></div>
<ul dir="auto">
<li>The <code class="notranslate">ec2.py</code> inventory script uses a <code class="notranslate">env</code>-based shebang line and <a href="https://github.com/ansible/ansible/issues/17147#issuecomment-241029301" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/17147/hovercard">now assumes</a> it is using the same Python interpreter binary/configuration as <code class="notranslate">ansible</code> tool because it attempts to import from the <code class="notranslate">ansible.module_utils</code> module.</li>
<li>But because the Ansible virtualenv is only being "used" and not "activated and then used" the <code class="notranslate">ec2.py</code> script is not executed by the same Python interpreter binary/configuration as the <code class="notranslate">ansible</code> tool.</li>
</ul>
<h5 dir="auto">A potential solution/workaround (and alternative to an activation wrapper script in other situations)</h5>
<p dir="auto">The solution/workaround I proposed in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="257936128" data-permission-text="Title is private" data-url="https://github.com/pypa/virtualenv/issues/1081" data-hovercard-type="issue" data-hovercard-url="/pypa/virtualenv/issues/1081/hovercard" href="https://github.com/pypa/virtualenv/issues/1081">pypa/virtualenv#1081</a>:</p>
<blockquote>
<p dir="auto">The existing modification of <code class="notranslate">sys.path</code> by virtualenv is achieved by way of a <a href="https://github.com/pypa/virtualenv/blob/master/virtualenv_embedded/site.py">custom <code class="notranslate">site.py</code> file</a>. Researching this approach led to the discovery that further customization is made possible by the use of a <a href="https://docs.python.org/2/library/site.html" rel="nofollow"><code class="notranslate">sitecustomize</code> module</a> located in the <code class="notranslate">site-packages</code> folder of the virtualenv's Python installation.</p>
<p dir="auto">Thus, the required modification of the <code class="notranslate">PATH</code> and other environment variables can be achieved via the creation of <code class="notranslate">/&lt;virtualenv path&gt;/lib/python2.7/site-packages/sitecustomize.py</code> (based on <code class="notranslate">activate_this.py</code>):</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import os
import sys 

old_os_path = os.environ.get('PATH', '')
os.environ['PATH'] = os.path.dirname(os.path.abspath(sys.executable)) + os.pathsep + old_os_path"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">os</span>
<span class="pl-k">import</span> <span class="pl-s1">sys</span> 

<span class="pl-s1">old_os_path</span> <span class="pl-c1">=</span> <span class="pl-s1">os</span>.<span class="pl-s1">environ</span>.<span class="pl-en">get</span>(<span class="pl-s">'PATH'</span>, <span class="pl-s">''</span>)
<span class="pl-s1">os</span>.<span class="pl-s1">environ</span>[<span class="pl-s">'PATH'</span>] <span class="pl-c1">=</span> <span class="pl-s1">os</span>.<span class="pl-s1">path</span>.<span class="pl-en">dirname</span>(<span class="pl-s1">os</span>.<span class="pl-s1">path</span>.<span class="pl-en">abspath</span>(<span class="pl-s1">sys</span>.<span class="pl-s1">executable</span>)) <span class="pl-c1">+</span> <span class="pl-s1">os</span>.<span class="pl-s1">pathsep</span> <span class="pl-c1">+</span> <span class="pl-s1">old_os_path</span></pre></div>
<p dir="auto">(Clearly this is just a quick proof-of-concept, to be robust the code would need to avoid duplication of paths and also modify the other environment variables.)</p>
</blockquote>
<p dir="auto">This approach can also function as an alternative to activation wrapper scripts that are used in other situations when the "automatic activation before use" of a virtualenv is desired when the Python interpreter located within the virtualenv is executed directly.</p>
<h5 dir="auto">Current status</h5>
<p dir="auto">From the conversation in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="257930613" data-permission-text="Title is private" data-url="https://github.com/pypa/virtualenv/issues/1080" data-hovercard-type="pull_request" data-hovercard-url="/pypa/virtualenv/pull/1080/hovercard" href="https://github.com/pypa/virtualenv/pull/1080">pypa/virtualenv#1080</a> it now appears that the <code class="notranslate">virtualenv</code> project is unlikely to incorporate the proposed solution due to (as I understand it): (a) the issue being seen as specific to Ansible and/or Homebrew's packaging; and, (b) my proposed solution seeming too "magic" and non-obvious to people who may encounter it.</p>
<p dir="auto">I have now <a href="https://github.com/Homebrew/brew/issues/3167" data-hovercard-type="issue" data-hovercard-url="/Homebrew/brew/issues/3167/hovercard">proposed the solution to Homebrew</a> for use in the tool itself or as part of the formula for Ansible but as the issue affects non-Homebrew Ansible virtualenv installations I wanted to at least document the approach here.</p>
<p dir="auto">It may be appropriate to add a note about the issue/solution to the docs or, at least, add a warning to <a href="http://docs.ansible.com/ansible/latest/intro_installation.html#latest-releases-via-pip" rel="nofollow">http://docs.ansible.com/ansible/latest/intro_installation.html#latest-releases-via-pip</a> where it says "Readers that use virtualenv can also install Ansible under virtualenv...".</p>
<h5 dir="auto">Related issues</h5>
<ul dir="auto">
<li>Likely: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="172106208" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/17147" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/17147/hovercard" href="https://github.com/ansible/ansible/issues/17147">#17147</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="96453904" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/11689" data-hovercard-type="pull_request" data-hovercard-url="/ansible/ansible/pull/11689/hovercard" href="https://github.com/ansible/ansible/pull/11689">#11689</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="24513988" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/5335" data-hovercard-type="pull_request" data-hovercard-url="/ansible/ansible/pull/5335/hovercard" href="https://github.com/ansible/ansible/pull/5335">#5335</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="60915089" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/10452" data-hovercard-type="pull_request" data-hovercard-url="/ansible/ansible/pull/10452/hovercard" href="https://github.com/ansible/ansible/pull/10452">#10452</a></li>
<li>Possibly <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="187915002" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/18414" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/18414/hovercard" href="https://github.com/ansible/ansible/issues/18414">#18414</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="59684351" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/10378" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/10378/hovercard" href="https://github.com/ansible/ansible/issues/10378">#10378</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="257040524" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/29987" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/29987/hovercard" href="https://github.com/ansible/ansible/issues/29987">#29987</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="185799884" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/18227" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/18227/hovercard" href="https://github.com/ansible/ansible/issues/18227">#18227</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="165837873" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/16724" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/16724/hovercard" href="https://github.com/ansible/ansible/issues/16724">#16724</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="141573527" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/15019" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/15019/hovercard" href="https://github.com/ansible/ansible/issues/15019">#15019</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="19307908" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/4083" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/4083/hovercard" href="https://github.com/ansible/ansible/issues/4083">#4083</a></li>
</ul>
<p dir="auto">Thanks for your time.</p>
<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">ansible-galaxy</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.3.0.0
  config file = /Users/callen/.ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, Dec 17 2016, 23:03:43) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]"><pre class="notranslate"><code class="notranslate">ansible 2.3.0.0
  config file = /Users/callen/.ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, Dec 17 2016, 23:03:43) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<p dir="auto">Nothing special.</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">macOS Sierra 10.12.4</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">When using <code class="notranslate">ansible-galaxy</code> to check out a <code class="notranslate">requirements.yml</code> role, if a template inside the role contains the name of the role, the template file gets mangled during checkout. It should leave files named as they are in the repo.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">Create a <code class="notranslate">requirements.yml</code> file with a single, simple role reference:</p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="---
- src: https://github.com/commandtab/nginx.git
  scm: git
  version: 1.0.0"><pre class="notranslate">---
- <span class="pl-ent">src</span>: <span class="pl-s">https://github.com/commandtab/nginx.git</span>
  <span class="pl-ent">scm</span>: <span class="pl-s">git</span>
  <span class="pl-ent">version</span>: <span class="pl-s">1.0.0</span></pre></div>
<p dir="auto">Install the role:</p>
<p dir="auto"><code class="notranslate">ansible-galaxy install -r requirements.yml --force</code></p>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">I expect the role to be cloned from the git repo to disk, leaving all filenames intact.</p>
<h5 dir="auto">ACTUAL RESULTS</h5>
<p dir="auto">If you compare the contents of the <a href="https://github.com/commandtab/nginx/tree/master/templates">role git repo's <code class="notranslate">templates/</code> directory</a> to the checked-out role, the <code class="notranslate">templates/</code> directory on disk has had two of the files renamed:</p>
<ul dir="auto">
<li><code class="notranslate">nginx.conf.j2</code> became <code class="notranslate">.conf.j2</code></li>
<li><code class="notranslate">nginx.repo.j2</code> became <code class="notranslate">.repo.j2</code></li>
</ul>
<p dir="auto">Note that <code class="notranslate">mime.types.j2</code> was not renamed.</p>