<h2 dir="auto">Summary</h2>
<p dir="auto">We would like to have a paramaterized role to configure a service and have a handler that restarts the service using parameters that are passed into the role.</p>
<p dir="auto">A real world example is a role that installs supervisor that takes a path to a virtual environment and config parameters as parameters.  In that case we want to notify a restart on the service using the parameters for each time it is called.  This seems to work ok in 1.3, it breaks in 1.4.</p>
<h2 dir="auto">Simple example to illustrate the issue:</h2>
<p dir="auto">test/tasks/main.yml:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- shell: echo &quot;test - {{ test_param }}&quot;
  notify: test handler"><pre class="notranslate"><code class="notranslate">- shell: echo "test - {{ test_param }}"
  notify: test handler
</code></pre></div>
<p dir="auto">test/handlers/main.yml:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- name: test handler
  debug: msg=&quot;handler {{ test_param }}&quot;"><pre class="notranslate"><code class="notranslate">- name: test handler
  debug: msg="handler {{ test_param }}"
</code></pre></div>
<p dir="auto">play:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- hosts: all
  roles:
    - { role: test, test_param: &quot;first call to test&quot; }
    - { role: test, test_param: &quot;second call to test&quot; }"><pre class="notranslate"><code class="notranslate">- hosts: all
  roles:
    - { role: test, test_param: "first call to test" }
    - { role: test, test_param: "second call to test" }
</code></pre></div>
<h3 dir="auto">ansible 1.3 output:</h3>
<p dir="auto"><em>This seems to work as expected</em></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK: [shell echo &quot;test - first call to test&quot;] ******************************** 
changed: [localhost]

TASK: [shell echo &quot;test - second call to test&quot;] ******************************* 
changed: [localhost]

NOTIFIED: [test handler] ****************************************************** 
ok: [localhost] =&gt; {&quot;item&quot;: &quot;&quot;, &quot;msg&quot;: &quot;handler first call to test&quot;}

NOTIFIED: [test handler] ****************************************************** 
ok: [localhost] =&gt; {&quot;item&quot;: &quot;&quot;, &quot;msg&quot;: &quot;handler second call to test&quot;}"><pre class="notranslate"><code class="notranslate">TASK: [shell echo "test - first call to test"] ******************************** 
changed: [localhost]

TASK: [shell echo "test - second call to test"] ******************************* 
changed: [localhost]

NOTIFIED: [test handler] ****************************************************** 
ok: [localhost] =&gt; {"item": "", "msg": "handler first call to test"}

NOTIFIED: [test handler] ****************************************************** 
ok: [localhost] =&gt; {"item": "", "msg": "handler second call to test"}
</code></pre></div>
<h3 dir="auto">ansible-playbook 1.4 (devel <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/ansible/ansible/commit/f347133d8564935867f6771d658f25d38a209f17/hovercard" href="https://github.com/ansible/ansible/commit/f347133d8564935867f6771d658f25d38a209f17"><tt>f347133</tt></a>) output:</h3>
<p dir="auto"><em>Note that only the handler was triggered only once for the first call. This can be an issue when you call a role with parameters where one of the parameters is used in the notifier (such as a service configuration file location, and a restart handler)</em></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK: [test | shell echo &quot;test - first call to test&quot;] ************************* 
changed: [localhost]

TASK: [test | shell echo &quot;test - second call to test&quot;] ************************ 
changed: [localhost]

NOTIFIED: [test | test handler] *********************************************** 
ok: [localhost] =&gt; {
    &quot;item&quot;: &quot;&quot;, 
    &quot;msg&quot;: &quot;handler first call to test&quot;
}
"><pre class="notranslate"><code class="notranslate">TASK: [test | shell echo "test - first call to test"] ************************* 
changed: [localhost]

TASK: [test | shell echo "test - second call to test"] ************************ 
changed: [localhost]

NOTIFIED: [test | test handler] *********************************************** 
ok: [localhost] =&gt; {
    "item": "", 
    "msg": "handler first call to test"
}

</code></pre></div>
<p dir="auto">Changing test/tasks/main.yml to flush its handlers seems to have issues in both versions:</p>
<p dir="auto">test/tasks/main.yml:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- shell: echo &quot;test - {{ test_param }}&quot;
  notify: test handler

- meta: flush_handlers"><pre class="notranslate"><code class="notranslate">- shell: echo "test - {{ test_param }}"
  notify: test handler

- meta: flush_handlers
</code></pre></div>
<h3 dir="auto">ansible 1.3:</h3>
<p dir="auto"><em>Note that both handlers for both parameters are flushing twice.</em></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK: [shell echo &quot;test - first call to test&quot;] ******************************** 
changed: [localhost]

NOTIFIED: [test handler] ****************************************************** 
ok: [localhost] =&gt; {&quot;item&quot;: &quot;&quot;, &quot;msg&quot;: &quot;handler first call to test&quot;}

NOTIFIED: [test handler] ****************************************************** 
ok: [localhost] =&gt; {&quot;item&quot;: &quot;&quot;, &quot;msg&quot;: &quot;handler second call to test&quot;}

TASK: [shell echo &quot;test - second call to test&quot;] ******************************* 
changed: [localhost]

NOTIFIED: [test handler] ****************************************************** 
ok: [localhost] =&gt; {&quot;item&quot;: &quot;&quot;, &quot;msg&quot;: &quot;handler first call to test&quot;}

NOTIFIED: [test handler] ****************************************************** 
ok: [localhost] =&gt; {&quot;item&quot;: &quot;&quot;, &quot;msg&quot;: &quot;handler second call to test&quot;}"><pre class="notranslate"><code class="notranslate">TASK: [shell echo "test - first call to test"] ******************************** 
changed: [localhost]

NOTIFIED: [test handler] ****************************************************** 
ok: [localhost] =&gt; {"item": "", "msg": "handler first call to test"}

NOTIFIED: [test handler] ****************************************************** 
ok: [localhost] =&gt; {"item": "", "msg": "handler second call to test"}

TASK: [shell echo "test - second call to test"] ******************************* 
changed: [localhost]

NOTIFIED: [test handler] ****************************************************** 
ok: [localhost] =&gt; {"item": "", "msg": "handler first call to test"}

NOTIFIED: [test handler] ****************************************************** 
ok: [localhost] =&gt; {"item": "", "msg": "handler second call to test"}
</code></pre></div>
<h3 dir="auto">ansible 1.4 (devel <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/ansible/ansible/commit/f347133d8564935867f6771d658f25d38a209f17/hovercard" href="https://github.com/ansible/ansible/commit/f347133d8564935867f6771d658f25d38a209f17"><tt>f347133</tt></a>) output:</h3>
<p dir="auto"><em>In this case when the second call to test.yml flushes its handler it uses the first definition of <code class="notranslate">{{ test_param }}</code>, not the second.</em></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK: [test | shell echo &quot;test - first call to test&quot;] ************************* 
changed: [localhost]

NOTIFIED: [test | test handler] *********************************************** 
ok: [localhost] =&gt; {
    &quot;item&quot;: &quot;&quot;, 
    &quot;msg&quot;: &quot;handler first call to test&quot;
}

TASK: [test | shell echo &quot;test - second call to test&quot;] ************************ 
changed: [localhost]

NOTIFIED: [test | test handler] *********************************************** 
ok: [localhost] =&gt; {
    &quot;item&quot;: &quot;&quot;, 
    &quot;msg&quot;: &quot;handler first call to test&quot;
}"><pre class="notranslate"><code class="notranslate">TASK: [test | shell echo "test - first call to test"] ************************* 
changed: [localhost]

NOTIFIED: [test | test handler] *********************************************** 
ok: [localhost] =&gt; {
    "item": "", 
    "msg": "handler first call to test"
}

TASK: [test | shell echo "test - second call to test"] ************************ 
changed: [localhost]

NOTIFIED: [test | test handler] *********************************************** 
ok: [localhost] =&gt; {
    "item": "", 
    "msg": "handler first call to test"
}
</code></pre></div>
<p dir="auto">Since I recently updated my Ansible installation repository, Ansible overrides default variables where it should not. If I call a sub role as dependency of a 'parent' role twice, but the last time with overriding a default variable, the variable was also overridden on the first call. Here is a tiny example setup (I omitted the starting <code class="notranslate">---</code> in every file):</p>
<p dir="auto"><strong>sub_role/defaults/main.yml</strong></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="myvar: &quot;first call&quot;"><pre class="notranslate"><code class="notranslate">myvar: "first call"
</code></pre></div>
<p dir="auto"><strong>sub_role/tasks/main.yml</strong></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- debug: var=myvar"><pre class="notranslate"><code class="notranslate">- debug: var=myvar
</code></pre></div>
<p dir="auto"><strong>parent_role/meta/main.yml</strong></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="dependencies:
  - { role: sub_role }
  - { role: sub_role, myvar: &quot;second call&quot; }"><pre class="notranslate"><code class="notranslate">dependencies:
  - { role: sub_role }
  - { role: sub_role, myvar: "second call" }
</code></pre></div>
<p dir="auto">Now executing the <em>parent_role</em> leads to following wrong result:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK [sub_role : debug] ********************************************************
ok: [localhost] =&gt; {
    &quot;myval&quot;: &quot;second call&quot;
}

TASK [sub_role : debug] ********************************************************
ok: [localhost] =&gt; {
    &quot;myval&quot;: &quot;second call&quot;
}
"><pre class="notranslate"><code class="notranslate">TASK [sub_role : debug] ********************************************************
ok: [localhost] =&gt; {
    "myval": "second call"
}

TASK [sub_role : debug] ********************************************************
ok: [localhost] =&gt; {
    "myval": "second call"
}

</code></pre></div>
<p dir="auto">Can you see the error? On the first call, the output of <code class="notranslate">myval</code> should be <em>first call</em> and not <em>second call</em>. Because of recent changes Ansible seems to interpret all role variables, starting with the first definition and ignoring omitted definitions.</p>