5

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

Adam Michalik
  • 9,448
  • 11
  • 55
  • 89
guettli
  • 26,461
  • 53
  • 224
  • 476

1 Answers1

3

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
sel-fish
  • 3,946
  • 2
  • 16
  • 35