Springe zum Hauptinhalt

Get current locale with Ansible

I just had the pro­blem to get the current­ly set lo­ca­te using an­si­ble. This is non­-­tri­vi­a­l, sin­ce one has to con­si­der four en­vi­ron­ment va­ri­a­bles: LC_ALL, LC_­MES­SA­GES (or an­o­ther one), and LANG. For de­tails see the Lo­ca­le Ca­te­go­ries do­cu­men­ta­ti­on.

Now here is the so­lu­ti­o­n, in­clu­ded in­to an an­si­ble play­boo­k:

# -*- 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:
  +49 871 6606-318
  +49 175 29 78 072
  h.goebel@goebel-consult.de