user2887201
user2887201

Reputation: 337

Store contents of a file in a variable

I am trying to extract the contents of a file and store it in a variable. My code looks like this.

define ssh_user (
        $name = undef,
        $role = undef,
        $password = undef,
        $usergroup = undef, ) {

                user { $name:
                        home => "/home/${name}/",
                        ensure => present,

                }->

                exec { "generate keys ${name}":
                        user => $name,
                        command => '/bin/echo -e "\n\n\n" | ssh-keygen -t rsa',
                        path => "/usr/bin/",
                }->
               file { "/home/${name}/.ssh/auhorized_keys":
                        ensure => file,
                        mode => 700,
                        source => "/home/${name}/.ssh/id_rsa.pub",
               } 
                @@node_user { $name:
                        require => File["/home/${name}/.ssh/auhorized_keys"],
                        key => file('/home/${name}/.ssh/id_rsa.pub'),    ## line causing error
                        role => "ssh::users::${name}::role",
                }
}

I get the following error,

Error: Could not find any files from /home/${name}/.ssh/id_rsa.pub at /etc/puppet/manifests/sshd_setup.pp:90 on node puppet.colo.seagate.com
Error: Could not find any files from /home/${name}/.ssh/id_rsa.pub at /etc/puppet/manifests/sshd_setup.pp:90 on node puppet.colo.seagate.com

I am creating files for a set users and storing the contents of the file in a varaible. After storing the file contents in a variable I am using it to create an exported resource.

I tried using "require" and "->" for ensuring the ordering between my resources; but it turns out the error may be because of the path to my file (it contains a variable name).

Upvotes: 1

Views: 8860

Answers (1)

xiankai
xiankai

Reputation: 2781

You're using single quotes instead of double quotes for that line, I'm guessing that's why the variable isn't parsed correctly?

ie. it should be

key => file("/home/${name}/.ssh/id_rsa.pub"),    ## line causing error

instead

Upvotes: 6

Related Questions