https://pulumi.com logo
#getting-started
Title
# getting-started
f

flaky-beach-82888

02/08/2022, 10:09 PM
can someone help me understand how to use cloudinit with pulumi? I tried the following: main
Copy code
resource_config = cloudinit.Config("resource", base64_encode=False,
                                    gzip=False,
                                    parts=[cloudinit.GetConfigPartArgs(
                                        content="baz",
                                        content_type="text/cloud-config",
                                        filename="cloudinit/test.yaml",
                                    )])

node0 = hcloud.Server(
  get_name('node'),
  image=image,
  location=location,
  server_type=server_type,
  ssh_keys=[default_ssh.id],
  user_data=resource_config
)
cloudinit file - taken from cloudinit docu
Copy code
#cloud-config

# run commands
# default: none
# runcmd contains a list of either lists or a string
# each item will be executed in order at rc.local like level with
# output to the console
# - runcmd only runs during the first boot
# - if the item is a list, the items will be properly executed as if
#   passed to execve(3) (with the first arg as the command).
# - if the item is a string, it will be simply written to the file and
#   will be interpreted by 'sh'
#
# Note, that the list has to be proper yaml, so you have to quote
# any characters yaml would eat (':' can be problematic)
runcmd:
 - [ ls, -l, / ]
 - [ sh, -xc, "echo $(date) ': hello world!'" ]
 - [ sh, -c, echo "=========hello world'=========" ]
 - ls -l /root
 # Note: Don't write files to /tmp from cloud-init use /run/somedir instead.
 # Early boot environments can race systemd-tmpfiles-clean LP: #1707222.
 - mkdir /run/mydir
 - [ wget, "<http://slashdot.org>", -O, /run/mydir/index.html ]
pulumi up runs through without errors, but cloud init is not applied. -> directory /run/mydir does not exists. edit: I use hetzner cloud, user_data should be supported according to pulumi doc.
1
l

little-cartoon-10569

02/08/2022, 10:25 PM
content
has to be the actual content.
filename
is just a string that is put into the MIME part header.
That code will create a script called "cloudinit/test.yaml" on your target system, containing "baz".
I use cloudinit via typescript, so there may be differences, but: you probably want to use the get_config function, the Config resource is deprecated; and you need to use
rendered
on the object returned from get_config, in order to get the rendered JSON object of the MIME parts.
f

flaky-beach-82888

02/08/2022, 11:23 PM
@little-cartoon-10569 awesome, thank you 🙏 this helped a lot as I was totally confused by content and filename. I adjusted my file accordingly and now have it got working :)
Copy code
# read cloud-init config payload from file
with open(cloud_init_path) as f:
  init_payload = f.read()

# create cloud-init config  
foo = cloudinit.get_config(base64_encode=False,
                                    gzip=False,
                                    parts=[cloudinit.GetConfigPartArgs(
                                        content=init_payload,
                                        content_type="text/cloud-config",
                                        filename="init.cfg",
                                    )])

node0 = hcloud.Server(
  get_name('node'),
  image=image,
  location=location,
  server_type=server_type,
  ssh_keys=[default_ssh.id],
  user_data=foo.rendered # set cloudinit config
)
👍 1
10 Views