From 95e0c5d810852c8bb3bdc7b74dfe670d48757491 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Jeanneret?= Date: Mon, 9 Sep 2024 14:55:11 +0200 Subject: [PATCH] Correct how config_drive content is generated It seems older cloud-init, as 22.1 exposed in RHEL-9.2, is having issues with the way some of the content is generated, preventing to apply the whole configuration. Namely, in the libvirt_manager, we might have no network configuration; but in the config_drive, a file was created with `''` as content, leading to cloud-init to crash (not a valid YAML dict). This patch changes how configuration snippets are checked and generated. It also ensures the `#cloud-config` header is present in all generated files. The error as detected by cloud-init was as follow: ``` [ 5.237126] cloud-init[744]: Cloud-init v. 22.1-9.el9 running 'init-local' at Mon, 09 Sep 2024 11:16:09 +0000. Up 5.21 seconds. [ 5.387470] cloud-init[744]: 2024-09-09 11:16:10,072 - util.py[WARNING]: Failed loading yaml blob. Yaml load allows (,) root types, but got str instead [ 5.389029] cloud-init[744]: 2024-09-09 11:16:10,072 - util.py[WARNING]: Getting data from failed ``` With newer cloud-init (23.x) as shipped by RHEL-9.4, the error wasn't fatal and it was still continuing to apply the other files (in this case, the user-data). In earlier version, it was a hard stop. Using `cloud-init devel schema -c ` also raised the missing header: ``` [root@builder2 /]# cloud-init devel schema -c config_drive/meta-data Error: Cloud config schema errors: format-l1.c1: File config_drive/meta-data needs to begin with "#cloud-config" ``` And adding the header, but without any actual data, lead to: ``` [root@builder2 /]# cloud-init devel schema -c config_drive/network-config Error: Cloud-config is not a YAML dict. ``` --- roles/config_drive/tasks/main.yml | 9 ++++++--- roles/config_drive/templates/meta-data.j2 | 2 +- roles/config_drive/templates/network-config.j2 | 1 + roles/config_drive/templates/user-data.j2 | 2 -- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/roles/config_drive/tasks/main.yml b/roles/config_drive/tasks/main.yml index 692391b2f5..1651c7a2a8 100644 --- a/roles/config_drive/tasks/main.yml +++ b/roles/config_drive/tasks/main.yml @@ -49,6 +49,9 @@ - name: Generate user-data register: _user_data_change + when: + - cifmw_config_drive_userdata is defined + - cifmw_config_drive_userdata | length > 0 ansible.builtin.template: src: "user-data.j2" dest: "{{ cifmw_config_drive_instancedir }}/user-data" @@ -57,8 +60,8 @@ - name: Generate network-data register: _net_data_change when: - - "cifmw_config_drive_networkconfig is defined" - - "cifmw_config_drive_networkconfig is not none" + - cifmw_config_drive_networkconfig is defined + - cifmw_config_drive_networkconfig | length > 0 ansible.builtin.template: src: "network-config.j2" dest: "{{ cifmw_config_drive_instancedir }}/network-config" @@ -95,6 +98,6 @@ -joliet -rock user-data meta-data {% if cifmw_config_drive_networkconfig is defined and - cifmw_config_drive_networkconfig is not none -%} + cifmw_config_drive_networkconfig | length > 0 -%} network-config {%- endif -%} diff --git a/roles/config_drive/templates/meta-data.j2 b/roles/config_drive/templates/meta-data.j2 index 96888d1989..076f311284 100644 --- a/roles/config_drive/templates/meta-data.j2 +++ b/roles/config_drive/templates/meta-data.j2 @@ -1,3 +1,3 @@ ---- +#cloud-config instance-id: {{ cifmw_config_drive_uuid }} local-hostname: {{ cifmw_config_drive_hostname }} diff --git a/roles/config_drive/templates/network-config.j2 b/roles/config_drive/templates/network-config.j2 index 59efc6e4ac..b985cb4a49 100644 --- a/roles/config_drive/templates/network-config.j2 +++ b/roles/config_drive/templates/network-config.j2 @@ -1 +1,2 @@ +#cloud-config {{ cifmw_config_drive_networkconfig | to_nice_yaml(indent=2) }} diff --git a/roles/config_drive/templates/user-data.j2 b/roles/config_drive/templates/user-data.j2 index a50c3d6033..cbe9a15795 100644 --- a/roles/config_drive/templates/user-data.j2 +++ b/roles/config_drive/templates/user-data.j2 @@ -1,4 +1,2 @@ #cloud-config -{% if cifmw_config_drive_userdata is defined and cifmw_config_drive_userdata is not none -%} {{ cifmw_config_drive_userdata | to_nice_yaml(indent=2) }} -{% endif %}