1

Hello Iam new to puppet and now trying to explore with puppet manifests. Actually Iam creating a manifest for mongodb and I want to use a variable inside the exec. But each time it is giving me error. My mongodb.pp is as below

    class db::mongodb ($interface) {
    $ip = inline_template("<%= scope.lookupvar('::ipaddress_${interface}') -%>") 
exec {'/bin/bash -c "mongo --host $ip --eval \'  db = db.getSiblingDB("test"); db.addUser({user: "test", pwd: "test", roles: [ "readWrite", "dbAdmin" ]})\'"' :
}
}

But while running this Iam getting error like below

could not connect to the server --eval:27017 at src/mongo/shell/mongo.js:147

IP part is missing in the error because $ip is not substituting IP in the mongo command.. I tried different ways but all gives the same error. Actually I want to know how to use variable in the exec command as above in the puppet?? Any help is much appreciated.

1 Answer 1

1

Variables are only interpolated in double-quoted strings (""), not single-quoted strings ('') and should be in the form ${ip} when inside a string (documentation, via visual index).

You also have quite a few quotes there, so removing the bash -c wrapper would simplify it by removing one set:

exec { "mongo --host ${ip} --eval '  db = db.getSiblingDB(\"test\"); db.addUser({user: \"test\", pwd: \"test\", roles: [ \"readWrite\", \"dbAdmin\" ]})'" :
}

If the bash shell is necessary, use provider => shell instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.. giving 'provider => shell' solves the issue .. But why bin/bash with same command as above is not working??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.