ANXS / postgresql

Fairly full featured Ansible role for Postgresql.
http://anxs.io/
MIT License
849 stars 573 forks source link

User defined variable values got overriden by role `vars/*.yml` default one #464

Closed lrk closed 2 years ago

lrk commented 4 years ago

Hi,

it seem the role force default vars in main.yml:

# file: postgresql/tasks/main.yml

- include_vars: "{{ item }}"
  with_first_found:
    - "../vars/{{ ansible_os_family }}.yml"
    - "../vars/empty.yml"
  tags: [always]

#
# Override defaults/main.yml with PostgreSQL version specific values
#
- include_vars: "{{ item }}"
  with_first_found:
    - "../vars/postgresql_{{ postgresql_version }}.yml"
    - "../vars/empty.yml"
  tags: [always]
...

Which cause user defined values to be overriden. (for example postgresql_cluster_name, postgresql_unix_socket_directories)

Example:

#file modified roles/anxs.postgresql/tasks/main.yml

- include_vars: "{{ item }}"
  with_first_found:
    - "../vars/{{ ansible_os_family }}.yml"
    - "../vars/empty.yml"
  tags: [always]

#
# Override defaults/main.yml with PostgreSQL version specific values
#
- include_vars: "{{ item }}"
  with_first_found:
    - "../vars/postgresql_{{ postgresql_version }}.yml"
    - "../vars/empty.yml"
  tags: [always]

- debug:
    msg: "role debug: {{ postgresql_cluster_name }} {{ postgresql_unix_socket_directories }}"
# file group_vars/postgresql.yml
postgresql_cluster_name: "my_cluster"
postgresql_pid_directory: "/var/run/postgresql/{{ postgresql_cluster_name }}/{{ postgresql_version }}"
postgresql_unix_socket_directories:
  - "{{ postgresql_pid_directory }}"
# file my_playbook.yml
- name: "Ensure databases are configured"
  hosts: databases
  pre_tasks:
    - debug:
        msg: "pre tasks playbook debug: {{ postgresql_cluster_name }} {{ postgresql_unix_socket_directories }}"
  roles:
    - anxs.postgresql
  post_tasks:
    - debug:
        msg: "post tasks playbook debug: {{ postgresql_cluster_name }} {{ postgresql_unix_socket_directories }}"

output:

...
TASK [debug] *********************************************************************************************************************************************************************************************************************************

ok: [db] => {
    "msg": "pre task playbook debug: my_cluster [u'/var/run/postgresql/my_cluster/10']"
}

...

TASK [anxs.postgresql : debug] ***************************************************************************************************************************************************************************************************************

ok: [db] => {
    "msg": "role debug: data [u'/var/run/postgresql/data/10', u'/tmp']"
}

...

TASK [debug] *********************************************************************************************************************************************************************************************************************************

ok: [db] => {
    "msg": "post task playbook debug: data [u'/var/run/postgresql/data/10', u'/tmp']"
} 
... 

I may be wrong, but i think it's not intended to force user defined values to default one ;)

Is there a way i did not see to disable that or do we have to submit a fix ?

For my roles, I tend to add "__" prefix to role's vars to avoid name collision and if i need to set variables to default values if they are not set by the user i use that:

- name: "Some task description"
  set_fact:
    some_role_variable: "{{ __some_role_variable_default_value }}"
  when: some_role_variable is not defined

Ansible version used: ansible 2.7.0

Best regards.

lrk commented 4 years ago

I forgot to mention the target os family is Redhat

thbar commented 4 years ago

I believe I have met the same issue while trying to override a postgis version.