Chef Multi line command
The &&
is the logical and
operator of a shell. Either you explicitly start the command in a shell, like:
execute 'Execute a shell' do
command "bash -c 'cmd1 && cmd2 && ..'"
end
or you use the bash resource:
bash 'Execute bash script' do
code <<-EOH
cmd1 \
&& cmd2 \
&& ...
EOH
end
Chef splits 'execute' command on multiple lines
Found the answer thanks to /u/coderanger's comment above!
I was getting instance_id
from ec2metadata as follows:
instance_id = shell_out("ec2metadata --instance-id").stdout
Adding .strip
fixed it:
instance_id = shell_out("ec2metadata --instance-id").stdout.strip
How do I add multiline statement inside chef gaurd?
Chef guards take in a block, like the first example here. Blocks can be multi line, but can't have methods declared inside of them. If you copy the contents of the method you found and put them inside the only_if
that should work.
powershell_script "Restart Computer" do
...
only_if do
<contents of method here>
end
end
While this will work, the cleaner way to implement such an extensive guard is to put the reboot_pending
method in a library and call that method from your recipe.
Which Chef resource can I execute multiple lines of BASH code using Chef's sensititve property?
Answered in comments, true
and false
are lowercase in Ruby. Using True
makes Ruby try to look for a const that doesn't exist.
Using a Chef recipe to append multiple lines to a config file
As you said yourself, the recommended Chef pattern is to manage the whole file.
If you're using Chef 11 you could probably make use of partials for what you're trying to achieve.
There's more info here and on this example cookbook.
As long as you have access to the original config template, just append <%= render "original_config.erb" %>
to the top of your parms_to_append.conf
template.
How can I create a simple text file that contains more than one line of text with CHEF?
You would use either the file
resource or template
resource, depending on if you wanted simple inline content, or a more complex Erb template:
file '/foo' do
content "line one\nline two\n"
end
Chef - execute vs bash resource
For a single script, use an execute
. The bash
resource is for including the script contents inline in the recipe code.
How to parse multiline stdout with ruby, in Inspec test
Why are you using Open3
? You want to be using the command
resource, not direct Ruby command execution. That said, you would just compare to a string with \n
in it.
Related Topics
Elf Header or Installation Issue with Bcrypt in Docker Container
What Is the Recommended Way to Perform Source-Level Debugging of System Library Calls
New Scala Worksheets Not Evaluated in Eclipse
Bash Sort - How to Sort Using Timestamp
How to Ssh Multiple Hops Without Putting the Local Rsa Key Everywhere
How to Limit the Number of Results Returned from Grep
How to Delete First Two Lines and Last Four Lines from a Text File with Bash
Automating Amazon Ebs Snapshots Anyone Have a Good Script or Solution for This on Linux
"Couldn't Find a File Descriptor Referring to the Console" on Ubuntu Bash on Windows
Which Context Are Softirq and Tasklet In
Delete a Column from a Delimited File in Linux
Install Marklogic Centos Virtualbox Vm
Bash: Best Architecture for Reading from Two Input Streams
Set Filetype and Comment Key Map with .S File
Need an Overview of Debugging Process from the Hardware Layer
How to Force 'Cp' to Overwrite Directory Instead of Creating Another One Inside