Impossible version of playbook group_var being overridden by role var

For an existing project, I first replace the bash provisioning script with an inconspicuous end-to-end Vagrant, and then roll it out to the / prod stage servers after the kinks have been handled.

Problem...

According to the ansible docs, when the priority is variable, the group_vars should override the role-playing vars, but I can see the opposite is happening.

Relevant files ...

Below is a snippet of mine Vagrantfile

(at the root of the project):

config.vm.provision "ansible" do |ansible|
  ansible.playbook = "app/config/provision/provision.yml"
end

      

I point out that in the playbook, several subdirectories are omitted as I am working in an existing codebase with its own practices and cannot leave any invalid material cluttering the root. In the play in question:

# app/config/provision/provision.yml
---
- hosts: all
  gather_facts: yes
  sudo: true

  roles:
    - apache
    - php

  post_tasks:
    - debug: var=vagrant_ansible_test_loading_vars
    - debug: var=apache_listen_ports

      

Notice the debug statements for the two vars, both of which are defined in the group_vars file along with the playbook:

# app/config/provision/group_vars/all
---
vagrant_ansible_test_loading_vars: "lorem ipsum"

apache_listen_ports:
  - 80
  - 8080

      

The apache role being used defines the defaults (which should have LOWEST priority):

# app/config/provision/roles/apache/defaults/main.yml
---
apache_listen_ports: [ 80, 8080 ]

      

The same role also defines vars (which should be SECOND low priority):

# app/config/provision/roles/apache/vars/main.yml
---
apache_listen_ports: [ 80 ]

      

Result (unexpected)

And yet, after vagrant up

, I get the following:

TASK: [debug var=vagrant_ansible_test_loading_vars] *************************** 
ok: [default] => {
    "vagrant_ansible_test_loading_vars": "lorem ipsum"
}

TASK: [debug var=apache_listen_ports] ***************************************** 
ok: [default] => {
    "apache_listen_ports": [
        80
    ]
}

      

The first variable, defined and initialized, tells me that my group_vars file is loading. The second variable was overridden from the value of group_vars, with (presumably) the value from the role shafts.

+3


source to share


2 answers


Variables in roles /rolename/vars/main.yml - VERY high priority. They are considered constants and cannot be overridden without changing the role. role / rolename / defaults / main.yml are low priority and are just that - defaults that can be easily overridden.



+5


source


I know this is an old question, but there is this interesting post on gist.github.com: https://gist.github.com/ekreutz/301c3d38a50abbaad38e638d8361a89e It lists the variables from least to most important.



0


source







All Articles