Nathan
Nathan

Reputation: 7709

Ansible: Make variable in inventory group_vars have higher precedence than in playbook group_vars

I have a ansible playbooks, which is supposed to be run with different inventories.

The playbook group_vars define some variables, which are defaults. For example:

MY_VAR: my_value

Now, I want to be able to override these variables in the inventory group_vars. But becauase inventory group_vars have lower precedence, it does not overwrite it.

I tried to define the variable like this in the playbook group_vars:

MY_VAR: {{ MY_VAR | default('my_value') }}

But that also does not work.

Can I somehow make the inventory group_vars overwrite the playbook group_vars?

Upvotes: 0

Views: 860

Answers (1)

Fredrik Rambris
Fredrik Rambris

Reputation: 356

How about using set_fact in pre_tasks? Playbook vars are evaluated early. set_fact when play is run. I'm thinking something like this:

pre_tasks:
  - name: Override variables
    set_fact:
      MY_VAR: "{{ MY_VAR | default('my_value') }}"

Upvotes: 2

Related Questions