[ovirt-users] Add multiple files to a VM via cloud-init

Juan Hernandez jhernand at redhat.com
Mon Nov 3 08:40:28 UTC 2014


On 11/02/2014 03:37 PM, Amedeo Salvati wrote:
> Hi Juan,
> 
> this means that example of python code on 
> http://www.ovirt.org/Features/Cloud-Init_Integration that I wrote some 
> months ago against v3.4 could be changed by encoding the content on base64?
> 
> at this time of this writing I haven't v3.5 to test the script / new code
> 
> best regards
> Amedeo Salvati
> 

Using base64 to encode the content of files is just an option offered by
cloud-init. You can use it or not, both alternatives should work fine. I
think that using base64 is slightly better, because you can use it for
binary or text files, and it is easier to paste into the cloud-init file.

> Il 01/11/2014 00:00, users-request at ovirt.org ha scritto:
>> The "custom_script" element doesn't work in combination with cloud-init
>> and run once. To make it work you have to use cloud-init and a file
>> element containing your custom script. Here you have an example:
>>
>> #!/usr/bin/python
>>
>> import base64
>> import re
>>
>> from ovirtsdk.api import API
>> from ovirtsdk.xml import params
>>
>> # A simple function to encode using base64 and now new lines:
>> def encode(s):
>>     return re.sub("\s+", "", base64.encodestring(s))
>>
>> # Connect to the server:
>> api = API(
>>    url="https://engine35.example.com/ovirt-engine/api",
>>    username="admin at internal",
>>    password="redhat123",
>>    insecure=True,
>>    debug=True
>> )
>>
>> # Find the virtual machine:
>> myvm = api.vms.get(name="myvm")
>>
>> # Prepare the cloud-init custom script to write files:
>> myscript = """\
>> write_files:
>> """
>>
>> # Append one file:
>> myscript += """\
>> - encoding: b64
>>    content: %s
>>    owner: root:root
>>    path: /etc/firstfile.txt
>>    permissions: '0644'
>> """ % encode("The content of the first file")
>>
>> # Append another file:
>> myscript += """\
>> - encoding: b64
>>    content: %s
>>    owner: root:root
>>    path: /etc/secondfile.txt
>>    permissions: '0644'
>> """ % encode("The content of the second file")
>>
>> # Prepare the action to trigger initialization using cloud-init:
>> action = params.Action(
>>    vm=params.VM(
>>      initialization=params.Initialization(
>>        cloud_init=params.CloudInit(
>>          files=params.Files(
>>            file=[
>>              params.File(
>>                name="myscript",
>>                type_="plaintext",
>>                content=myscript
>>              )
>>            ]
>>          )
>>        )
>>      )
>>    )
>> )
>>
>> # Start the virtual machine:
>> myvm.start(action)
>>
>> # Disconnect from the server:
>> api.disconnect()
>>
>> Regarding the problem with the root password we have a bug in 3.5 that
>> makes this fail. See here:
>>
>>    https://bugzilla.redhat.com/1156155
>>
>> The problem is that we are not passing the user name to cloud-init, and
>> as a result it is changing the default cloud-init user password, not the
>> root pssword. To workaround the issue you can edit the
>> "/etc/cloud/cloud.cfg" file of the VM and change the default user name:
>>
>>    system_info:
>>    distro: ...
>>    default_user:
>>      name: root  <-- Change this from "fedora" or "cloud-init" to root
> 

-- 
Dirección Comercial: C/Jose Bardasano Baos, 9, Edif. Gorbea 3, planta
3ºD, 28016 Madrid, Spain
Inscrita en el Reg. Mercantil de Madrid – C.I.F. B82657941 - Red Hat S.L.



More information about the Users mailing list