# Role defaults available in another role's scope

**URL:** <https://forum.ansible.com/t/role-defaults-available-in-another-roles-scope/3975>\
**Category:** Get Help\
**Tags:** variables, role, playbook\
**Created:** [February 21, 2024, 1:54pm UTC](https://forum.ansible.com/t/role-defaults-available-in-another-roles-scope/3975 "2024-02-21T13:54:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluppfisk](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bluppfisk/32/2371_2.png) [@bluppfisk](https://forum.ansible.com/u/bluppfisk)\
**Post date:** [February 21, 2024, 1:54pm UTC](https://forum.ansible.com/t/role-defaults-available-in-another-roles-scope/3975/1 "2024-02-21T13:54:36Z")

</div>

I noticed that defaults specified in one role’s scope become available in a second role’s scope if both roles are part of the same playbook. I wonder why these defaults do not stick to their role scope?

Sample:

**role\_1/defaults/main.yml** :  
`snaap: true`

**role\_2/tasks/main.yml** :

```auto
- name: debug
  ansible.builtin.debug: "{{ snaap }}"

```

**playbook.yml** :

```auto
- role: role_1
  tags: [role1]
- role: role_2
  tags: [role2]

```

I run:  
`ansible-playbook playbook.yml --tags role2`

**result** :

```auto
TASK [role_2 : debug] ***
ok:
msg: true # even though not defined in role_2

```

A nice extra is that if you define in **role\_2/defaults/main.yml** :  
`snaap: "{{ snaap }}"`

you get a nice infinite recursion error 🙂

---

<div class="post-metadata">

**Author:** ![akira6592](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akira6592/32/222_2.png) [@akira6592](https://forum.ansible.com/u/akira6592)\
**Post date:** [February 21, 2024, 2:10pm UTC](https://forum.ansible.com/t/role-defaults-available-in-another-roles-scope/3975/2 "2024-02-21T14:10:02Z")

</div>

@bluppfisk Hello.

You can change `DEFAULT_PRIVATE_ROLE_VARS` to `True` to be private.

[https://docs.ansible.com/ansible/latest/reference\_appendices/config.html#default-private-role-vars](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-private-role-vars)

If change by Environment Variable

```auto
% ANSIBLE_PRIVATE_ROLE_VARS=True ansible-playbook -i localhost, playbook.yml --tags role2

PLAY [test] ****************************************************************************************************************************************************************************************

TASK [role_2 : debug] ***************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'snaap' is undefined.(omit)

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 22, 2024, 2:10pm UTC](https://forum.ansible.com/t/role-defaults-available-in-another-roles-scope/3975/3 "2024-03-22T14:10:54Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
