如何使用 Ansible 的 flatpak 模块在类似 Debian 的系统中自动化安装系统级的 Zoom Flatpak。

Ansible 模块 flatpak

community.general.flatpak 管理 Flatpak 应用程序

让我们来谈谈 Ansible 的 flatpak 模块。它的全名是 community.general.flatpak,属于由 Ansible 社区维护的 community.general 模块集合。该模块的目的是在目标系统中管理 Flatpak 应用程序。

参数说明

  • name(字符串):Flatpak 的名称,用于指定要安装或卸载的应用程序。
  • state(字符串):present/absent,指定要执行的操作(安装或卸载)。
  • method(字符串):system/user,指定安装方式(系统级或用户级,默认为系统级)。
  • remote(字符串):指定 Flatpak 的远程仓库,默认为 flathub。
  • no_dependencies(布尔值):是否跳过依赖项安装,默认为 no。
  • executable(字符串):指定 Flatpak 的可执行文件路径,默认为 flatpak。

示例代码

以下是一个完整的 Ansible Playbook,用于在类似 Debian 的系统中安装 Zoom Flatpak:

---
- name: flatpak module demo
  hosts: all
  become: true
  gather_facts: false
  tasks:
    - name: Ensure flatpak is installed
      ansible.builtin.apt:
        name: flatpak
        state: present

    - name: Add flathub remote repository
      community.general.flatpak_remote:
        name: flathub
        state: present
        flatpakrepo_url: "https://dl.flathub.org/repo/flathub.flatpakrepo"
        method: system

    - name: Install Zoom via flatpak
      community.general.flatpak:
        name: us.zoom.Zoom
        state: present
        method: system

执行过程

执行命令

ansible-pilot $ ansible-playbook -i virtualmachines/ubuntu2110desktop/inventory container/flatpak_debian.yml

输出结果

PLAY [flatpak module demo] *******************************************************
TASK [Ensure flatpak is installed] ************************************************
changed: [ubuntu2110.example.com]
TASK [Add flathub remote repository] **********************************************
changed: [ubuntu2110.example.com]
TASK [Install Zoom via flatpak] ***************************************************
changed: [ubuntu2110.example.com]
PLAY RECAP ***********************************************************************
ubuntu2110.example.com     : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

幂等性测试

再次运行 Playbook 时,不会重复执行已成功完成的任务:

ansible-pilot $ ansible-playbook -i virtualmachines/ubuntu2110desktop/inventory container/flatpak_debian.yml

输出结果:

PLAY [flatpak module demo] *******************************************************
TASK [Ensure flatpak is installed] ************************************************
ok: [ubuntu2110.example.com]
TASK [Add flathub remote repository] **********************************************
ok: [ubuntu2110.example.com]
TASK [Install Zoom via flatpak] ***************************************************
ok: [ubuntu2110.example.com]
PLAY RECAP ***********************************************************************
ubuntu2110.example.com     : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

执行前

在执行 Playbook 之前,目标主机上未安装 Flatpak 和 Zoom:

ansible-pilot $ ssh devops@ubuntu2110.example.com
Welcome to Ubuntu 21.10 (GNU/Linux 5.13.0-30-generic x86_64)
$ flatpak list
-sh: 1: flatpak: not found
$ flatpak remotes
-sh: 2: flatpak: not found
$ cat /etc/os-release
PRETTY_NAME="Ubuntu 21.10"
NAME="Ubuntu"
VERSION_ID="21.10"
VERSION="21.10 (Impish Indri)"
VERSION_CODENAME=impish
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=impish
$ exit
Connection to ubuntu2110.example.com closed.

执行后

执行 Playbook 后,Zoom 已通过 Flatpak 成功安装:

ansible-pilot $ ssh devops@ubuntu2110.example.com
Welcome to Ubuntu 21.10 (GNU/Linux 5.13.0-30-generic x86_64)
$ flatpak list
Name                   Application ID                    Version      Branch   Installation
Freedesktop Platform   org.freedesktop.Platform          21.08.10     21.08    system
Mesa                   org.freedesktop.Platform.GL.default   21.3.5       21.08    system
openh264               org.freedesktop.Platform.openh264 2.1.0        2.0      system
Zoom                   us.zoom.Zoom                      5.9.6.2225   stable   system
$ flatpak remotes
Name    Options
flathub system
$

通过以上步骤,你可以使用 Ansible 在类似 Debian 的系统中自动化安装 Zoom Flatpak,并确保其正常运行。