ansible debconf module not working properly to set default locale

Viewed 1270
- name: Generate required locales
  locale_gen: 
name: "{{ item }}"
state: present
  with_items:
  - "fr_FR"
  - "fr_FR.UTF-8"
  - "fr_FR@euro"
  become: yes

- name: rebuild locales database
  command: "{{ item }}"
  with_items:
    - dpkg-reconfigure locales -f noninteractive
    - /usr/sbin/locale-gen
  become: yes

- name: Set defaultt locale to fr_FR.UTF-8
  debconf:
    name: locales
    question: locales/default_environment_locale
    value: fr_FR.UTF-8
    vtype: select
  become: yes

- name: rebuild locales database3
  command: "{{ item }}"
  with_items:
    - dpkg-reconfigure locales -f noninteractive
    - /usr/sbin/locale-gen
  become: yes

2 firsts tasks are working nicely (if I do : sudo dpkg-reconfigure locales, the 3 fr_FR I wanted are selected) but for the second part : ansible say its working (changed) but if I do : cat /etc/default/locale I still have EN as default ? why ?

2 Answers

This specific locales package always re-read its configuration from the /etc/locale.gen and /etc/default/locale files, according to what I can read in /var/lib/dpkg/info/locales.config.

Because of this, in this specific case you can't use the debconf module. Instead, you have to first use the locale_gen module to select your available locales, then the lineinfile module on /etc/default/locale to select your default locale, and finally the command module to launch a dpkg-reconfigure -f noninteractive locales (which will in turn run the locale-gen command) to apply all this to the system.

The debconf configuration will be updated accordingly. It looks like the other way around (files are usually updated from debconf choices), but that's how it's done for now.

Here is a complete solution I actually use myself:

vars:
  locales_generated:
    - en_US.UTF-8
    - fr_CA.UTF-8
    - fr_FR.UTF-8
  locales_default: fr_CA.UTF-8

tasks:

- name: select locales to be generated
  locale_gen:
    name: "{{ item }}"
    state: present
  loop: "{{ locales_generated }}"
  notify: reconfigure locales

- name: set default locale
  lineinfile:
    dest: "/etc/default/locale"
    regexp: "^LANG="
    line: "LANG={{ locales_default }}"
  notify: reconfigure locales

handlers:

- name: reconfigure locales
  command: dpkg-reconfigure -f noninteractive locales

I didn't find the way to make this module working properly so instead I used locale_gen and fileinline to replace the default locale in /etc/default/locale. It's not THE answer i'm looking for but it's a way to make things works if anyone had or will have the same issue.

Related