Just enough Ruby for Chef
- Refer Here for the official docs from chef
- Local variables
x = 1 # number
y = 'hello' # string
z = "hai" # string
- Embed a ruby variable in a string
topic = 'chef'
'Hello all, Welcome to #{topic}'
"Hello all, Welcome to #{topic}"
- True and false (boolean)
- Arrays
- White space Arrays: This is shortcut for creating arrays without quotes and commas
- Hash:
- Conditional Statement using if else:
if <condition>
# if block
elsif <condition>
# else if block
else
# else block
end
- Looping using each
%w(redhat ubuntu).each do |osname|
puts(osname)
end
- Other useful statements
Chef contd..
- Now lets try to apply whitespace array to array for package names in the apache_phpinfo cookbook Refer Here for the change set
- Now apply the kitchen convergence and verify if everything works or not
- Now our aim is to ensure this cookbook works for redhat instances also. The manual steps are
sudo yum install httpd -y
sudo systemctl enable httpd
sudo systemctl start httpd
sudo yum install php php-mysql php-fpm -y
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
sudo systemctl restart httpd
- When we want to run the recipe on different distributions of linux we should have a way to identify the way to find the information about the node on which recipe is executing.
- For doing this in chef there is tool called as
ohaiwhich gets installed on the node and informs the chef server about the details of the node
