Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible Variable precedence - what are 'role params' & 'include params'

Tags:

ansible

The Ansible documentation has a list of Variable precedence

Some of them are clear to me but I wonder whether anybody coulde kinkdy shed some light on those 2.

20. role (and include_role) params
21. include params

usage, location, syntax.

I am trying to get Variable declaration a little further to the surface inside a little more complex playbook utilizing 2 roles I am currently working on.

concrete ... some values inside task files of a role should rather be declared as variables in a single location.

like image 317
vrms Avatar asked Oct 16 '25 02:10

vrms


1 Answers

In a nutshell examples:

role params

(full playbook)

- name: I'm a dummy play
  hosts: localhost

  roles:
    - role: somerole
      vars:
        param1: "I'm a role param"

Include role params

(task only)

- name: Including role somerole
  ansible.builtin.include_role:
    name: somerole
  vars:
    param1: "I'm an include role param"

Include params

(task only)

- name: Including a task file
  ansible.builtin.include_tasks: sometasks.yaml
  vars:
    param1: "I'm an include param"

As a fictive and (most probably bad practice) example: if you include a role passing a parameter and later include a task file in that role passing that same parameter with a different value, the include param will take precedence over the role param.

like image 152
Zeitounator Avatar answered Oct 18 '25 01:10

Zeitounator