# Module\_defaults from role

**URL:** <https://forum.ansible.com/t/module-defaults-from-role/42482>\
**Category:** Get Help\
**Created:** [May 6, 2025, 5:31pm UTC](https://forum.ansible.com/t/module-defaults-from-role/42482 "2025-05-06T17:31:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PieterVoet](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pietervoet/32/8365_2.png) [@PieterVoet](https://forum.ansible.com/u/PieterVoet)\
**Post date:** [May 6, 2025, 5:31pm UTC](https://forum.ansible.com/t/module-defaults-from-role/42482/1 "2025-05-06T17:31:29Z")

</div>

Hi all,

I created a role that eventually assembles some variables. Now I want set ‘module\_defaults’ using variables set from that role.

The role is called in the playbook as :

```auto
   roles:
     - { role: config-sync, tags: ["always"] }

  tasks:
    - name: Task that use a module that is in module_defaults
       ....

```

I can set ‘module\_defaults’ from one of the role taskfiles, but then a task executed from the playbook does not get the module\_defaults set.

Does somebody know if this is possible ? Or is ‘module\_defaults’ scoped to the role this way ?  
Thanks for any help !

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [May 6, 2025, 7:06pm UTC](https://forum.ansible.com/t/module-defaults-from-role/42482/2 "2025-05-06T19:06:01Z")

</div>

all keywords are in the scope they are declared so play → role → task, but from role it will not ‘backtrack’ to play and other tasks roles.

---

<div class="post-metadata">

**Author:** ![PieterVoet](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pietervoet/32/8365_2.png) [@PieterVoet](https://forum.ansible.com/u/PieterVoet)\
**Post date:** [May 7, 2025, 7:24am UTC](https://forum.ansible.com/t/module-defaults-from-role/42482/3 "2025-05-07T07:24:11Z")

</div>

OK. Thanks for your response Brian !

---

<div class="post-metadata">

**Author:** ![PieterVoet](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pietervoet/32/8365_2.png) [@PieterVoet](https://forum.ansible.com/u/PieterVoet)\
**Post date:** [May 8, 2025, 2:42pm UTC](https://forum.ansible.com/t/module-defaults-from-role/42482/4 "2025-05-08T14:42:30Z")

</div>

BTW, I noticed something in my favor :

Like I said, my playbooks’ ‘module\_defaults’ uses variables… Something like :

```auto
   module_defaults: 
     group/k8s:
       host: "https://{{ k8s.host }}:{{ k8s.port }}"

```

And if those variables are not set, the playbook complains and exits.

Now I discovered that setting this in ‘playbooks/group\_vars/all’

```auto
k8s:
  host:
  port:

```

The playbook starts, and I can include a role that eventually sets real values to ‘k8s.host’ and ‘k8s.port’… Then once that’s done, all ‘k8s’ modules succesfully have their defaults set !  
Cool. So I apparently resolving the variables used in ‘module\_defaults’ is done in a later stage.  
For me , this resolves my problem. I’m just curious if this is supposed to work this way, and will  
sustain later versions.

Thanks.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [May 8, 2025, 2:48pm UTC](https://forum.ansible.com/t/module-defaults-from-role/42482/5 "2025-05-08T14:48:56Z")

</div>

All variables are ‘lazy evaluation’, but we need to evaluate the `module_defaults` before we know if we have to use the particular one, so the variables do need to be defined, even if not specifically used, but when actually used it should have gotten reevaluated to the ‘current context’, which hopefully matches what you need to do (we might have fixed this in 2.19, but I need to check).
