Search code examples
templatesattributeschef-infracookbook

Execute command line in attribute


I want to generate MAC address and UUID in attribute and then pass the values to template. something like this :

Attribute/default.rb:

default['libvirt']['xml_mac_Adrr'] = 'openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/:$//''
default['libvirt']['xml_uuid'] = 'uuidgen virbr0'

Template/network.erb:

<uuid><%= node['libvirt']['xml_uuid'] %></uuid>
  <mac address='<%= node['libvirt']['xml_mac_Adrr']%>'/>

How can I do that?

UPDATE I want to modify the default.xml network for the virtual network. Basically, we have to do it by virsh-net command Now I want to use a template to pass UUID & MAC address values to XML file and modify it in guest machine. this is my recipe:

template '/etc/libvirt/qemu/network/default.xml' do
 source 'qemu-network.erb'
 owner "root"
 group "root"
 mode "0644"
end

Solution

  • Yo can use backquotes to execute shell commands inside ruby and capture the response:

    default['libvirt']['xml_mac_Adrr'] = `openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/:$//'`
    default['libvirt']['xml_uuid'] = `uuidgen virbr0`
    

    EDIT:

    The second problem I see is that you have to use instance variables in the controller to share information with the view. So the best way would be:

    @mac = `openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/:$//'`
    @uuid = `uuidgen virbr0`
    

    Then at view level you can use:

    <uuid><%=@uuid %></uuid>
    <mac address='<%=@mac %>'/>