Search code examples
salt-projectuser-managementhome-directory

SaltStack: $HOME of users which does not exist yet


I create a user foo on a minion. The minion evalutes /etc/default/useradd. This means the salt master does not know whether the new $HOME will be /home/foo or in our case /localhome/foo.

How can I get the $HOME of user foo as jinia variable?

I need it in a systemd service file.

I would like to avoid custom pillar data, since this is redundant. Is there a way to get it via grains?

Does it work during boostrapping? First the user foo needs to be created, then the systemd file can be created by looking up the $HOME of foo...

This would work if the user does already exist:

{{ salt['user.info'](user).get('home') }}/foo:
  file.recurse:
    - source:    salt://conf/common/foo

Related issue: https://github.com/saltstack/salt/issues/7883


Solution

  • Answer this question:

    Is there a way to get it via grains?

    1) add file '_grains/homeprefix.py' under the file_roots specified by the master config file, the content of which is :

    #!/usr/bin/env python
    from os.path import dirname, expanduser
    
    def gethomeprefix():
      # initialize a grains dictionary
      grains = {}
      # Some code for logic that sets grains like
      grains['homeprefix'] = dirname(expanduser("~"))
      return grains
    

    2) run sync cmd on master to sync grains info to minion :

    salt '*' saltutil.sync_grains
    

    3) run grains.get on master to test :

    salt '*' grains.get homeprefix