Skip to content

Instantly share code, notes, and snippets.

@holmanb
Created November 23, 2021 00:47
Show Gist options
  • Save holmanb/b69e142a2b6be5729a1f7b7bf4e0434c to your computer and use it in GitHub Desktop.
Save holmanb/b69e142a2b6be5729a1f7b7bf4e0434c to your computer and use it in GitHub Desktop.
root@me:~# cloud-init devel schema --docs cc_write_files
Write Files
-----------
**Summary:** write arbitrary files
Write out arbitrary content to files, optionally setting permissions.
Parent folders in the path are created if absent.
Content can be specified in plain text or binary. Data encoded with
either base64 or binary gzip data can be specified and will be decoded
before being written. For empty file creation, content can be omitted.
.. note::
if multiline data is provided, care should be taken to ensure that it
follows yaml formatting standards. to specify binary data, use the yaml
option ``!!binary``
.. note::
Do not write files under /tmp during boot because of a race with
systemd-tmpfiles-clean that can cause temp files to get cleaned during
the early boot process. Use /run/somedir instead to avoid race
LP:1707222.
**Internal name:** ``cc_write_files``
**Module frequency:** once-per-instance
**Supported distros:** all
**Config schema**:
**write_files:** (array of object)
Each item in **write_files** list supports the following keys:
**path:** (string) Path of the file to which ``content`` is decoded and written
**content:** (string) Optional content to write to the provided ``path``. When content is present and encoding is not 'text/plain', decode the content prior to writing. Default: **''**
**owner:** (string) Optional owner:group to chown on the file. Default: **root:root**
**permissions:** (string) Optional file permissions to set on ``path`` represented as an octal string '0###'. Default: **'0644'**
**encoding:** (string) Optional encoding type of the content. Default is **text/plain** and no content decoding is performed. Supported encoding types are: gz, gzip, gz+base64, gzip+base64, gz+b64, gzip+b64, b64, base64.
**append:** (boolean) Whether to append ``content`` to existing file if ``path`` exists. Default: **false**.
**defer:** (boolean) Defer writing the file until 'final' stage, after users were created, and packages were installed. Default: **False**.
**Examples**::
# Write out base64 encoded content to /etc/sysconfig/selinux
write_files:
- encoding: b64
content: CiMgVGhpcyBmaWxlIGNvbnRyb2xzIHRoZSBzdGF0ZSBvZiBTRUxpbnV4...
owner: root:root
path: /etc/sysconfig/selinux
permissions: '0644'
# --- Example2 ---
# Appending content to an existing file
write_files:
- content: |
15 * * * * root ship_logs
path: /etc/crontab
append: true
# --- Example3 ---
# Provide gziped binary content
write_files:
- encoding: gzip
content: !!binary |
H4sIAIDb/U8C/1NW1E/KzNMvzuBKTc7IV8hIzcnJVyjPL8pJ4QIA6N+MVxsAAAA=
path: /usr/bin/hello
permissions: '0755'
# --- Example4 ---
# Create an empty file on the system
write_files:
- path: /root/CLOUD_INIT_WAS_HERE
# --- Example5 ---
# Defer writing the file until after the package (Nginx) is
# installed and its user is created alongside
write_files:
- path: /etc/nginx/conf.d/example.com.conf
content: |
server {
server_name example.com;
listen 80;
root /var/www;
location / {
try_files $uri $uri/ $uri.html =404;
}
}
owner: 'nginx:nginx'
permissions: '0640'
defer: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment