Repository navigation
OSAC-1558: add bm_host_metal3_provisioning template role #358
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| --- | ||
| - name: Fail if host class is not metal3 | ||
| ansible.builtin.fail: | ||
| msg: "bm_host_metal3_provisioning requires hostClass 'metal3', got '{{ bare_metal_instance.spec.hostClass | default('') }}'" | ||
| when: bare_metal_instance.spec.hostClass | default('') != "metal3" | ||
|
|
||
| - name: Parse template parameters | ||
| ansible.builtin.set_fact: | ||
| template_params: "{{ bare_metal_instance.spec.templateParameters | from_json }}" | ||
|
|
||
| - name: Fail if image URL is missing | ||
| ansible.builtin.fail: | ||
| msg: "templateParameters.imageURL is required for Metal3 provisioning" | ||
| when: template_params.imageURL | default('') | length == 0 | ||
|
|
||
| - name: Parse externalHostID into namespace and name | ||
| ansible.builtin.set_fact: | ||
| bmh_namespace: "{{ bare_metal_instance.spec.externalHostID.split('/')[0] }}" | ||
| bmh_name: "{{ bare_metal_instance.spec.externalHostID.split('/')[1] }}" | ||
|
|
||
| - name: Extract SSH key and userDataSecret from template parameters | ||
| ansible.builtin.set_fact: | ||
| ssh_key: "{{ template_params.sshKey | default('') }}" | ||
| user_data_secret_ref: "{{ template_params.userDataSecret | default('') }}" | ||
|
|
||
| - name: Get BareMetalHost | ||
| kubernetes.core.k8s_info: | ||
| api_version: metal3.io/v1alpha1 | ||
| kind: BareMetalHost | ||
| namespace: "{{ bmh_namespace }}" | ||
| name: "{{ bmh_name }}" | ||
| register: bmh_result | ||
|
Comment on lines
+26
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pass the remote kubeconfig to every Kubernetes module call. These Proposed fix pattern kubernetes.core.k8s_info:
+ kubeconfig: "{{ kubeconfig | default(omit) }}"
api_version: metal3.io/v1alpha1
kind: BareMetalHost kubernetes.core.k8s:
+ kubeconfig: "{{ kubeconfig | default(omit) }}"
state: present
definition:Apply the same module argument to every Kubernetes task in this file. As per coding guidelines, “Always include Also applies to: 69-75, 129-139, 141-152, 190-197, 199-208 🤖 Prompt for AI AgentsSources: Coding guidelines, Learnings |
||
|
|
||
| - name: Fail if BareMetalHost not found | ||
| ansible.builtin.fail: | ||
| msg: "BareMetalHost '{{ bmh_namespace }}/{{ bmh_name }}' not found" | ||
| when: bmh_result.resources | length == 0 | ||
|
|
||
| - name: Extract BMH info | ||
| ansible.builtin.set_fact: | ||
| bmh_info: "{{ bmh_result.resources[0] }}" | ||
|
|
||
| - name: Display current BMH state | ||
| ansible.builtin.debug: | ||
| msg: | ||
| - "BMH: {{ bmh_namespace }}/{{ bmh_name }}" | ||
| - "Provisioning State: {{ bmh_info.status.provisioning.state | default('unknown') }}" | ||
|
|
||
| - name: Check if BMH is already provisioned with the correct image | ||
| ansible.builtin.set_fact: | ||
| already_provisioned: >- | ||
| {{ (bmh_info.status.provisioning.state | default('') == 'provisioned') and | ||
| (bmh_info.status.provisioning.image.url | default('') == template_params.imageURL) }} | ||
|
Comment on lines
+49
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include checksum fields in the idempotency guard. The skip condition only compares the image URL, so a reused URL with a changed Proposed fix already_provisioned: >-
{{ (bmh_info.status.provisioning.state | default('') == 'provisioned') and
- (bmh_info.status.provisioning.image.url | default('') == template_params.imageURL) }}
+ (bmh_info.spec.image.url | default('') == template_params.imageURL) and
+ (bmh_info.spec.image.checksum | default('') == (template_params.imageChecksum | default('')) ) and
+ (bmh_info.spec.image.checksumType | default('auto') == (template_params.checksumType | default('auto')) ) }}🤖 Prompt for AI Agents |
||
|
|
||
| - name: Skip provisioning when BMH already has the desired image | ||
| when: already_provisioned | bool | ||
| ansible.builtin.debug: | ||
| msg: "BMH {{ bmh_namespace }}/{{ bmh_name }} is already provisioned with the desired image, skipping" | ||
|
|
||
| - name: Provision BMH | ||
| when: not (already_provisioned | bool) | ||
| block: | ||
| - name: Build userData content | ||
| when: ssh_key | length > 0 or user_data_secret_ref | length > 0 | ||
| block: | ||
| - name: Read userDataSecret from BareMetalInstance namespace | ||
| when: user_data_secret_ref | length > 0 | ||
| block: | ||
| - name: Fetch userDataSecret | ||
| kubernetes.core.k8s_info: | ||
| api_version: v1 | ||
| kind: Secret | ||
| namespace: "{{ bare_metal_instance.metadata.namespace }}" | ||
| name: "{{ user_data_secret_ref }}" | ||
| register: user_data_secret_result | ||
|
|
||
| - name: Fail if userDataSecret not found | ||
| ansible.builtin.fail: | ||
| msg: >- | ||
| Secret '{{ user_data_secret_ref }}' not found in namespace | ||
| '{{ bare_metal_instance.metadata.namespace }}' | ||
| when: user_data_secret_result.resources | length == 0 | ||
|
|
||
| - name: Extract user data content from Secret | ||
| ansible.builtin.set_fact: | ||
| existing_user_data: "{{ user_data_secret_result.resources[0].data.userdata | b64decode }}" | ||
|
Comment on lines
+84
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read the same Line 86 reads Proposed fix+ - name: Fail if userDataSecret is missing userData key
+ ansible.builtin.fail:
+ msg: "Secret '{{ user_data_secret_ref }}' must contain data.userData"
+ when: user_data_secret_result.resources[0].data.userData is not defined
+
- name: Extract user data content from Secret
ansible.builtin.set_fact:
- existing_user_data: "{{ user_data_secret_result.resources[0].data.userdata | b64decode }}"
+ existing_user_data: "{{ user_data_secret_result.resources[0].data.userData | b64decode }}"Also applies to: 129-139 🤖 Prompt for AI Agents |
||
|
|
||
| - name: Generate cloud-init with SSH key only | ||
| when: | ||
| - ssh_key | length > 0 | ||
| - user_data_secret_ref | length == 0 | ||
| ansible.builtin.set_fact: | ||
| final_user_data: | | ||
| #cloud-config | ||
| ssh_authorized_keys: | ||
| - {{ ssh_key }} | ||
|
Comment on lines
+88
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Find and examine the file
fd -t f "create.yaml" | grep "bm_host_metal3_provisioning"Repository: osac-project/osac-aap Length of output: 164 🏁 Script executed: #!/bin/bash
# Get file size and examine the specific lines
if [ -f "collections/ansible_collections/osac/templates/roles/bm_host_metal3_provisioning/tasks/create.yaml" ]; then
wc -l "collections/ansible_collections/osac/templates/roles/bm_host_metal3_provisioning/tasks/create.yaml"
echo "=== Lines 80-100 ==="
sed -n '80,100p' "collections/ansible_collections/osac/templates/roles/bm_host_metal3_provisioning/tasks/create.yaml"
echo "=== Lines 110-135 ==="
sed -n '110,135p' "collections/ansible_collections/osac/templates/roles/bm_host_metal3_provisioning/tasks/create.yaml"
fiRepository: osac-project/osac-aap Length of output: 2386 Change the merge path from The SSH-only path (lines 88–96) correctly uses Proposed fix- name: Parse existing cloud-init and merge SSH key
ansible.builtin.set_fact:
final_user_data: |
`#cloud-config`
{{ (existing_user_data | regex_replace('^`#cloud-config`\s*\n?', '') | from_yaml | default({}))
| combine({'ssh_authorized_keys':
((existing_user_data | regex_replace('^`#cloud-config`\s*\n?', '') | from_yaml | default({})).ssh_authorized_keys | default([]))
+ [ssh_key]})
| to_nice_yaml }}Consider extracting the regex/from_yaml operation into a separate task to avoid duplication: + - name: Parse existing cloud-init content
+ ansible.builtin.set_fact:
+ parsed_user_data: "{{ existing_user_data | regex_replace('^`#cloud-config`\\s*\\n?', '') | from_yaml | default({}, true) }}"
+
- name: Parse existing cloud-init and merge SSH key
ansible.builtin.set_fact:
final_user_data: |
`#cloud-config`
- {{ (existing_user_data | regex_replace('^`#cloud-config`\s*\n?', '') | from_yaml | default({}))
- | combine({'ssh_authorized_keys':
- ((existing_user_data | regex_replace('^`#cloud-config`\s*\n?', '') | from_yaml | default({})).ssh_authorized_keys | default([]))
+ {{ parsed_user_data
+ | combine({'ssh_authorized_keys':
+ (parsed_user_data.ssh_authorized_keys | default([]))
+ [ssh_key]})
| to_nice_yaml }}🤖 Prompt for AI Agents |
||
|
|
||
| - name: Use existing user data as-is (no SSH key to merge) | ||
| when: | ||
| - ssh_key | length == 0 | ||
| - user_data_secret_ref | length > 0 | ||
| ansible.builtin.set_fact: | ||
| final_user_data: "{{ existing_user_data }}" | ||
|
|
||
| - name: Merge SSH key into existing cloud-init user data | ||
| when: | ||
| - ssh_key | length > 0 | ||
| - user_data_secret_ref | length > 0 | ||
| block: | ||
| - name: Fail if user data is not cloud-config format | ||
| ansible.builtin.fail: | ||
| msg: >- | ||
| Cannot inject SSH key into user data that is not cloud-config format. | ||
| User data must start with '#cloud-config' to support SSH key injection. | ||
| Either remove the ssh_key parameter or provide user data in cloud-config | ||
| format with ssh_authorized_keys included. | ||
| when: not (existing_user_data | trim is match('^#cloud-config')) | ||
|
|
||
| - name: Parse existing cloud-init and merge SSH key | ||
| ansible.builtin.set_fact: | ||
| final_user_data: >- | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if spec.metaData maps to cloud init metadata, then the ssh keys can be injected there: https://docs.cloud-init.io/en/24.1/explanation/instancedata.html#v1-public-ssh-keys. |
||
| #cloud-config | ||
| {{ (existing_user_data | regex_replace('^#cloud-config\s*\n?', '') | from_yaml | default({})) | ||
| | combine({'ssh_authorized_keys': | ||
| ((existing_user_data | regex_replace('^#cloud-config\s*\n?', '') | from_yaml | default({})).ssh_authorized_keys | default([])) | ||
| + [ssh_key]}) | ||
| | to_nice_yaml }} | ||
|
|
||
| - name: Create userData Secret in BMH namespace | ||
| kubernetes.core.k8s: | ||
| state: present | ||
| definition: | ||
| apiVersion: v1 | ||
| kind: Secret | ||
| metadata: | ||
| name: "{{ bmh_name }}-user-data" | ||
| namespace: "{{ bmh_namespace }}" | ||
| stringData: | ||
| userData: "{{ final_user_data }}" | ||
|
Comment on lines
+63
to
+139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suppress Ansible logging for cloud-init and Secret payloads.
Proposed fix pattern - name: Build userData content
+ no_log: true
when: ssh_key | length > 0 or user_data_secret_ref | length > 0
block: - name: Create networkData Secret
+ no_log: true
when: template_params.networkData | default('') | length > 0
kubernetes.core.k8s:Also applies to: 141-152 🧰 Tools🪛 Checkov (3.3.1)[low] 63-142: Ensure block is handling task errors properly (CKV2_ANSIBLE_3) [low] 66-89: Ensure block is handling task errors properly (CKV2_ANSIBLE_3) [low] 105-130: Ensure block is handling task errors properly (CKV2_ANSIBLE_3) 🤖 Prompt for AI Agents |
||
|
|
||
| - name: Create networkData Secret | ||
| when: template_params.networkData | default('') | length > 0 | ||
| kubernetes.core.k8s: | ||
| state: present | ||
| definition: | ||
| apiVersion: v1 | ||
| kind: Secret | ||
| metadata: | ||
| name: "{{ bmh_name }}-network-data" | ||
| namespace: "{{ bmh_namespace }}" | ||
| stringData: | ||
| networkData: "{{ template_params.networkData }}" | ||
|
|
||
| - name: Build base BMH patch | ||
| ansible.builtin.set_fact: | ||
| bmh_patch: | ||
| spec: | ||
| image: | ||
| url: "{{ template_params.imageURL }}" | ||
| checksum: "{{ template_params.imageChecksum | default('') }}" | ||
| checksumType: "{{ template_params.checksumType | default('auto') }}" | ||
| online: true | ||
|
|
||
| - name: Add networkData to BMH patch | ||
| when: template_params.networkData | default('') | length > 0 | ||
| ansible.builtin.set_fact: | ||
| bmh_patch: >- | ||
| {{ bmh_patch | combine({ | ||
| 'spec': bmh_patch.spec | combine({ | ||
| 'networkData': { | ||
| 'name': bmh_name ~ '-network-data', | ||
| 'namespace': bmh_namespace | ||
| } | ||
| }) | ||
| }, recursive=True) }} | ||
|
|
||
| - name: Add userData to BMH patch | ||
| when: final_user_data is defined | ||
| ansible.builtin.set_fact: | ||
| bmh_patch: >- | ||
| {{ bmh_patch | combine({ | ||
| 'spec': bmh_patch.spec | combine({ | ||
| 'userData': { | ||
| 'name': bmh_name ~ '-user-data', | ||
| 'namespace': bmh_namespace | ||
| } | ||
| }) | ||
| }, recursive=True) }} | ||
|
|
||
| - name: Patch BareMetalHost with image and provisioning settings | ||
| kubernetes.core.k8s: | ||
| api_version: metal3.io/v1alpha1 | ||
| kind: BareMetalHost | ||
| namespace: "{{ bmh_namespace }}" | ||
| name: "{{ bmh_name }}" | ||
| state: present | ||
| definition: "{{ bmh_patch }}" | ||
|
|
||
| - name: Wait for BMH to reach provisioned state | ||
| kubernetes.core.k8s_info: | ||
| api_version: metal3.io/v1alpha1 | ||
| kind: BareMetalHost | ||
| namespace: "{{ bmh_namespace }}" | ||
| name: "{{ bmh_name }}" | ||
| register: bmh_provision_status | ||
| until: bmh_provision_status.resources[0].status.provisioning.state | default('') == "provisioned" | ||
| retries: 120 | ||
| delay: 30 | ||
|
|
||
| - name: Metal3 provisioning completed | ||
| ansible.builtin.debug: | ||
| msg: "Metal3 provisioning completed for BMH {{ bmh_namespace }}/{{ bmh_name }}" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| --- | ||
| - name: Parse externalHostID into namespace and name | ||
| ansible.builtin.set_fact: | ||
| bmh_namespace: "{{ bare_metal_instance.spec.externalHostID.split('/')[0] }}" | ||
| bmh_name: "{{ bare_metal_instance.spec.externalHostID.split('/')[1] }}" | ||
|
Comment on lines
+2
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate Line 5 assumes Proposed fix+- name: Validate externalHostID format
+ ansible.builtin.assert:
+ that:
+ - (bare_metal_instance.spec.externalHostID | default('')) is match('^[^/]+/[^/]+$')
+ fail_msg: "spec.externalHostID must be '<namespace>/<name>'"
+
- name: Parse externalHostID into namespace and name
ansible.builtin.set_fact:
bmh_namespace: "{{ bare_metal_instance.spec.externalHostID.split('/')[0] }}"
bmh_name: "{{ bare_metal_instance.spec.externalHostID.split('/')[1] }}"🤖 Prompt for AI Agents |
||
|
|
||
| - name: Get BareMetalHost | ||
| kubernetes.core.k8s_info: | ||
| api_version: metal3.io/v1alpha1 | ||
| kind: BareMetalHost | ||
| namespace: "{{ bmh_namespace }}" | ||
| name: "{{ bmh_name }}" | ||
| register: bmh_result | ||
|
Comment on lines
+7
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pass the remote kubeconfig to every Kubernetes module call. These Kubernetes tasks also omit Proposed fix pattern kubernetes.core.k8s_info:
+ kubeconfig: "{{ kubeconfig | default(omit) }}"
api_version: metal3.io/v1alpha1
kind: BareMetalHost kubernetes.core.k8s:
+ kubeconfig: "{{ kubeconfig | default(omit) }}"
api_version: v1
kind: SecretApply the same module argument to every Kubernetes task in this file. As per coding guidelines, “Always include Also applies to: 39-50, 52-61, 63-69, 71-77 🤖 Prompt for AI AgentsSources: Coding guidelines, Learnings |
||
|
|
||
| - name: Skip deprovisioning if BareMetalHost not found | ||
| when: bmh_result.resources | length == 0 | ||
| ansible.builtin.debug: | ||
| msg: "BareMetalHost '{{ bmh_namespace }}/{{ bmh_name }}' not found, skipping deprovisioning" | ||
|
|
||
| - name: Deprovision BMH | ||
| when: bmh_result.resources | length > 0 | ||
| block: | ||
| - name: Extract BMH info | ||
| ansible.builtin.set_fact: | ||
| bmh_info: "{{ bmh_result.resources[0] }}" | ||
|
|
||
| - name: Check if BMH has an image set | ||
| ansible.builtin.set_fact: | ||
| bmh_has_image: "{{ bmh_info.spec.image is defined and bmh_info.spec.image.url is defined and bmh_info.spec.image.url | length > 0 }}" | ||
|
|
||
| - name: Skip deprovisioning when no image is set | ||
| when: not (bmh_has_image | bool) | ||
| ansible.builtin.debug: | ||
| msg: "BMH {{ bmh_namespace }}/{{ bmh_name }} has no image set, skipping deprovisioning" | ||
|
|
||
| - name: Remove image and networkData from BMH | ||
| when: bmh_has_image | bool | ||
|
Comment on lines
+27
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clear stale BMH data refs even when no image URL is set. The patch is gated only on Proposed fix- - name: Check if BMH has an image set
+ - name: Check if BMH has provisioning fields set
ansible.builtin.set_fact:
- bmh_has_image: "{{ bmh_info.spec.image is defined and bmh_info.spec.image.url is defined and bmh_info.spec.image.url | length > 0 }}"
+ bmh_has_provisioning_fields: >-
+ {{ (bmh_info.spec.image is defined and bmh_info.spec.image.url is defined and bmh_info.spec.image.url | length > 0) or
+ (bmh_info.spec.networkData is defined) or
+ (bmh_info.spec.userData is defined) }}
- name: Skip deprovisioning when no image is set
- when: not (bmh_has_image | bool)
+ when: not (bmh_has_provisioning_fields | bool)
ansible.builtin.debug:
- msg: "BMH {{ bmh_namespace }}/{{ bmh_name }} has no image set, skipping deprovisioning"
+ msg: "BMH {{ bmh_namespace }}/{{ bmh_name }} has no provisioning fields set, skipping deprovisioning"
- name: Remove image and networkData from BMH
- when: bmh_has_image | bool
+ when: bmh_has_provisioning_fields | bool
block:Also applies to: 63-77 🤖 Prompt for AI Agents |
||
| block: | ||
| - name: Patch BareMetalHost to remove image, networkData, and userData | ||
| kubernetes.core.k8s: | ||
| api_version: metal3.io/v1alpha1 | ||
| kind: BareMetalHost | ||
| namespace: "{{ bmh_namespace }}" | ||
| name: "{{ bmh_name }}" | ||
| state: present | ||
| definition: | ||
| spec: | ||
| image: null | ||
| networkData: null | ||
| userData: null | ||
|
|
||
| - name: Wait for BMH to reach available or ready state | ||
| kubernetes.core.k8s_info: | ||
| api_version: metal3.io/v1alpha1 | ||
| kind: BareMetalHost | ||
| namespace: "{{ bmh_namespace }}" | ||
| name: "{{ bmh_name }}" | ||
| register: bmh_deprovision_status | ||
| until: bmh_deprovision_status.resources[0].status.provisioning.state | default('') in ["available", "ready"] | ||
| retries: 120 | ||
| delay: 30 | ||
|
|
||
| - name: Delete networkData Secret | ||
| kubernetes.core.k8s: | ||
| api_version: v1 | ||
| kind: Secret | ||
| namespace: "{{ bmh_namespace }}" | ||
| name: "{{ bmh_name }}-network-data" | ||
| state: absent | ||
|
|
||
| - name: Delete userData Secret | ||
| kubernetes.core.k8s: | ||
| api_version: v1 | ||
| kind: Secret | ||
| namespace: "{{ bmh_namespace }}" | ||
| name: "{{ bmh_name }}-user-data" | ||
| state: absent | ||
|
|
||
| - name: Metal3 deprovisioning completed | ||
| ansible.builtin.debug: | ||
| msg: "Metal3 deprovisioning completed for BMH {{ bmh_namespace }}/{{ bmh_name }}" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate
externalHostIDbefore splitting it.Line 19 assumes
spec.externalHostIDis exactlynamespace/name; malformed input fails with a Jinja index error before the role can produce an actionable validation failure.Proposed fix
🤖 Prompt for AI Agents