Skip to content

Instantly share code, notes, and snippets.

@resmo
Last active August 20, 2020 12:43
Show Gist options
  • Save resmo/f8bfd83ba13fce300ca988e39a612ead to your computer and use it in GitHub Desktop.
Save resmo/f8bfd83ba13fce300ca988e39a612ead to your computer and use it in GitHub Desktop.
role vars vs tasks vars

given a role "my-role" roles/my-role/main.yml

- name: run debug
  debug:
    msg: "{{ myvar }}"

playbook 1

- name: My Test Play
  hosts: localhost
  vars:
    myvar: PLAY
  roles:
  - name: My Run A
    role: my-role
  - name: My Run B
    role: my-role
    vars:
      myvar: B

result 1

PLAY [My Test Play] ****************************************************************************************************************

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

TASK [my-role : run debug] *********************************************************************************************************
ok: [localhost] => {
    "msg": "B"
}

TASK [my-role : run debug] *********************************************************************************************************
ok: [localhost] => {
    "msg": "B"
}

PLAY RECAP *************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

playbook 2

- name: My Test Play
  hosts: localhost
  vars:
    myvar: PLAY
  roles:
  - name: My Run A
    role: my-role
  - name: My Run B
    role: my-role
    myvar: B

result 2

PLAY [My Test Play] ****************************************************************************************************************

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

TASK [my-role : run debug] *********************************************************************************************************
ok: [localhost] => {
    "msg": "PLAY"
}

TASK [my-role : run debug] *********************************************************************************************************
ok: [localhost] => {
    "msg": "B"
}

PLAY RECAP *************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment