<p dir="auto">We have two mysql masters and I wanted to create an active/passive group based on which server has the <a href="https://en.wikipedia.org/wiki/Virtual_IP_address" rel="nofollow">VIP</a>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# ansible all -i inventory.py --limit tiers-production-app-mysql -m setup -a 'filter=ansible_eth1_1'
db-02.example.com | success &gt;&gt; {
    &quot;ansible_facts&quot;: {}, 
    &quot;changed&quot;: false
}

db-01.example.com | success &gt;&gt; {
    &quot;ansible_facts&quot;: {
        &quot;ansible_eth1_1&quot;: {
            &quot;ipv4&quot;: {
                &quot;address&quot;: &quot;10.0.2.75&quot;, 
                &quot;netmask&quot;: &quot;255.255.255.255&quot;, 
                &quot;network&quot;: &quot;10.0.2.75&quot;
            }
        }
    }, 
    &quot;changed&quot;: false"><pre class="notranslate"><code class="notranslate"># ansible all -i inventory.py --limit tiers-production-app-mysql -m setup -a 'filter=ansible_eth1_1'
db-02.example.com | success &gt;&gt; {
    "ansible_facts": {}, 
    "changed": false
}

db-01.example.com | success &gt;&gt; {
    "ansible_facts": {
        "ansible_eth1_1": {
            "ipv4": {
                "address": "10.0.2.75", 
                "netmask": "255.255.255.255", 
                "network": "10.0.2.75"
            }
        }
    }, 
    "changed": false
</code></pre></div>
<p dir="auto">So in the cause above, <code class="notranslate">db-01.example.com</code> is the master.</p>
<p dir="auto">The debug playbook below shows the correct group names:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# ansible-playbook -i inventory.py all-debug.yml --limit tiers-production-app-mysql -vv;

PLAY [all] ******************************************************************** 

GATHERING FACTS *************************************************************** 
&lt;1.2.3.75&gt; REMOTE_MODULE setup
&lt;1.2.3.76&gt; REMOTE_MODULE setup
ok: [db-02.example.com]
ok: [db-01.example.com]

TASK: [debug ] **************************************************************** 
ok: [db-01.example.com] =&gt; {
    &quot;msg&quot;: &quot;tiers-production-app-mysql-replication-master-active&quot;
}
ok: [db-02.example.com] =&gt; {
    &quot;msg&quot;: &quot;tiers-production-app-mysql-replication-master-passive&quot;
}"><pre class="notranslate"><code class="notranslate"># ansible-playbook -i inventory.py all-debug.yml --limit tiers-production-app-mysql -vv;

PLAY [all] ******************************************************************** 

GATHERING FACTS *************************************************************** 
&lt;1.2.3.75&gt; REMOTE_MODULE setup
&lt;1.2.3.76&gt; REMOTE_MODULE setup
ok: [db-02.example.com]
ok: [db-01.example.com]

TASK: [debug ] **************************************************************** 
ok: [db-01.example.com] =&gt; {
    "msg": "tiers-production-app-mysql-replication-master-active"
}
ok: [db-02.example.com] =&gt; {
    "msg": "tiers-production-app-mysql-replication-master-passive"
}
</code></pre></div>
<p dir="auto">Now comes the strange thing. I expected the <code class="notranslate">group_by</code> statement below to work:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    - name: create dynamic active / passive groups
      group_by:
        key: &quot;tiers-production-app-mysql-replication-master-{{ 'active' if ansible_eth1_1 is defined else 'passive' }}&quot;"><pre class="notranslate"><code class="notranslate">    - name: create dynamic active / passive groups
      group_by:
        key: "tiers-production-app-mysql-replication-master-{{ 'active' if ansible_eth1_1 is defined else 'passive' }}"
</code></pre></div>
<p dir="auto">But <strong>it doesn't</strong>, the following statement does:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    - name: create dynamic active / passive groups
      group_by:
        key: &quot;tiers-production-app-mysql-replication-master-{{ 'active' if hostvars[inventory_hostname]['ansible_eth1_1'] is defined else 'passive' }}&quot;"><pre class="notranslate"><code class="notranslate">    - name: create dynamic active / passive groups
      group_by:
        key: "tiers-production-app-mysql-replication-master-{{ 'active' if hostvars[inventory_hostname]['ansible_eth1_1'] is defined else 'passive' }}"
</code></pre></div>
<p dir="auto">When I debug <code class="notranslate">ansible_eth1_1</code> (use it as a group name), both machines have the <strong>same values</strong>. So it looks like variables are not been reset.</p>
<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 1.9.2
  configured module search path = None"><pre class="notranslate"><code class="notranslate">ansible 1.9.2
  configured module search path = None
</code></pre></div>
<h5 dir="auto">Ansible Configuration:</h5>
<p dir="auto">Reproduced with default config.</p>
<h5 dir="auto">Environment:</h5>
<p dir="auto">Solaris 10, 11.</p>
<h5 dir="auto">Summary:</h5>
<p dir="auto">With certain keys, <code class="notranslate">group_by</code> dumps all hosts into the same group, regardless of expected differences in the keys.  I can reproduce the below problem using the <code class="notranslate">ansible_container</code> variable but not the <code class="notranslate">ansible_virtualization_type</code> variable; it's not clear to me how these variables differ in nature.</p>
<h5 dir="auto">Steps To Reproduce:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- hosts: host-g:host-z
  tasks:
    - group_by: key=container_{{ ansible_container | default('global') }}
    - debug: var=groups"><pre class="notranslate"><code class="notranslate">- hosts: host-g:host-z
  tasks:
    - group_by: key=container_{{ ansible_container | default('global') }}
    - debug: var=groups
</code></pre></div>
<p dir="auto">In this example, <code class="notranslate">host-z</code> is a container, and <code class="notranslate">host-g</code> is not a container.</p>
<h5 dir="auto">Expected Results:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ok: [host-g] =&gt; {
    &quot;var&quot;: {
        &quot;groups&quot;: {
            &quot;container_global&quot;: [
                &quot;host-g&quot;
             ],
             &quot;container_zone&quot;: [
                &quot;host-z&quot;
            ]
        }
    }
}"><pre class="notranslate"><code class="notranslate">ok: [host-g] =&gt; {
    "var": {
        "groups": {
            "container_global": [
                "host-g"
             ],
             "container_zone": [
                "host-z"
            ]
        }
    }
}
</code></pre></div>
<h5 dir="auto">Actual Results:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ok: [host-g] =&gt; {
    &quot;var&quot;: {
        &quot;groups&quot;: {
            &quot;container_global&quot;: [
                &quot;host-g&quot;,
                &quot;host-z&quot;
            ]
        }
    }
}"><pre class="notranslate"><code class="notranslate">ok: [host-g] =&gt; {
    "var": {
        "groups": {
            "container_global": [
                "host-g",
                "host-z"
            ]
        }
    }
}
</code></pre></div>