Ansible

如何使用 ansible 生成和設置語言環境?

  • April 21, 2022

我正在尋找一個冪等的 ansible 角色/任務,以確保將某個語言環境(如 en_US.UTF-8)設置為預設值。它應該生成語言環境(僅在必要時)並將其設置為預設值(也僅在必要時)。

對於第一部分,我將註冊輸出locale -a | grep "{{ locale_name }}"並在需要時生成。

對於第二部分,我想知道每次執行 update-locale 是否足夠好,因為該命令本身無論如何都是冪等的。

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 }}'
 become: yes
 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

另外,這就是我所擁有的group_vars/main.yml

config_system_locale: 'pt_PT.UTF-8'
config_system_language: 'en_US.UTF-8'

引用自:https://serverfault.com/questions/959026