如何使用 ansible 生成和设置语言环境?

mni*_*ess 11 ansible

我正在寻找一个幂等的 ansible 角色/任务,以确保将某个区域设置(如 en_US.UTF-8)设置为默认值。它应该生成语言环境(仅在必要时)并将其设置为默认值(也仅在必要时)。

对于第一部分locale -a | grep "{{ locale_name }}",如果需要,我会注册输出并生成。

对于第二部分,我想知道每次运行 update-locale 是否足够好,因为该命令本身无论如何都是幂等的。

rfg*_*ral 12

localectl即使新值等于当前值,来自@mniess answer的命令也将始终报告为“已更改”。下面是我个人结束了同时设置LANGLANGUAGE解决这个问题:

- name: Ensure localisation files for '{{ config_system_locale }}' are available
  locale_gen:
    name: "{{ config_system_locale }}"
    state: present

- name: Ensure localisation files for '{{ config_system_language }}' are available
  locale_gen:
    name: "{{ config_system_language }}"
    state: present

- name: Get current locale and language configuration
  command: localectl status
  register: locale_status
  changed_when: false

- name: Parse 'LANG' from current locale and language configuration
  set_fact:
    locale_lang: "{{ locale_status.stdout | regex_search('LANG=([^\n]+)', '\\1') | first }}"

- name: Parse 'LANGUAGE' from current locale and language configuration
  set_fact:
    locale_language: "{{ locale_status.stdout | regex_search('LANGUAGE=([^\n]+)', '\\1') | default([locale_lang], true) | first }}"

- name: Configure locale to '{{ config_system_locale }}' and language to '{{ config_system_language }}'
  command: localectl set-locale LANG={{ config_system_locale }} LANGUAGE={{ config_system_language }}
  changed_when: locale_lang != config_system_locale or locale_language != config_system_language
Run Code Online (Sandbox Code Playgroud)

另外,这就是我所拥有的group_vars/main.yml

config_system_locale: 'pt_PT.UTF-8'
config_system_language: 'en_US.UTF-8'
Run Code Online (Sandbox Code Playgroud)


mni*_*ess 6

这是我最终的结果:

- name: Ensure the locale exists
  locale_gen:
    name: en_US.UTF-8
    state: present
- name: set as default locale
  command: localectl set-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8
Run Code Online (Sandbox Code Playgroud)