Puppet - how to provision a user on the guest machine with the same username as the user on the host machine

空扰寡人 提交于 2019-12-11 12:36:58

问题


How can I configure puppet to provision a user on the guest machine with a username that matches the username of the user on the host machine who provisions the guest? (Using puppet apply, not server/master)

$ puppet --version
3.4.3
$ vagrant --version
Vagrant 1.7.2

This is what I've tried so far:

Following the instructions in this SO answer I tried to use vagrant/puppet to create a user account on the guest machine which matches the user account on the host machine. But ultimately, it didn't work. Instead of getting the host username, a "$user_name" user was created.

Vagrantfile:

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "npalm/mint17-amd64-cinnamon"
  config.vm.hostname = "dev-env"
  config.vm.provider "virtualbox" do |vb|
    vb.name = "dev-env"
    vb.customize ["modifyvm", :id, "--cpus", "2"]
    vb.customize ["modifyvm", :id, "--memory", "1280"]
  end
  config.vm.provision "shell", path: "provisioning/shell/apt-update.sh"
  config.vm.provision "shell", path: "provisioning/shell/puppet.sh"
  config.vm.provision "puppet" do |puppet|
    puppet.facter = {
      "user_name" => ENV['USER']
    }
    puppet.options = "--verbose --debug"
    puppet.manifests_path = "provisioning/puppet/manifests"
    puppet.module_path = "provisioning/puppet/modules"
  end
end

provisioning/shell/apt-update.sh:

#!/usr/bin/env bash
sudo apt-get -y update
sudo apt-get install -y aptitude

provisioning/shell/puppet.sh:

#!/usr/bin/env bash
sudo aptitude install -y puppet

provisioning/puppet/manifests/default.pp:

user { '$user_name':
  ensure  => 'present',
  comment => 'developer account',
  home    => '/home/$user_name',
  shell   => '/bin/bash',
  uid     => 643
}
->
file{'$user_name home':
  ensure => 'directory',
  path => '/home/$user_name',
  owner => '$user_name',
  group => '$user_name'
}

Actual Output

##############
# Host Machine
##############
$ whoami
axiopisty

###############
# Guest Machine
###############
$ ls -la /home/
total 16
drwxr-xr-x  4 root       root       4096 Dec 17 23:41 .
drwxr-xr-x 24 root       root       4096 Sep 28 20:41 ..
drwxr-xr-x  2 $user_name $user_name 4096 Dec 17 23:41 $user_name
drwxr-xr-x 13 vagrant    vagrant    4096 Sep 28 20:42 vagrant

Expected Output

##############
# Host Machine
##############
$ whoami
axiopisty

###############
# Guest Machine
###############
$ ls -la /home/
total 16
drwxr-xr-x  4 root       root       4096 Dec 17 23:41 .
drwxr-xr-x 24 root       root       4096 Sep 28 20:41 ..
drwxr-xr-x  2 axiopisty  axiopisty  4096 Dec 17 23:41 axiopisty
drwxr-xr-x 13 vagrant    vagrant    4096 Sep 28 20:42 vagrant

回答1:


Puppet will not resolve variable names when its simple quoted. To make sure interpolation of your variable names is done, you must use "; so the following in your puppet file will work

user { $user_name:
  ensure  => 'present',
  comment => 'developer account',
  home    => "/home/$user_name",
  shell   => '/bin/bash',
  uid     => 643
}
->
file{"$user_name home":
  ensure => 'directory',
  path  => "/home/$user_name",
  owner => "$user_name",
  group => "$user_name"
}


来源:https://stackoverflow.com/questions/34345939/puppet-how-to-provision-a-user-on-the-guest-machine-with-the-same-username-as

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!