Skip to content

Commit 3a8e0f0

Browse files
committed
guestfs: bringup: fix ssh key injection
This ensures the user exists before any SSH operations and uses the standardized virt-builder.j2 template for consistent image customization. The custom-image role now: * Detects the control host's kdevops UID and preserves it in guests * Uses virt-builder.j2 template for all customization commands * Handles both system and non-system libvirt configurations * Creates temporary command files and cleans them up properly Fixes error: sudo virt-sysprep -a /var/lib/libvirt/images/kdevops/guestfs/debian13/root.raw \ --hostname debian13 --ssh-inject \ kdevops:file:/media/tarkir/dagomez/src/linux-kdevops/ kdevops/guestfs/debian13/ssh/id_ed25519.pub \ --timezone Etc/UTC [ 0.0] Examining the guest ... [ 2.2] Performing "abrt-data" ... [ 2.2] Performing "backup-files" ... [ 2.3] Performing "bash-history" ... ... [ 3.2] Performing "customize" ... [ 3.2] Setting a random seed virt-sysprep: warning: random seed could not be set for this type of guest [ 3.3] Setting the machine ID in /etc/machine-id [ 3.3] Setting the hostname: debian13 [ 4.0] SSH key inject: kdevops virt-sysprep: error: ssh-inject: the user kdevops does not exist on the guest If reporting bugs, run virt-sysprep with debugging enabled and include the complete output: virt-sysprep -v -x [...] Generated-by: Claude AI Signed-off-by: Daniel Gomez <[email protected]>
1 parent a5a119d commit 3a8e0f0

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

playbooks/roles/base_image/tasks/custom-image.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,78 @@
8585
chdir: "{{ custom_image_dir }}"
8686
changed_when: false
8787

88+
- name: Get the UID of the kdevops user on the control host
89+
ansible.builtin.command:
90+
cmd: "id -u kdevops"
91+
register: id_output
92+
changed_when: false
93+
failed_when: false
94+
when:
95+
- not sentinel_stat.stat.exists
96+
97+
- name: Set the kdevops UID for custom image
98+
ansible.builtin.set_fact:
99+
kdevops_uid: "-u {{ id_output.stdout }}"
100+
when:
101+
- not sentinel_stat.stat.exists
102+
- id_output.rc == 0
103+
104+
- name: Set default kdevops UID for custom image if user doesn't exist
105+
ansible.builtin.set_fact:
106+
kdevops_uid: ""
107+
when:
108+
- not sentinel_stat.stat.exists
109+
- id_output.rc != 0
110+
111+
- name: Create a temporary file for virt-customize commands
112+
ansible.builtin.tempfile:
113+
state: file
114+
register: custom_command_file
115+
when:
116+
- not sentinel_stat.stat.exists
117+
118+
- name: Construct the virt-customize command file for custom image
119+
ansible.builtin.template:
120+
src: "{{ role_path }}/templates/virt-builder.j2"
121+
dest: "{{ custom_command_file.path }}"
122+
mode: "u=rw"
123+
when:
124+
- not sentinel_stat.stat.exists
125+
126+
- name: Customize the downloaded image with kdevops user and settings
127+
become: true
128+
become_method: ansible.builtin.sudo
129+
ansible.builtin.command:
130+
argv:
131+
- "virt-customize"
132+
- "-a"
133+
- "{{ custom_image }}"
134+
- "--commands-from-file"
135+
- "{{ custom_command_file.path }}"
136+
when:
137+
- libvirt_uri_system|bool
138+
- not sentinel_stat.stat.exists
139+
140+
- name: Customize the downloaded image with kdevops user and settings (non-root)
141+
ansible.builtin.command:
142+
argv:
143+
- "virt-customize"
144+
- "-a"
145+
- "{{ custom_image }}"
146+
- "--commands-from-file"
147+
- "{{ custom_command_file.path }}"
148+
when:
149+
- not libvirt_uri_system|bool
150+
- not sentinel_stat.stat.exists
151+
152+
- name: Clean up the virt-customize command file
153+
ansible.builtin.file:
154+
path: "{{ custom_command_file.path }}"
155+
state: absent
156+
when:
157+
- custom_command_file.path is defined
158+
- not sentinel_stat.stat.exists
159+
88160
- name: Touch the custom image sentinel
89161
ansible.builtin.file:
90162
path: "{{ custom_image_ok }}"

playbooks/roles/guestfs/tasks/main.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@
3131
- bringup
3232
ansible.builtin.set_fact:
3333
base_image: "{{ storagedir }}/base_images/{{ virtbuilder_os_version }}.raw"
34+
when:
35+
- not guestfs_has_custom_raw_image|bool
36+
delegate_to: localhost
37+
38+
- name: Set the pathname of the custom OS base image
39+
tags:
40+
- base_image
41+
- bringup
42+
ansible.builtin.set_fact:
43+
base_image: "{{ storagedir }}/custom_images/{{ virtbuilder_os_version }}/{{ virtbuilder_os_version }}.raw"
44+
when:
45+
- guestfs_has_custom_raw_image|bool
3446
delegate_to: localhost
3547

3648
- name: Ensure the required base OS image exists

0 commit comments

Comments
 (0)