如何在类 RedHat 工作站(如 RedHat Enterprise Linux、CentOS、CentOS Stream、Fedora 等)上安装最新版的 Google Chrome Stable?以下是一个完整的指南,涵盖从添加 GPG 密钥、设置 Google 仓库到安装软件的全过程。代码已在 RHEL 8.4 上测试通过。
使用 Ansible 在类 RedHat 系统中安装 Google Chrome
任务步骤
- 添加 Google Chrome 的 GPG 密钥 ⇒
ansible.builtin.rpm_key - 添加 Google Chrome 仓库 ⇒
ansible.builtin.yum_repository - 更新 yum 缓存并安装 Google Chrome ⇒
ansible.builtin.yum
详细步骤
- 下载仓库的 GPG 签名密钥
使用
ansible.builtin.rpm_key模块。此加密密钥用于验证软件包和仓库的真实性,确保软件与 Google 发布的版本一致。 - 将 Google Chrome 仓库添加到系统中
使用
ansible.builtin.yum_repository模块。这是额外的软件源,系统包管理器(如 yum/dnf)会从此处获取软件。 - 更新可用软件包的 yum 缓存,并安装 Google Chrome
使用
ansible.builtin.yum模块。
参数说明
rpm_keykey(字符串):仓库 GPG 签名密钥的 URL 或密钥 ID。state(字符串):present或absent,确保密钥存在于系统中。
yum_repositoryname(字符串):仓库名称。baseurl(字符串):仓库的 URL。gpgcheck(布尔值):是否启用 GPG 验证。gpgkey(字符串):GPG 验证的密钥 URL。
yumname(字符串):软件包名称(此处为 Google Chrome)。state(字符串):latest、present或absent,确保软件包安装到最新版本。update_cache(布尔值):是否更新缓存,确保下载最新版本的软件包。
示例代码
以下是一个 Ansible Playbook,用于在类 RedHat 系统中安装 Google Chrome。
文件名:install_chrome_redhat.yml
---
- name: 安装 Google Chrome
hosts: all
become: true
tasks:
- name: 添加 Yum 签名密钥
ansible.builtin.rpm_key:
key: "https://dl.google.com/linux/linux_signing_key.pub"
state: present
- name: 添加 Google Chrome 仓库
ansible.builtin.yum_repository:
name: google-chrome
description: "Google Chrome 仓库"
baseurl: "http://dl.google.com/linux/chrome/rpm/stable/x86_64"
enabled: true
gpgcheck: true
gpgkey: "https://dl.google.com/linux/linux_signing_key.pub"
- name: 安装 Google Chrome
ansible.builtin.yum:
name: "google-chrome-stable"
state: latest
update_cache: true
执行命令
$ ansible-playbook -i demo/inventory install_chrome_redhat.yml
执行结果
PLAY [安装 Google Chrome] *******************************************************
TASK [Gathering Facts] ******************************************************
ok: [demo.example.com]
TASK [添加 Yum 签名密钥] ******************************************************
changed: [demo.example.com]
TASK [添加 Google Chrome 仓库] ***********************************************
changed: [demo.example.com]
TASK [安装 Google Chrome] ****************************************************
changed: [demo.example.com]
PLAY RECAP *******************************************************************
demo.example.com : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
验证安装
$ ssh devops@demo.example.com
$ sudo su -
# ls -l /etc/yum.repos.d/
total 420
-rw-r--r--. 1 root root 205 Oct 12 14:50 google-chrome.repo
# cat /etc/yum.repos.d/google-chrome.repo
[google-chrome]
async = 1
baseurl = http://dl.google.com/linux/chrome/rpm/stable/x86_64
enabled = 1
gpgcheck = 1
gpgkey = https://dl.google.com/linux/linux_signing_key.pub
name = Google Chrome 仓库
# rpm -qa | grep google-chrome-stable
google-chrome-stable-94.0.4606.81-1.x86_64
# yum list installed google-chrome-stable
Updating Subscription Management repositories.
Installed Packages
google-chrome-stable.x86_64 94.0.4606.81-1 @google-chrome
通过以上步骤,您可以在类 RedHat 系统中成功安装 Google Chrome,并确保其来自官方的可信源。