Skip to content

Commit 593a271

Browse files
committed
role: heat_stack - compress user data support
Add support for passing user data as a compressed archive. Assisted-By: Claude Code/claude-4.5-sonnet Signed-off-by: Harald Jensås <hjensas@redhat.com>
1 parent 02c69ae commit 593a271

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

roles/heat_stack/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ input.
66
When the stack has been successfully created/updated the stack output is stored
77
in the `stack_outputs` fact, and also written to file.
88

9+
## Role Variables
10+
11+
- `os_cloud`: OpenStack cloud name from clouds.yaml
12+
- `stack_name`: Name of the Heat stack to create/update
13+
- `stack_template_path`: Path to the Heat template file
14+
- `stack_parameters`: Dictionary of parameters to pass to the Heat template
15+
- `compress_heat_files`: (Optional) List of file archives to compress for use as user data. Each item should define:
16+
- `archive`: Base name for the archive (without extension)
17+
- `files`: List of files to include in the tar.gz archive
18+
919
## Example playbook
1020

1121
```yaml
@@ -21,3 +31,57 @@ in the `stack_outputs` fact, and also written to file.
2131
stack_template_path: "{{ stack_template_path }}"
2232
stack_parameters: "{{ stack_parameters }}"
2333
```
34+
35+
## Compressing files for user data
36+
37+
When you need to pass multiple files as user data to instances, you can use the
38+
`compress_heat_files` variable to create compressed tar archives that are base64
39+
encoded. This is useful for passing scripts, configuration files, or other data
40+
that instances need at boot time.
41+
42+
```yaml
43+
- name: Deploy stack with compressed user data
44+
hosts: localhost
45+
roles:
46+
- role: heat_stack
47+
vars:
48+
...
49+
compress_heat_files:
50+
- archive: "data"
51+
files:
52+
- "script.sh"
53+
- "config.yaml"
54+
- "setup.py"
55+
```
56+
57+
The role will create `data.tar.gz` and `data.tar.gz.b64` files in the same
58+
directory as the stack template. The base64-encoded version can then be referenced
59+
in your Heat template using `get_file`.
60+
61+
### Heat template example
62+
63+
```yaml
64+
resources:
65+
server-write-files:
66+
type: OS::Heat::CloudConfig
67+
properties:
68+
cloud_config:
69+
write_files:
70+
- path: /tmp/data.tar.gz
71+
encoding: b64
72+
content: {get_file: archive.tar.gz.b64}
73+
owner: root:root
74+
permissions: '0644'
75+
76+
server-runcmd:
77+
type: OS::Heat::CloudConfig
78+
properties:
79+
cloud_config:
80+
runcmd:
81+
- ['tar', '-xzf', '/tmp/data.tar.gz', '-C', '/opt/']
82+
- ['chmod', '+x', '/opt/script.sh']
83+
- ['/opt/script.sh']
84+
```
85+
86+
This example writes the compressed archive to the instance, then extracts it and
87+
runs a script from the archive during instance initialization.

roles/heat_stack/defaults/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@
1717
hotstack_work_dir: "{{ playbook_dir }}"
1818
os_cloud: "{{ lookup('ansible.builtin.env', 'OS_CLOUD') }}"
1919
hotstack_revive_snapshot: false
20+
# List of file archives to compress and base64 encode for user data.
21+
# Creates tar.gz and tar.gz.b64 files that can be referenced in Heat
22+
# template using get_file. See README.md for complete usage examples.
23+
compress_heat_files: []

roles/heat_stack/tasks/main.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@
3636
msg:
3737
"{{ _latest_snapset.output }}"
3838

39+
- name: Compress files for user data
40+
when: compress_heat_files | default([]) | length > 0
41+
block:
42+
- name: Create tar archives and compress
43+
ansible.builtin.shell: |
44+
tar -czf "{{ item.archive }}.tar.gz" {{ item.files | join(' ') }}
45+
base64 -w0 "{{ item.archive }}.tar.gz" > "{{ item.archive }}.tar.gz.b64"
46+
args:
47+
chdir: "{{ stack_template_path | dirname }}"
48+
loop: "{{ compress_heat_files }}"
49+
changed_when: true
50+
3951
- name: Create stack
4052
openstack.cloud.stack:
4153
cloud: "{{ os_cloud }}"

0 commit comments

Comments
 (0)