ansibleansible-facts

How to get an arbitrary remote user's home directory in Ansible?


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

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

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

}

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?


Solution

  • 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 }}"
    

    OUTPUT:

    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
    

    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
    

    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.