Overridden variable leaking out of dependency declared in role meta?

I have 2 roles (r1 and r2), r2 depends on r1 and the dependency overrides a variable (x). I would expect the overridden value to only apply while r1 is run as a dependency. However the overridden value gets applied throughout the entire play.

If r1 is run in it’s only play, without r2, then I get the value from group_vars/ as I expect.

Below is a minimal reproduction. Have I found a bug, or am I misunderstanding how Ansible variables are scoped?

With thanks, Alex

group_vars/g1

x: set by group_vars

roles/r1/tasks/main.yml

  • debug:
    var: x

roles/r2/meta/main.yml

dependencies:

  • role: r1
    vars:
    x: overridden by dependency

roles/r2/tasks/main.yml

  • debug:
    var: x

inventory

g1:
hosts:
localhost:

playbook.yml

  • hosts: g1
    connection: local
    roles:
  • r1
  • r2

ansible.cfg

[defaults]
stdout_callback=yaml

:/tmp/test [v]$ ansible-playbook -i inventory playbook.yml

PLAY [Play that includes dependency] ******************************************************

TASK [Gathering Facts] ********************************************************************
ok: [localhost]

TASK [r1 : debug] *************************************************************************
ok: [localhost] =>
x: overridden by dependency

TASK [r1 : debug] *************************************************************************
ok: [localhost] =>
x: overridden by dependency

TASK [r2 : debug] *************************************************************************
ok: [localhost] =>
x: overridden by dependency

PLAY [Play that excludes dependency] ******************************************************

TASK [Gathering Facts] ********************************************************************
ok: [localhost]

TASK [r1 : debug] *************************************************************************
ok: [localhost] =>
x: set by group_vars

PLAY RECAP ********************************************************************************
localhost : ok=6 changed=0 unreachable=0 failed=0

:/tmp/test [v]$ ansible-playbook --version
ansible-playbook 2.7.6
config file = /tmp/test/ansible.cfg
configured module search path = [u’/home/alex/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /tmp/v/local/lib/python2.7/site-packages/ansible
executable location = /tmp/v/bin/ansible-playbook
python version = 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]