jebjeb
jebjeb

Reputation: 125

I need some assistance defining variables in a chef recipe

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?

Upvotes: 0

Views: 84

Answers (2)

StephenKing
StephenKing

Reputation: 37580

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

Upvotes: 1

justMiles
justMiles

Reputation: 573

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

Upvotes: 0

Related Questions