Puppet - 如何在来宾计算机上使用与主机用户相同的用户名配置用户

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

我如何配置 Puppet 来为来宾计算机上的用户提供与提供来宾的主机上的用户名相匹配的用户名? (使用puppet apply,不是server/master)

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

这是我目前尝试过的:

按照此 SO answer 中的说明,我尝试使用 vagrant/puppet 在来宾计算机上创建一个与主机上的用户帐户相匹配的用户帐户。但最终,它没有奏效。没有获取主机用户名,而是创建了一个“$user_name”用户。

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'
}

实际输出

##############
# 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

预期输出

##############
# 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

Puppet 在简单引用时不会解析变量名。要确保 interpolation 的变量名已完成,您必须使用 ";所以你的人偶文件中的以下内容将起作用

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