set_fact/include/role vars precedence

45 views
Skip to first unread message

Takeshi Yaegashi

unread,
Mar 7, 2016, 4:01:24 AM3/7/16
to Ansible Project
Hi,

I'm confused with variable precedence of Ansible 2.0 documented in http://docs.ansible.com/ansible/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable .  Suppose I have playbooks and roles as follows:

hosts (inventory):
[default]
localhost x=1

roles/a/tasks/main.yml (task in role a):
---
- set_fact:
    x: '{{ x + 1 }}'

roles/b/tasks/main.yml (task in role b):
---
- debug:
    var: x

tasks.yml (playbook):
---
- hosts: all
  gather_facts: no
  tasks:
    - include: roles/a/tasks/main.yml
      vars:
        x: 10
    - include: roles/b/tasks/main.yml
    - include: roles/b/tasks/main.yml
      vars:
        x: 20

roles.yml (playbook):
---
- hosts: all
  gather_facts: no
  roles:
    - role: a
      x: 10
    - role: b
    - role: b
      x: 20

With Ansible 2.0.1.0 those two playbooks show diffrent results in the last debug task:
$ ansible-playbook -i hosts tasks.yml 

PLAY ***************************************************************************

TASK [include] *****************************************************************
included: /home/yaegashi/tmp/test/roles/a/tasks/main.yml for localhost

TASK [set_fact] ****************************************************************
ok: [localhost]

TASK [include] *****************************************************************
included: /home/yaegashi/tmp/test/roles/b/tasks/main.yml for localhost

TASK [debug] *******************************************************************
ok: [localhost] => {
    "x": "11"
}

TASK [include] *****************************************************************
included: /home/yaegashi/tmp/test/roles/b/tasks/main.yml for localhost

TASK [debug] *******************************************************************
ok: [localhost] => {
    "x": 20
}

PLAY RECAP *********************************************************************
localhost                  : ok=6    changed=0    unreachable=0    failed=0   

$ ansible-playbook -i hosts roles.yml 

PLAY ***************************************************************************

TASK [a : set_fact] ************************************************************
ok: [localhost]

TASK [b : debug] ***************************************************************
ok: [localhost] => {
    "x": "11"
}

TASK [b : debug] ***************************************************************
ok: [localhost] => {
    "x": "11"
}

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

While include vars can override vars by set_fact, role variable can't.  Is that an expected behavior?
What prcedences are actually given to vars passed by role/include calls respectively?
My first thought was both of them are "role and include vars" in the precedence list in the document.

Regards,
-- 
YAEGASHI Takeshi <yaeg...@debian.org>
Reply all
Reply to author
Forward
0 new messages