如何使用 Ansible 的 snap 模块在类似 Debian 的系统中自动化安装系统级的 Spotify Snap。
Ansible 模块 snap
community.general.snap 管理 Snap 应用程序
让我们来探讨 Ansible 的 snap 模块。它的全名是 community.general.snap,属于由 Ansible 社区维护的 community.general 模块集合。该模块的目的是在目标系统中管理 Snap 应用程序。
参数说明
- name(字符串):Snap 的名称,用于指定要安装或卸载的应用程序。
- state(字符串):present/absent,指定要执行的操作(安装或卸载)。
- channel(字符串):指定使用的通道,默认为
stable。 - classic(布尔值):是否以经典模式安装,允许 Snap 与系统具有与传统包相同的访问级别,默认为
no。
示例代码
以下是一个完整的 Ansible Playbook,用于在类似 Debian 的系统中安装 Spotify Snap:
---
- name: snap module demo
hosts: all
become: true
gather_facts: false
tasks:
- name: Ensure snapd is installed
ansible.builtin.apt:
name: snapd
state: present
- name: Install Spotify via snap
community.general.snap:
name: spotify
state: present
执行过程
执行命令
ansible-pilot $ ansible-playbook -i virtualmachines/ubuntu2110desktop/inventory container/snap_debian.yml
输出结果
PLAY [snap module demo] *******************************************************
TASK [Ensure snapd is installed] ***********************************************
ok: [ubuntu2110.example.com]
TASK [Install Spotify via snap] ***********************************************
changed: [ubuntu2110.example.com]
PLAY RECAP *********************************************************************
ubuntu2110.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
幂等性测试
再次运行 Playbook 时,不会重复执行已成功完成的任务:
ansible-pilot $ ansible-playbook -i virtualmachines/ubuntu2110desktop/inventory container/snap_debian.yml
输出结果:
PLAY [snap module demo] *******************************************************
TASK [Ensure snapd is installed] ***********************************************
ok: [ubuntu2110.example.com]
TASK [Install Spotify via snap] ***********************************************
ok: [ubuntu2110.example.com]
PLAY RECAP *********************************************************************
ubuntu2110.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
执行前
在执行 Playbook 之前,目标主机上未安装 Spotify Snap:
ansible-pilot $ ssh devops@ubuntu2110.example.com
Welcome to Ubuntu 21.10 (GNU/Linux 5.13.0-30-generic x86_64)
$ snap --version
snap 2.54.3.2
snapd 2.54.3.2
series 16
ubuntu 21.10
kernel 5.13.0-30-generic
$ snap list
Name Version Rev Tracking Publisher Notes
bare 1.0 5 latest/stable canonical✓ base
core 16-2.54.3 12725 latest/stable canonical✓ core
core20 20220215 1361 latest/stable canonical✓ base
firefox 97.0.1-1 1025 latest/stable mozilla✓ -
gnome-3-38-2004 0+git.1f9014a 99 latest/stable canonical✓ -
gtk-common-themes 0.1-59-g7bca6ae 1519 latest/stable canonical✓ -
snap-store 3.38.0-66-gbd5b8f7 558 latest/stable canonical✓ -
$
执行后
执行 Playbook 后,Spotify 已通过 Snap 成功安装:
ansible-pilot $ ssh devops@ubuntu2110.example.com
Welcome to Ubuntu 21.10 (GNU/Linux 5.13.0-30-generic x86_64)
$ snap list
Name Version Rev Tracking Publisher Notes
bare 1.0 5 latest/stable canonical✓ base
core 16-2.54.3 12725 latest/stable canonical✓ core
core18 20211215 2284 latest/stable canonical✓ base
core20 20220215 1361 latest/stable canonical✓ base
firefox 97.0.1-1 1025 latest/stable mozilla✓ -
gnome-3-28-1804 3.28.0-19-g98f9e67.98f9e67 161 latest/stable canonical✓ -
gnome-3-38-2004 0+git.1f9014a 99 latest/stable canonical✓ -
gtk-common-themes 0.1-59-g7bca6ae 1519 latest/stable canonical✓ -
snap-store 3.38.0-66-gbd5b8f7 558 latest/stable canonical✓ -
spotify 1.1.77.643.g3c4c6fc6 57 latest/stable spotify✓ -
$
通过以上步骤,你可以使用 Ansible 在类似 Debian 的系统中自动化安装 Spotify Snap,并确保其正常运行。