public inbox for kdevops@lists.linux.dev
 help / color / mirror / Atom feed
From: Luis Chamberlain <mcgrof@kernel.org>
To: Chuck Lever <cel@kernel.org>, Daniel Gomez <da.gomez@kruces.com>,
	kdevops@lists.linux.dev
Cc: Chuck Lever <chuck.lever@oracle.com>,
	Luis Chamberlain <mcgrof@kernel.org>
Subject: [PATCH v2 22/33] bootlinux: Harden update-grub/install.yml
Date: Sun, 27 Jul 2025 18:14:22 -0700	[thread overview]
Message-ID: <20250728011434.3197091-23-mcgrof@kernel.org> (raw)
In-Reply-To: <20250728011434.3197091-1-mcgrof@kernel.org>

From: Chuck Lever <chuck.lever@oracle.com>

After the kernel_release_file variable is set, ensure that
subsequent tasks check whether the release file exists so
they don't crap out.

Address some ansible-lint complaints:
 - fqcn[action-core]: Use FQCN for builtin module actions (set_fact).
 - fqcn[action-core]: Use FQCN for builtin module actions (stat).
 - name[template]: Jinja templates should only be at the end of 'name'
 - command-instead-of-shell: Use shell only when shell functionality is required.
 - fqcn[action-core]: Use FQCN for builtin module actions (shell).
 - no-changed-when: Commands should not change things if nothing needs doing.
 - fqcn[action-core]: Use FQCN for builtin module actions (set_fact).
 - fqcn[action-core]: Use FQCN for builtin module actions (set_fact).

To address "command-instead-of-shell", replace

  shell: cat {{ kernelrelease_file }}"

with the Ansible slurp module to pull the kernel release name
directly into a simple string variable.

Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
---
 .../bootlinux/tasks/update-grub/install.yml   | 83 ++++++++++++++-----
 1 file changed, 64 insertions(+), 19 deletions(-)

diff --git a/playbooks/roles/bootlinux/tasks/update-grub/install.yml b/playbooks/roles/bootlinux/tasks/update-grub/install.yml
index 8402c09b..a10064ee 100644
--- a/playbooks/roles/bootlinux/tasks/update-grub/install.yml
+++ b/playbooks/roles/bootlinux/tasks/update-grub/install.yml
@@ -93,39 +93,76 @@
   tags: [ 'uninstall-linux', 'manual-update-grub' ]
   import_tasks: update-grub/main.yml
 
-- name: Set file used to extract KERNELRELEASE variable
-  set_fact:
+- name: Set the pathname used to extract the kernel release
+  tags:
+    - vars
+  ansible.builtin.set_fact:
     kernelrelease_file: "{{ target_linux_dir_path }}/include/config/kernel.release"
-  tags: vars
 
-- name: Check if {{ kernelrelease_file }} exists
-  stat:
+- name: Stat {{ kernelrelease_file }}
+  tags:
+    - vars
+  ansible.builtin.stat:
+    get_checksum: false
+    get_mime: false
     path: "{{ kernelrelease_file }}"
   register: kernel_release_file
-  tags: vars
 
-- name: Get Linux build KERNELRELEASE varible which is set in include/config/kernel.release
-  shell: cat {{ kernelrelease_file }}
-  register: kernelrelease
-  when: kernel_release_file.stat.exists
+- name: Slurp {{ kernelrelease_file }}
+  tags:
+    - vars
+  ansible.builtin.slurp:
+    src: "{{ kernelrelease_file }}"
+  register: slurped_kernel_release
+  when:
+    - kernel_release_file.stat.exists
+
+- name: Set default kernelrelease if not determined
+  ansible.builtin.set_fact:
+    kernelrelease: "unknown"
+  when:
+    - kernelrelease is not defined
 
-- name: Construct command line to determine default kernel ID
-  set_fact:
+- name: Get the kernel release of the kernel to be installed
+  tags:
+    - vars
+  ansible.builtin.set_fact:
+    kernelrelease: "{{ slurped_kernel_release.content | b64decode | trim }}"
+  when:
+    - kernel_release_file.stat.exists
+
+- name: Construct the command line to determine the default boot entry
+  tags:
+    - saved
+  ansible.builtin.set_fact:
     determine_default_kernel_id: >-
       awk -F\' '/menuentry / {print $2}'
       /boot/grub/grub.cfg | awk '{print NR-1" ... "$0}' |
-      grep {{ kernelrelease.stdout }} | head -1 | awk '{print $1}'
+      grep {{ kernelrelease }} | head -1 | awk '{print $1}'
   when:
-    ansible_facts['os_family']|lower != 'redhat' or ansible_facts['distribution_major_version'] | int < 8
+    - kernel_release_file is defined
+    - kernel_release_file.stat is defined
+    - kernel_release_file.stat.exists
+    - kernelrelease is defined
+    - kernelrelease != "unknown"
+    - ansible_os_family != "RedHat" or ansible_distribution_major_version | int < 8
 
-- name: Construct command line to determine default kernel ID for RHEL >= 8
-  set_fact:
+- name: Construct the command line to determine default boot entry for RHEL >= 8
+  tags:
+    - saved
+  ansible.builtin.set_fact:
     determine_default_kernel_id: >-
       for f in $(ls -1 /boot/loader/entries/*.conf); do
       cat $f;
-      done | grep title | awk '{ gsub("title ", "", $0); print }' | grep '{{ kernelrelease.stdout }}';
+      done | grep title | awk '{ gsub("title ", "", $0); print }' | grep '{{ kernelrelease }}';
   when:
-    ansible_facts['os_family']|lower == 'redhat' and ansible_facts['distribution_major_version'] | int >= 8
+    - kernel_release_file is defined
+    - kernel_release_file.stat is defined
+    - kernel_release_file.stat.exists
+    - kernelrelease is defined
+    - kernelrelease != "unknown"
+    - ansible_os_family == "RedHat"
+    - ansible_distribution_major_version | int >= 8
 
 # If this fails then grub-set-default won't be run, and the assumption here
 # is either you do the work to enhance the heuristic or live happy with the
@@ -143,6 +180,8 @@
   register: grub_boot_number_cmd
   changed_when: false
   when:
+    - kernel_release_file is defined
+    - kernel_release_file.stat is defined
     - kernel_release_file.stat.exists
 
 - name: Obtain command to set default kernel to boot
@@ -163,10 +202,13 @@
   become_method: sudo
   command: "{{ grub_set_default_boot_kernel }} \"{{ target_boot_entry }}\""
   vars:
-    target_boot_entry: "{{ grub_boot_number_cmd.stdout_lines.0 }}"
+    target_boot_entry: "{{ grub_boot_number_cmd.stdout_lines.0 if (grub_boot_number_cmd is defined and grub_boot_number_cmd.stdout_lines is defined) else '' }}"
   tags: [ 'saved' ]
   when:
+    - grub_boot_number_cmd is defined
+    - grub_boot_number_cmd.rc is defined
     - grub_boot_number_cmd.rc == 0
+    - grub_boot_number_cmd.stdout is defined
     - grub_boot_number_cmd.stdout != ""
 
 - name: Itemize kernel and GRUB entry we just selected
@@ -177,6 +219,9 @@
     target_boot_entry: "{{ grub_boot_number_cmd.stdout_lines.0 }}"
   tags: [ 'saved' ]
   when:
+    - grub_boot_number_cmd is defined
+    - grub_boot_number_cmd.rc is defined
     - grub_boot_number_cmd.rc == 0
+    - grub_boot_number_cmd.stdout is defined
     - grub_boot_number_cmd.stdout != ""
 
-- 
2.47.2


  parent reply	other threads:[~2025-07-28  1:14 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-28  1:14 [PATCH v2 00/33] remove vagrant and bootlinux shape up Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 01/33] vagrant: remove entire vagrant configuration directory Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 02/33] kconfigs: fix Kconfig references after vagrant removal Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 03/33] scripts: remove Vagrant-specific scripts and Makefiles Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 04/33] playbooks: remove Vagrant-specific playbooks and roles Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 05/33] gitignore: remove Vagrant-specific ignore patterns Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 06/33] docs: remove Vagrant-specific documentation files Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 07/33] Remove all remaining Vagrant references from codebase Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 08/33] terraform: Clean up the destroy tasks Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 09/33] Switch to the cloud.terraform.terraform module Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 10/33] terraform: Make use of the new "terraform_output" module Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 11/33] terraform: Move "wait_for_connection" out of the terraform playbook Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 12/33] terraform: Remove "delegate_to: localhost" Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 13/33] terraform: Replace scripts/status_terraform.sh Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 14/33] Kconfig: Convert the 9p option to a choice menu Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 15/33] bootlinux: fix making 9p default if using libvirt Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 16/33] bootlinux: Relocate tasks that select a kernel .config Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 17/33] bootlinux: Simplify tasks that select the kernel .config to build Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 18/33] bootlinux: Select the kernel .config earlier Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 19/33] bootlinux: Move 9p build tasks to a subrole Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 20/33] bootlinux: Move tasks for building on target nodes " Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 21/33] bootlinux: Clean up a grub set-up task Luis Chamberlain
2025-07-28  1:14 ` Luis Chamberlain [this message]
2025-07-28  1:14 ` [PATCH v2 23/33] Add a guest/instance for building the test kernel Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 24/33] bootlinux: Add a new builder choice Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 25/33] workflows: Add a kconfig setting for installing kernels via package Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 26/33] bootlinux: Enclose tasks to find kernel release name in a block: Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 27/33] bootlinux: Pick up kernel release info for pre-built packages Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 28/33] bootlinux: Install pre-built kernels from packages Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 29/33] bootlinux: Add an option to build with clang instead of gcc Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 30/33] Makefile: add make style for style checking Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 31/33] CLAUDE.md: new workflow guide for hosts and nodes Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 32/33] CLAUDE.md: add don't BS rules Luis Chamberlain
2025-07-28  1:14 ` [PATCH v2 33/33] gen_nodes/gen_hosts: avoid usage of fs_config_path on task names Luis Chamberlain
2025-07-29 20:07 ` [PATCH v2 00/33] remove vagrant and bootlinux shape up Luis Chamberlain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250728011434.3197091-23-mcgrof@kernel.org \
    --to=mcgrof@kernel.org \
    --cc=cel@kernel.org \
    --cc=chuck.lever@oracle.com \
    --cc=da.gomez@kruces.com \
    --cc=kdevops@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox