<h5 dir="auto">ISSUE TYPE</h5>

<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">Lookup plugin API, vars, templating?  Not exactly sure.</p>

<h5 dir="auto">ANSIBLE VERSION</h5>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.4.2.0
  config file = None
  configured module search path = [u'/Users/dale/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/dale/.vpy/2.7/lib/python2.7/site-packages/ansible
  executable location = /Users/dale/.vpy/2.7/bin/ansible
  python version = 2.7.14 (default, Sep 22 2017, 00:05:22) [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)]"><pre class="notranslate"><code class="notranslate">ansible 2.4.2.0
  config file = None
  configured module search path = [u'/Users/dale/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/dale/.vpy/2.7/lib/python2.7/site-packages/ansible
  executable location = /Users/dale/.vpy/2.7/bin/ansible
  python version = 2.7.14 (default, Sep 22 2017, 00:05:22) [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)]
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>

<p dir="auto">Default configuration in use</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>

<p dir="auto">Mac OS X 10.11.6</p>
<h5 dir="auto">SUMMARY</h5>

<p dir="auto">Inventory variables (and probably other variables too) seen from a lookup plugin do not have Jinja expressions expanded.</p>
<p dir="auto">I cannot tell if this is related to, or even duplicate of, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="202365770" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/20545" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/20545/hovercard" href="https://github.com/ansible/ansible/issues/20545">#20545</a>.  I'm also not sure if this is a regression of other issues such as <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="38263997" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/8213" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/8213/hovercard" href="https://github.com/ansible/ansible/issues/8213">#8213</a>.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>

<p dir="auto"><code class="notranslate">inventory.yml</code>:</p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="all:
  hosts:
    localhost:
      foo: bar
      baz: '{{ foo }}'"><pre class="notranslate"><span class="pl-ent">all</span>:
  <span class="pl-ent">hosts</span>:
    <span class="pl-ent">localhost</span>:
      <span class="pl-ent">foo</span>: <span class="pl-s">bar</span>
      <span class="pl-ent">baz</span>: <span class="pl-s"><span class="pl-pds">'</span>{{ foo }}<span class="pl-pds">'</span></span></pre></div>
<p dir="auto"><code class="notranslate">lookup_plugins/example_plugin.py</code>:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from ansible.plugins import lookup as ansible_lookup
class LookupModule(ansible_lookup.LookupBase):
    def run(self, terms, variables=None, **kwargs):
        return [variables['baz']]"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">ansible</span>.<span class="pl-s1">plugins</span> <span class="pl-k">import</span> <span class="pl-s1">lookup</span> <span class="pl-k">as</span> <span class="pl-s1">ansible_lookup</span>
<span class="pl-k">class</span> <span class="pl-v">LookupModule</span>(<span class="pl-s1">ansible_lookup</span>.<span class="pl-v">LookupBase</span>):
    <span class="pl-k">def</span> <span class="pl-en">run</span>(<span class="pl-s1">self</span>, <span class="pl-s1">terms</span>, <span class="pl-s1">variables</span><span class="pl-c1">=</span><span class="pl-c1">None</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>):
        <span class="pl-k">return</span> [<span class="pl-s1">variables</span>[<span class="pl-s">'baz'</span>]]</pre></div>
<p dir="auto"><code class="notranslate">site.yml</code>:</p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="- hosts: localhost
  gather_facts: false
  tasks:
    - debug:
        msg: 'should say &quot;bar&quot;: {{ baz }}'
    - debug:
        msg: 'should say &quot;bar&quot;: {{ lookup(&quot;example_plugin&quot;) }}'"><pre class="notranslate">- <span class="pl-ent">hosts</span>: <span class="pl-s">localhost</span>
  <span class="pl-ent">gather_facts</span>: <span class="pl-c1">false</span>
  <span class="pl-ent">tasks</span>:
    - <span class="pl-ent">debug</span>:
        <span class="pl-ent">msg</span>: <span class="pl-s"><span class="pl-pds">'</span>should say "bar": {{ baz }}<span class="pl-pds">'</span></span>
    - <span class="pl-ent">debug</span>:
        <span class="pl-ent">msg</span>: <span class="pl-s"><span class="pl-pds">'</span>should say "bar": {{ lookup("example_plugin") }}<span class="pl-pds">'</span></span></pre></div>

<h5 dir="auto">EXPECTED RESULTS</h5>

<p dir="auto">I expected <code class="notranslate">{{ foo }}</code> to be expanded like so:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ ansible-playbook -i inventory.yml -l localhost site.yml

PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
ok: [localhost] =&gt; {
    &quot;msg&quot;: &quot;should say \&quot;bar\&quot;: bar&quot;
}

TASK [debug] *******************************************************************
ok: [localhost] =&gt; {
    &quot;msg&quot;: &quot;should say \&quot;bar\&quot;: bar&quot;
}"><pre class="notranslate"><code class="notranslate">$ ansible-playbook -i inventory.yml -l localhost site.yml

PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
ok: [localhost] =&gt; {
    "msg": "should say \"bar\": bar"
}

TASK [debug] *******************************************************************
ok: [localhost] =&gt; {
    "msg": "should say \"bar\": bar"
}
</code></pre></div>
<h5 dir="auto">ACTUAL RESULTS</h5>

<p dir="auto">The lookup plugin sees <code class="notranslate">{{ foo }}</code> instead of the expansion of that into <code class="notranslate">bar</code>:</p>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ ansible-playbook -i inventory.yml -l localhost site.yml

PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
ok: [localhost] =&gt; {
    &quot;msg&quot;: &quot;should say \&quot;bar\&quot;: bar&quot;
}

TASK [debug] *******************************************************************
ok: [localhost] =&gt; {
    &quot;msg&quot;: &quot;should say \&quot;bar\&quot;: {{ foo }}&quot;
}"><pre class="notranslate"><code class="notranslate">$ ansible-playbook -i inventory.yml -l localhost site.yml

PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
ok: [localhost] =&gt; {
    "msg": "should say \"bar\": bar"
}

TASK [debug] *******************************************************************
ok: [localhost] =&gt; {
    "msg": "should say \"bar\": {{ foo }}"
}
</code></pre></div>
<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.1.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides"><pre class="notranslate"><code class="notranslate">ansible 2.1.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<p dir="auto">[ssh_connection]<br>
ssh_args = -o ControlMaster=no -o ControlPersist=60s</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">Control machine: OSX El Capitan latest<br>
Target Machine: VMWare Debian 3.13.0-91-generic</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto"><code class="notranslate">delegate_to</code> causes wrong user to be used to connect to target machine</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">I have an inventory file as such:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="build_machine ansible_host=localhost ansible_connection=local
staging_machine ansible_host=my.staging.host ansible_user=stager"><pre class="notranslate"><code class="notranslate">build_machine ansible_host=localhost ansible_connection=local
staging_machine ansible_host=my.staging.host ansible_user=stager
</code></pre></div>
<p dir="auto">I have a playbook running synchronize:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- name: Copy build to staging
  hosts: staging_machine
  tasks:
    - synchronize: src=somefolder dest=otherfolder
      delegate_to: staging_machine
      remote_user: stager"><pre class="notranslate"><code class="notranslate">- name: Copy build to staging
  hosts: staging_machine
  tasks:
    - synchronize: src=somefolder dest=otherfolder
      delegate_to: staging_machine
      remote_user: stager
</code></pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">I expected the playbook to synchronize folders between build and staging machine.</p>
<h5 dir="auto">ACTUAL RESULTS</h5>
<p dir="auto">The task prompts for password of the wrong user:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="local-mac-user@my-staging-host's password:"><pre class="notranslate"><code class="notranslate">local-mac-user@my-staging-host's password:
</code></pre></div>
<p dir="auto">Playbook works with version <code class="notranslate">2.0.2.0</code>.</p>