Puppet - 如何使用与主机上的用户相同的用户名在客户机上配置用户

时间:2015-12-17 23:03:44

标签: vagrant puppet provisioning

如何配置puppet以在客户机上为用户配置用户名,该用户名与主机上配置来宾的用户的用户名相匹配? (使用puppet apply,不是服务器/主人)

$ 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

供应/壳/ apt-update.sh:

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

供应/壳/ puppet.sh:

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

供应/木偶/舱单/ 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

1 个答案:

答案 0 :(得分:2)

当简单引用时,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"
}