2017-04-26 27 views
0

我打算實現爲每個用戶添加多個ssh密鑰的可能性。 對於單個鍵,我用:在傀儡中的位置迭代陣列

if ($sshkey) { 
    ssh_authorized_key { $resourcename: 
     ensure => 'present', 
     type => 'ssh-rsa', 
     key  => '$sshkey', 
     user => $title, 
     require => User[$title], 
    } 
    } 

對於多個鍵,我認爲這可能工作:

if ($sshkeyarray != []) { 
    $sshkeyarray.each |String $singlesshkey| { 
     ssh_authorized_key { $resourcename: 
     ensure => 'present', 
     type => 'ssh-rsa', 
     key  => '$singlesshkey', 
     user => $title, 
     require => User[$title], 
     } 
    } 
    } 

但資源名稱只能使用一次,所以我想給喜歡的名字第一個ssh密鑰爲「resourcename_1」,第n個密鑰爲「resourcename_n」。

我該怎麼做?我可以從數組中獲得singlesshkey的位置並將其添加到resourdcename中嗎?

回答

1

如文檔here你可以做到這一點說明:

$sshkeyarray.each |$index, String $singlesshkey| { 
    ssh_authorized_key { "${resourcename}_${index}": 
     ensure => 'present', 
     type => 'ssh-rsa', 
     key  => $singlesshkey, 
     user => $title, 
     require => User[$title], 
    } 
    } 

注意到,有沒有必要或者測試一個空數組。循環播放一個空數組不會導致任何事情發生。

+0

完美,謝謝! – Studentus

+1

哇,這是我以前不知道的'each()'函數的細節!我今天學了些新東西。 –