Skip to content

Commit

Permalink
feat: add rhsm_enabled to enable or disable Red Hat
Browse files Browse the repository at this point in the history
Closes #977

without Red Hat Subscription Manager, you needs additionnal yum
repository.
Added variables
-  rhsm_enable
-  yum_repositories
-  rpm_gpg_keys

Signed-off-by: Fabien SEISEN <seisen@gmail.com>
  • Loading branch information
nesies committed Nov 21, 2024
1 parent fb37d5e commit 8189ef3
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ansible/roles/base/tasks/redhat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
msg: "OS: {{ ansible_distribution }} {{ ansible_distribution_version }}"

- name: Checking the Red Hat Subscription Manager status.
when: ansible_distribution == 'RedHat'
when:
- ansible_distribution == 'RedHat'
- rhsm_enabled
ansible.builtin.command:
cmd: subscription-manager status
register: result
Expand Down
4 changes: 3 additions & 1 deletion ansible/roles/configure/tasks/redhat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
- name: Disconnecting from Red Hat Subscription Manager.
community.general.redhat_subscription:
state: absent
when: ansible_distribution == 'RedHat'
when:
- ansible_distribution == 'RedHat'
- rhsm_enabled

# Tasks for configuring SSH for public key authentication.
- name: Configuring SSH for Public Key Authentication without cloud-init.
Expand Down
12 changes: 12 additions & 0 deletions builds/linux/rhel/8/data/ks.pkrtpl.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ timezone ${vm_guest_os_timezone}
### Partitioning
${storage}

### Additional yum repositories
%{ for repo in yum_repositories ~}
repo --name ${repo.name} --baseurl ${repo.url} %{ if repo.install }--install%{ endif }
%{ endfor ~}

### Modifies the default set of services that will run under the default runlevel.
services --enabled=NetworkManager,sshd

Expand All @@ -67,8 +72,15 @@ skipx

### Post-installation commands.
%post
%{ for gpg_key in rpm_gpg_keys ~}
rpm --import ${gpg_key}
%{ endfor ~}
%{ if rhsm_enabled ~}
/usr/sbin/subscription-manager register --username ${rhsm_username} --password ${rhsm_password} --autosubscribe --force
/usr/sbin/subscription-manager repos --enable "codeready-builder-for-rhel-8-x86_64-rpms"
%{ else ~}
dnf remove --assumeyes subscription-manager
%{ endif ~}
dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf makecache
dnf install -y sudo open-vm-tools perl
Expand Down
4 changes: 4 additions & 0 deletions builds/linux/rhel/8/linux-rhel.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ locals {
build_username = var.build_username
build_password = var.build_password
build_password_encrypted = var.build_password_encrypted
rhsm_enabled = var.rhsm_enabled
rhsm_username = var.rhsm_username
rhsm_password = var.rhsm_password
vm_guest_os_language = var.vm_guest_os_language
Expand All @@ -74,6 +75,8 @@ locals {
lvm = var.vm_disk_lvm
})
additional_packages = join(" ", var.additional_packages)
rpm_gpg_keys = var.rpm_gpg_keys
yum_repositories = var.yum_repositories
})
}
http_ks_command = "inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg"
Expand Down Expand Up @@ -231,6 +234,7 @@ build {
"--extra-vars", "ansible_username=${var.ansible_username}",
"--extra-vars", "ansible_key='${var.ansible_key}'",
"--extra-vars", "enable_cloudinit=${var.vm_guest_os_cloudinit}",
"--extra-vars", "{\"rhsm_enabled\": ${var.rhsm_enabled}}",
]
}

Expand Down
17 changes: 17 additions & 0 deletions builds/linux/rhel/8/linux-rhel.pkrvars.hcl.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,20 @@ vm_firmware = "efi-secure"
iso_datastore_path = "iso/linux/rhel"
iso_content_library_item = "rhel-8.9-x86_64-dvd"
iso_file = "rhel-8.9-x86_64-dvd.iso"

rpm_gpg_keys = [
"/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release"
]

yum_repositories = [
{
"name": "baseos",
"url": "http://url/path/to/repo",
"install": true
},
{
"name": "appstream",
"url": "http://url/path/to/repo",
"install": true
}
]
23 changes: 23 additions & 0 deletions builds/linux/rhel/8/variables.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

// Red Hat Subscription Manager Credentials

variable "rhsm_enabled" {
type = bool
description = "Enable Red Hat Subscription Manager."
}

variable "rhsm_username" {
type = string
description = "The username to Red Hat Subscription Manager."
Expand Down Expand Up @@ -459,3 +464,21 @@ variable "additional_packages" {
description = "Additional packages to install."
default = []
}

// Additional rpm gpg keys
variable "rpm_gpg_keys" {
type = list(string)
description = "Additional rpm gpg keys"
default = []
}

// Additional yum repositories
variable "yum_repositories" {
type = list(object({
name = string
url = string
install = bool
}))
description = "Additional yum repositories"
default = []
}
13 changes: 13 additions & 0 deletions builds/linux/rhel/9/data/ks.pkrtpl.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ timezone ${vm_guest_os_timezone}
### Partitioning
${storage}

### Additional yum repositories
%{ for repo in yum_repositories ~}
repo --name ${repo.name} --baseurl ${repo.url} %{ if repo.install }--install%{ endif }
%{ endfor ~}

### Modifies the default set of services that will run under the default runlevel.
services --enabled=NetworkManager,sshd

Expand All @@ -67,9 +72,17 @@ skipx

### Post-installation commands.
%post
%{ for gpg_key in rpm_gpg_keys ~}
rpm --import ${gpg_key}
%{ endfor ~}
%{ if rhsm_enabled ~}
/usr/sbin/subscription-manager register --username ${rhsm_username} --password ${rhsm_password} --autosubscribe --force
/usr/sbin/subscription-manager repos --enable "codeready-builder-for-rhel-9-x86_64-rpms"
%{ else ~}
dnf remove --assumeyes subscription-manager
%{ endif ~}
dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm

dnf makecache
dnf install -y sudo open-vm-tools perl
%{ if additional_packages != "" ~}
Expand Down
4 changes: 4 additions & 0 deletions builds/linux/rhel/9/linux-rhel.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ locals {
build_username = var.build_username
build_password = var.build_password
build_password_encrypted = var.build_password_encrypted
rhsm_enabled = var.rhsm_enabled
rhsm_username = var.rhsm_username
rhsm_password = var.rhsm_password
vm_guest_os_language = var.vm_guest_os_language
Expand All @@ -74,6 +75,8 @@ locals {
lvm = var.vm_disk_lvm
})
additional_packages = join(" ", var.additional_packages)
rpm_gpg_keys = var.rpm_gpg_keys
yum_repositories = var.yum_repositories
})
}
http_ks_command = "inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg"
Expand Down Expand Up @@ -231,6 +234,7 @@ build {
"--extra-vars", "ansible_username=${var.ansible_username}",
"--extra-vars", "ansible_key='${var.ansible_key}'",
"--extra-vars", "enable_cloudinit=${var.vm_guest_os_cloudinit}",
"--extra-vars", "{\"rhsm_enabled\": ${var.rhsm_enabled}}",
]
}

Expand Down
17 changes: 17 additions & 0 deletions builds/linux/rhel/9/linux-rhel.pkrvars.hcl.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,20 @@ vm_firmware = "efi-secure"
iso_datastore_path = "iso/linux/rhel"
iso_content_library_item = "rhel-9.4-x86_64-dvd"
iso_file = "rhel-9.4-x86_64-dvd.iso"

rpm_gpg_keys = [
"/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release"
]

yum_repositories = [
{
"name": "baseos",
"url": "http://url/path/to/repo",
"install": true
},
{
"name": "appstream",
"url": "http://url/path/to/repo",
"install": true
}
]
23 changes: 23 additions & 0 deletions builds/linux/rhel/9/variables.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

// Red Hat Subscription Manager Credentials

variable "rhsm_enabled" {
type = bool
description = "Enable Red Hat Subscription Manager."
}

variable "rhsm_username" {
type = string
description = "The username to Red Hat Subscription Manager."
Expand Down Expand Up @@ -459,3 +464,21 @@ variable "additional_packages" {
description = "Additional packages to install."
default = []
}

// Additional rpm gpg keys
variable "rpm_gpg_keys" {
type = list(string)
description = "Additional rpm gpg keys"
default = []
}

// Additional yum repositories
variable "yum_repositories" {
type = list(object({
name = string
url = string
install = bool
}))
description = "Additional yum repositories"
default = []
}
2 changes: 2 additions & 0 deletions builds/rhsm.pkrvars.hcl.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
*/

// Red Hat Subscription Manager Credentials

rhsm_enabled = true
rhsm_username = "packer"
rhsm_password = "VMw@re123!"
2 changes: 2 additions & 0 deletions docs/getting-started/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ additional_packages = ["git", "make", "vim"]
Edit the `config/redhat.pkrvars.hcl` file to configure the credentials for your Red Hat Subscription
Manager account.
You can also disable Red Hat Subscription Manger by setting `rhsm_enabled = false`.
```hcl linenums="1" title="config/rhsm.pkrvars.hcl" hl_lines="1"
--8<-- "./builds/rhsm.pkrvars.hcl.example:10:100"
```
Expand Down

0 comments on commit 8189ef3

Please sign in to comment.