如何在 Ansible 中获取任意远程用户的主目录? [英] How to get an arbitrary remote user's home directory in Ansible?

查看:36
本文介绍了如何在 Ansible 中获取任意远程用户的主目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 shell 使用 getentawk 的组合来做到这一点:

I can do that with shell using combination of getent and awk like this:

getent passwd $user | awk -F: '{ print $6 }'

作为参考,在 Puppet 中,我可以使用自定义事实,如下所示:

For the reference, in Puppet I can use a custom fact, like this:

require 'etc'

Etc.passwd { |user|

   Facter.add("home_#{user.name}") do
      setcode do
         user.dir
      end
   end

}

这使得用户的主目录可用作 home_ 事实.

which makes the user's home directory available as a home_<user name> fact.

如何获取任意远程用户的主目录?

How do I get the home directory of an arbitrary remote user?

推荐答案

Ansible(从 1.4 开始)已经在 ansible_env 变量.

Ansible (from 1.4 onwards) already reveals environment variables for the user under the ansible_env variable.

- hosts: all
  tasks:
    - name: debug through ansible.env
      debug: var=ansible_env.HOME

不幸的是,您显然只能使用它来获取连接用户的环境变量,如本剧本和输出所示:

Unfortunately you can apparently only use this to get environment variables for the connected user as this playbook and output shows:

- hosts: all
  tasks:
    - name: debug specified user's home dir through ansible.env
      debug: var=ansible_env.HOME
      become: true
      become_user: "{{ user }}"

    - name: debug specified user's home dir through lookup on env
      debug: var=lookup('env','HOME')
      become: true
      become_user: "{{ user }}"

输出:

vagrant@Test-01:~$ ansible-playbook -i "inventory/vagrant" env_vars.yml -e "user=testuser"

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.0.30]

TASK: [debug specified user's home dir through ansible.env] *******************
ok: [192.168.0.30] => {
    "var": {
        "/home/vagrant": "/home/vagrant"
    }
}

TASK: [debug specified user's home dir through lookup on env] *****************
ok: [192.168.0.30] => {
    "var": {
        "/home/vagrant": "/home/vagrant"
    }
}

PLAY RECAP ********************************************************************
192.168.0.30               : ok=3    changed=0    unreachable=0    failed=0

与 Ansible 中的任何内容一样,如果您无法获得一个模块来提供您想要的东西,那么您始终可以自由地shell 使用这样的东西(尽管这应该谨慎使用,因为它可能很脆弱并且描述性较差):

As with anything in Ansible, if you can't get a module to give you what you want then you are always free to shell out (although this should be used sparingly as it may be fragile and will be less descriptive) using something like this:

- hosts: all
  tasks:
    - name: get user home directory
      shell: >
             getent passwd {{ user }}  | awk -F: '{ print $6 }'
      changed_when: false
      register: user_home

    - name: debug output
      debug:
        var: user_home.stdout

很可能有一种更干净的方法来做到这一点,我有点惊讶使用 become_user 切换到指定的用户似乎不会影响 env 查找,但这应该给你你想要的.

There may well be a cleaner way of doing this and I'm a little surprised that using become_user to switch to the user specified doesn't seem to affect the env lookup but this should give you what you want.

这篇关于如何在 Ansible 中获取任意远程用户的主目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆