Module_defaults from role

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 :

   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 !

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.

1 Like

OK. Thanks for your response Brian !

BTW, I noticed something in my favor :

Like I said, my playbooks’ ‘module_defaults’ uses variables… Something like :

   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’

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.

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).