Retrieve contents of file between resources


#1

I’m attempting to modify the opscode-mysql cookbook to add support for the
MySQL 5.6 RHEL binaries. Since MySQL 5.6.8, the RPM package install adds the
–random-password flag to mysql_install_db which generates a random password at
package install time and stores it in /root/.mysql_secret. I need to parse the
file and extract the password after the package has been installed and pass
it into the execute[“assign-root-password”] resource.

The file looks like this:

# The random password set for the root user at Mon Mar 25 21:59:23 2013

(local time): abcdefg

I can extract the password with this (simplified) code:

if ::File.file?('/root/.mysql_secret')
  initial_password =

::File.readlines(mysql_secret_file).grep(/random/).last.split(’: ').last.chomp
end
initial_password_param = initial_password ? “-p#{initial_password}” : “”

Because chef executes ruby code before the resources are executed, I can’t
figure out how to extract that temporary random password after the package
install and pass it to execute[assign-root-password].

Ideally, I’d like to be able to do something like this:

execute "assign-root-password" do
  command "\"#{node['mysql']['mysqladmin_bin']}\" -u root

#{initial_password_param} password
"#{node[‘mysql’][‘server_root_password’]}"“
action :run
only_if “”#{node[‘mysql’][‘mysql_bin’]}” -u root
#{initial_password_param} -e ‘show databases;’"
end

I’ve thought about using a ruby_block, but I can’t see a way to pass the
variable into the execute.

Hopefully this is more clear than mud. Thanks in advance for any assistance!
Aaron


#2

I have this in an ruby block that may do something similar to what you want.
I found that if i set items in the execute do block to variables, it would
fill them in at compile time rather than run time.
setting it with the ruby block works at converge time. I’ve dumbed it down
a bit from how it is in my recipe, but this should give you a start:

mystring = “goodbye”

ruby_block “generate_my_execute_command” do
block do
resources(“execute[my_execute]”).command "echo #{mystring} >
/tmp/thefile"
resources(“execute[my_execute]”).action :run
end
end

execute “my_execute” do
command "echo not doing anything"
action :nothing
end

On Mon, Mar 25, 2013 at 6:45 PM, aaron@9minutesnooze.com wrote:

I’m attempting to modify the opscode-mysql cookbook to add support for the
MySQL 5.6 RHEL binaries. Since MySQL 5.6.8, the RPM package install adds
the
–random-password flag to mysql_install_db which generates a random
password at
package install time and stores it in /root/.mysql_secret. I need to
parse the
file and extract the password after the package has been installed and
pass
it into the execute[“assign-root-password”] resource.

The file looks like this:

# The random password set for the root user at Mon Mar 25 21:59:23 2013

(local time): abcdefg

I can extract the password with this (simplified) code:

if ::File.file?('/root/.mysql_secret')
  initial_password =

::File.readlines(mysql_secret_file).grep(/random/).last.split(’:
’).last.chomp
end
initial_password_param = initial_password ? “-p#{initial_password}” :
""

Because chef executes ruby code before the resources are executed, I can’t
figure out how to extract that temporary random password after the package
install and pass it to execute[assign-root-password].

Ideally, I’d like to be able to do something like this:

execute "assign-root-password" do
  command "\"#{node['mysql']['mysqladmin_bin']}\" -u root

#{initial_password_param} password
"#{node[‘mysql’][‘server_root_password’]}"“
action :run
only_if “”#{node[‘mysql’][‘mysql_bin’]}” -u root
#{initial_password_param} -e ‘show databases;’"
end

I’ve thought about using a ruby_block, but I can’t see a way to pass the
variable into the execute.

Hopefully this is more clear than mud. Thanks in advance for any
assistance!
Aaron