Previous step to define server
Jazb opened this issue · comments
How can I do so that a previous step can ask me about the server to use
I have tried a series of things with "ask" but it doesn't work, I understand that the server stament is part of the runbook DSL and receives only one string but in what way can I make it modular and save in a variable and then define it, my best try has been the next:
Runbook.book "Restart Nginx" do
description <<-DESC
This is a simple runbook to restart nginx and verify
it starts successfully
DESC
target_host = ""
section "Restart Nginx" do
step "Define server to restart" do
ask "Name of server to restart", into: :target_host, default: "localhost", echo: true
ruby_command do |rb_cmd, metadata, run|
target_host = rb_cmd.parent.target_host
end
wait 5
end
server "#{target_host}"
user "jazb"
step "Stop Nginx" do
note "Stopping Nginx..."
command "whoami"
end
step { wait 5 }
step "Start Nginx" do
note "Starting Nginx..."
command "whoami"
confirm "Nginx is taking traffic?"
notice "Make sure to report why you restarted nginx"
end
end
end
Hi Jesus,
You are correct that ask
receives its value at runtime, but server
is evaluated at compile time, so it will have already executed by the time ask
is executed.
You have a couple options.
- You can define the commands in a
ruby_command
block because this is evaluated at runtime.
ask "Name of server to restart", into: :target_host, default: "localhost"
ruby_command do |rb_cmd, metadata, run|
command "whoami", ssh_config: {servers: [target_host], user: "jazb"}
end
- You can pass the host in as an environment variable, or have it somehow be set outside of the runbook.
target_host = ENV["TARGET_HOST"] || "localhost"
Runbook.book "Restart Nginx" do
section "Restart Nginx" do
server target_host
user "jazb"
step "Define server to restart" do
wait 5
end
step "Stop Nginx" do
note "Stopping Nginx..."
command "whoami"
end
end
end
I'm going to close this issue, but feel free to reopen it if you run into further issues.