我正在寻找一个幂等的 ansible 角色/任务来确保将某个语言环境(如 en_US.UTF-8)设置为默认值。它应该生成语言环境(仅在必要时)并将其设置为默认值(也仅在必要时)。
对于第一部分,我将注册输出locale -a | grep "{{ locale_name }}"
并在需要时生成。
对于第二部分,我想知道每次运行 update-locale 是否足够好,因为该命令本身是幂等的。
答案1
localectl
即使新值等于当前值,@mniess 答案中的命令也将始终报告为“已更改”。以下是我个人最终设置和LANG
解决LANGUAGE
该问题的方法:
- 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'
答案2
以下是我最终得到的结果:
- 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
答案3
虽然 rfgamaral 的答案很棒,但它只适用于 Debian,因为社区locale_gen
模块不支持 RHEL. 支持 RHEL/CentOS和Debian,请尝试以下操作:
- name: check if locale exists
shell: "locale -a | grep -i {{ config_system_locale | regex_replace('-', '') | quote }}"
register: found_locale
changed_when: no
failed_when: no
- name: create locale
command: "localedef -i {{ config_system_locale | regex_replace('(.*)\\..*', '\\1') | quote }} -f {{ config_system_locale | regex_replace('.*\\.(.*)', '\\1') | quote }} {{ config_system_locale | quote }}"
when: not ansible_check_mode and found_locale.rc != 0
- name: check if language exists
shell: "locale -a | grep -i {{ config_system_language | regex_replace('-', '') | quote }}"
register: found_language
changed_when: no
failed_when: no
- name: create language
command: "localedef -i {{ config_system_language | regex_replace('(.*)\\..*', '\\1') | quote }} -f {{ config_system_language | regex_replace('.*\\.(.*)', '\\1') | quote }} {{ config_system_language | quote }}"
when: not ansible_check_mode and found_language.rc != 0
- 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
答案4
我在 Fedora 系统上遇到了同样的问题,最终找到了一个专门适用于以下系统的解决方案localectl
:
剧本:
- name: Apply localectl settings
tags:
- system
- localectl
block:
- name: Import localectl vars
ansible.builtin.include_vars: localectl/vars.yml
- name: Set localectl setting
ansible.builtin.include_tasks: localectl/_tasks.yml
loop: "{{ localectl_settings }}"
loop_control:
label: "{{ item.name }}"
localectl/vars.yml:
localectl_settings:
- name: language
value: LANG=de_DE.UTF-8
regex: "System Locale: "
command: set-locale
- name: keymap
value: euro
regex: "VC Keymap: "
command: set-keymap
- name: x11 layout
value: eu
regex: "X11 Layout: "
command: set-x11-keymap
localectl/_tasks.yml:
---
- name: Set localectl settings
become: true
tags:
- system
- localectl
block:
- name: Get status of localectl {{ item.name }}
ansible.builtin.shell:
cmd: localectl status | grep -E "{{ item.regex }}{{ item.value }}"
register: result_locale_status
failed_when: false
changed_when: false
- name: Set localectl setting
ansible.builtin.command:
cmd: "{{ localectl_command }}"
when: result_locale_status.rc == 1
loop_control:
loop_var: localectl_command
loop:
- "localectl {{ item.command }} {{ item.value }}"