1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ansible で ターゲットノードにおける python の locale としてutf-8を指定する(2025/03更新)

Last updated at Posted at 2022-07-08

レアケースとおもえるが必要になったので備忘

2025/03追記:

ansible-core 2.18.3環境においても、playbookにおいて以下の様にenvironmentを設定することで、接続先のロケールを上書きして接続できることが確認できました。

- hosts: all
  environment:
    LANG: JA_JP.UTF-8
    LC_ALL: JA_JP.UTF-8
  tasks:
...

2024/11追記: Ansible 2.17.1以降ではこの対応方法が使えない可能性

以下の修正の影響
https://github.com/ansible/ansible/issues/83476
GitHubGitHub
ansible_python_interpreter="/usr/bin/env python3" doesn't work anymore · Issue #83476 · ansible/ansible

playbook側で、play や task に environment: { LANG: C } などを付けて、環境変数 LANG や LC_ALL などを設定することで対応できる可能性がある。

Ansible で ターゲットノードにおける python の locale を指定する

ターゲットノードにおける python のロケールは、接続に使用するユーザーのロケールが通常は使用されることになるが、それとは異なったロケールを使用したい場合の指定方法。

ターゲットノードにおける python は、変数では ansible_python_interpreter、ansible.cfg では interpreter_python によって指定する。

つまりこれらで ロケール を設定しつつ pythonを実行できればよい。

Cロケールでpython3を利用する例

ansible-playbookコマンドへの引数による指定例

$ ansible-playbook ...  -e 'ansible_python_interpreter="/usr/bin/env LANG=Ja_JP.UTF-8 LC_ALL=Ja_JP.UTF-8 /usr/bin/python3"' ...

play varsにおける指定例

− hosts: all
  vars:
    ansible_python_interpreter: "/usr/bin/env LANG=Ja_JP.UTF-8 LC_ALL=Ja_JP.UTF-8 /usr/bin/python3"
  tasks:
  ...

inventory varsにおける指定例

inventory.yml

yaml
all:
  vars:
    ansible_python_interpreter: "/usr/bin/env LANG=Ja_JP.UTF-8 LC_ALL=Ja_JP.UTF-8 /usr/bin/python3"
  children:
    ungrouped:
      hosts:
        node1:

ansible.cfgによる指定例 (Ansible 2.17.1以降では使えない可能性)

ansible.cfg
[defaults]
interpreter_python="/usr/bin/env LANG=Ja_JP.UTF-8 LC_ALL=Ja_JP.UTF-8 /usr/bin/python3"
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?