0

Here is the LDAP command that I want to run, which works perfectly:

/opt/openldap/setup —cli —no-prompt —rootUserDN "cn=Directory Manager" — rootUserPasswpord secretpass —hostname localhost —ldapPort 389 —adminConnectorPort 4444 —baseDN "dc=example,dc=com" —addBaseEntry —doNotStart —acceptLicense'

Here is the content of my chef attribute file (server.rb)

default["openldap"]["server"]["ROOTDN"] = "cn=Directory Manager"
default["openldap"]["server"]["ROOTPASS"] = "secret pass"
default["openldap"]["server"]["LDAPPORT"] = "389"
default["openldap"]["server"]["ADMINPORT"] = "4444"
default["openldap"]["server"]["BASEDN"] = "dc=example,dc=com"
default["openldap"]["server"]["DJDEST"] = "/opt/openldap"

Here is the content of my chef recipe file (default.rb)

execute 'Ldap: Installing Openldap Instance' do
  command node["openldap"]["server"]["DJDEST"]'/setup --cli --no-prompt--rootUserDN "["openldap"]["server"]["ROOTDN"] " --rootUserPassword ["openldap"]  ["server"]["ROOTPASS"]--hostname ["openldap"]["server"]["ODJFQDN"] --ldapPort ["openldap"]["server"]["LDAPPORT"] --adminConnectorPort ["openldap"]["server"]["ADMINPORT"] --baseDN "["openldap"]["server"]["BASEDN"]" --addBaseEntry --doNotStart --acceptLicense'
  action :run
end

I want to run the execute resource using variables instead of manually hardcoding the ldap script in the code block. Can anyone please advice me on what i am doing wrong?

StephenKing
  • 36,187
  • 11
  • 83
  • 112
jebjeb
  • 115
  • 1
  • 4
  • 12
  • Hey. Welcome to StackOverflow. I tried a bit to improve the syntax, but you seem to have couple of issues. What quotes are you using? Could you please double-check all the things you pasted and I tried to fix so that we get your real problem? – StephenKing Aug 05 '16 at 19:04
  • Is your problem actually string interpolation in ruby, so that you want to combine an attribute and a string together? like `node[...] + '/setup ..' `? – StephenKing Aug 05 '16 at 19:05

2 Answers2

1

Assuming that I understand your question right, you want to generate a string from few variables and strings. Then your question is similar to this one.

You could either combine the content of a variable a = "foo" and the string "bar" using:

a = "foo"
b = a + "bar"

Or you can use string interpolation (probably more common):

a = "foo"
b = "#{a}bar"

The result in both cases is "foobar".

Translated to your chef context, the resulting execute resource would be:

execute 'Ldap: Installing Openldap Instance' do
  command "#{node['openldap']['server']['DJDEST']}/setup --cli --no-prompt--rootUserDN "#{node['openldap']['server']['ROOTDN']}" --rootUserPassword #{node['openldap']['server']['ROOTPASS']} --hostname #{node['openldap']['server']['ODJFQDN']} --ldapPort #{node['openldap']['server']['LDAPPORT']} --adminConnectorPort #{node['openldap']['server']['ADMINPORT']} --baseDN "#{node['openldap']['server']['BASEDN']}" --addBaseEntry --doNotStart --acceptLicense"
  action :run
end

If it makes it easier for you, you can also extract this into a variable:

installLdapCommand = "#{node['openldap']['server']['DJDEST']}/setup --cli --no-prompt--rootUserDN #{node['openldap']['server']['ROOTDN']} --rootUserPassword #{node['openldap']['server']['ROOTPASS']} --hostname #{node['openldap']['server']['ODJFQDN']} --ldapPort #{node['openldap']['server']['LDAPPORT']} --adminConnectorPort #{node['openldap']['server']['ADMINPORT']} --baseDN "#{node['openldap']['server']['BASEDN']}" --addBaseEntry --doNotStart --acceptLicense"
execute 'Ldap: Installing Openldap Instance' do
  command installLdapCommand
  action :run
end
Community
  • 1
  • 1
StephenKing
  • 36,187
  • 11
  • 83
  • 112
0

Instead of

'["openldap"]["server"]["ROOTDN"] --option'

try using ruby's string interpolation

"#{node['openldap']['server"]['ROOTDN']} --option"

Like so:

execute 'Ldap: Installing Openldap Instance' do
  command "#{node['openldap']['server']['DJDEST']}/setup --cli --no-prompt--rootUserDN #{node['openldap']['server']['ROOTDN']} --rootUserPassword #{node['openldap']['server']['ROOTPASS']} --hostname #{node['openldap']['server']['ODJFQDN']} --ldapPort #{node['openldap']['server']['LDAPPORT']} --adminConnectorPort #{node['openldap']['server']['ADMINPORT']} --baseDN #{node['openldap']['server']['BASEDN']} --addBaseEntry --doNotStart --acceptLicense"
  action :run
end
justMiles
  • 553
  • 3
  • 11