LoginSignup
24
22

More than 5 years have passed since last update.

ansible で sudo 時に実行ユーザの HOME 環境変数を取得する

Last updated at Posted at 2015-06-14

ansible で sudo: yes を利用すると {{ ansible_env.HOME }} が root のものになってしまう。これでは開発環境の構築など実行ユーザのホームディレクトリ配下にファイルを配置したいときなどに困る。

ansible では実行ユーザ名が {{ ansible_ssh_user }} に設定されているためこれを利用して実行ユーザの HOME 環境変数を取得して利用することができる。

pre_tasks:
  - name: Get ansible_user home directory
    shell: 'getent passwd "{{ansible_ssh_user}}" | cut -d: -f6'
    register: ansible_home_result

  - name: Set the fact for the other scripts to use
    set_fact: ansible_home='{{ansible_home_result.stdout}}'

上記のタスクを pre_task で実行することで {{ ansible_home }} に実行ユーザの HOME 環境変数が設定されるのでそれを他のタスクから利用すればよい。

ちなみに {{ ansible_ssh_user }}connection: local 時でも設定されているので問題なく利用できる。

24
22
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
24
22