Springe zum Hauptinhalt

Get current locale with Ansible

I just had the problem to get the currently set locate using ansible. This is non-trivial, since one has to consider four environment variables: LC_ALL, LC_MESSAGES (or another one), and LANG. For details see the Locale Categories documentation.

Now here is the solution, included into an ansible playbook:

# -*- indent-tabs-mode: nil -*-
---

- hosts: all
  become: yes
  gather_facts: false

  vars:

   env1:
     LC_ALL: "de_DE.UTF-8"
     LC_MESSAGES: "de_AT.UTF-8"
     LANG: "de_CH.UTF-8"

   env2:
     LC_MESSAGES: "de_AT.UTF-8"
     LANG: "de_CH.UTF-8"

   env3:
     LANG: "de_CH.UTF-8"

   env4:
     empty: empty


   davical_default_locale: '{{ (ansible_env.LC_ALL|d(ansible_env.LC_MESSAGES)|d(ansible_env.LANG)|d("en")).split(".",1)[0] }}'

  tasks:

  - set_fact:
      ansible_env: '{{ env1 }}'
  - debug: var=davical_default_locale

  - set_fact:
      ansible_env: '{{ env2 }}'
  - debug: var=davical_default_locale

  - set_fact:
      ansible_env: '{{ env3 }}'
  - debug: var=davical_default_locale

  - set_fact:
      ansible_env: '{{ env4 }}'
  - debug: var=davical_default_locale
Portrait von Hartmut Goebel
Hartmut Goebel
Diplom-Informatiker, CISSP, CSSLP, ISO 27001 Lead Implementer

Haben Sie noch Fragen?
Anruf oder Mail genügt:
Telefon:   +49 871 6606-318
Mobil:   +49 175 29 78 072
E-Mail:   h.goebel@goebel-consult.de