4

I have a playbook similar to the one below which uses the base_acl variable. Let's pretend base_acl: "default_acl.j2. I have a host that I want to use a different base_acl value for. I've looked over the ansible precedence docs and don't see a way to do this other than using --extra-vars on the command line which I don't want to do.

I've tried defining base_acl in a host_vars file (should have higher precedence according to link above?) for the host in question, however the play still prints "playbook" for the variable value when run.

Is there a way to tell ansible to give host_vars precedence over playbook variables?

---
- hosts: all
  gather_facts: no
  vars:
    base_acl   : "playbook"
    
  tasks:
  - debug: msg="variable is {{ base_acl }}"
0

1 Answer 1

5

I've tried defining base_acl in a host_vars file (should have higher precedence according to link above?) for the host in question, however the play still prints "playbook" for the variable value when run.

From the link you have referenced in your question:

...
8  - inventory file or script host vars
9  - inventory host_vars/*
10 - playbook host_vars/*
11 - host facts / cached set_facts
12 - play vars
...
22 - extra vars (for example, -e "user=my_user")(always win precedence)

So, since the highest number wins and that all host related entries (8 through 11) have a lower number than 12, your statement is actually wrong. The only correct assesment is that an extra var would always override everything.

There are actually many ways to achieve what you are looking for. But in your situation this is what I would do.

  1. remove the play var entry in your playbook
  2. put the default value in group_vars/all.yml (either at inventory or playbook level):
    base_acl: "playbook"
    
  3. override the default value for every relevant host in host_vars/<your_host>.yml (either at inventory or playbook level):
    base_acl: "override value"
    

Note that the above will work as well to override the default value for a specific group in group_vars/<your_group>.yml (either at inventory or playbook level).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .