ansible role配apt源

  1. 准备自定义的 APT 源文件

首先,在你的 Ansible 控制机上,创建一个 files 目录来存放源文件。

创建 files 目录 (如果你的 Playbook 目录下没有的话):

mkdir -p files

2.创建并编辑 sources.list 文件:我们以配置阿里云的 Ubuntu 22.04 (focal) 源为例。你可以根据你的操作系统版本(如 Ubuntu 18.04 bionic,Debian 11 bullseye 等)去对应的镜像站获取配置。

vim files/sources.list

将以下阿里云的 Ubuntu 22.04 源配置粘贴到文件中:

deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse

deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse

deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse

deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

3.编写 Ansible Playbook

现在,创建一个 Playbook 文件,例如 configure_apt.yml。

vim configure_apt.yml

将以下内容粘贴到文件中

复制代码
- name: 配置 APT 源
  hosts: all
  become: yes

  tasks:
    # 任务1: 备份文件
    - name: 1. 备份当前的 sources.list 文件
      ansible.builtin.copy:
        src: /etc/apt/sources.list
        dest: /etc/apt/sources.list.bak_{{ ansible_date_time.date }}
        remote_src: yes
        force: no

    # 任务2: 部署新文件
    - name: 2. 部署新的 sources.list 文件
      ansible.builtin.copy:
        src: files/sources.list
        dest: /etc/apt/sources.list
        owner: root
        group: root
        mode: '0644'
      notify:
        - 更新 apt 缓存

  # handlers 部分 (请确保这一行是顶格的,没有任何前置空格)
  handlers:
    - name: 更新 apt 缓存
      ansible.builtin.apt:
        update_cache: yes
        cache_valid_time: 3600

Playbook 详解

hosts: group1: 指定 Playbook 的目标主机组。

任务 1: 备份当前的 sources.list 文件:

使用 command 模块执行 mv 命令,将原有的 sources.list 重命名为 .bak 备份文件。

args: creates: ... 确保了只有当备份文件不存在时,才会执行备份,保证了幂等性。

ignore_errors: yes 用于容错,如果服务器上本来就没有 sources.list 文件,则忽略 mv 命令的错误。

任务 2: 部署新的 source.list 文件:

使用 copy 模块将控制机上准备好的 files/sources.list 文件复制到远程服务器的 /etc/apt/ 目录下。

notify: - 更新 apt 缓存 表示只有当 copy 模块实际替换了文件时,才会触发名为 更新 apt 缓存 的 handler。

handlers:

name: 更新 apt 缓存: 定义了一个 handler。

ansible.builtin.apt: update_cache: yes: 使用 apt 模块来执行 apt-get update 操作,这比直接使用 command: apt-get update 更好,因为 apt 模块是幂等的,并且能更好地处理各种情况。

cache_valid_time: 3600: 这是一个性能优化。它告诉 Ansible,如果距离上次更新缓存不到 3600 秒,就不要再执行 update_cache,从而避免了不必要的网络开销和时间浪费。

4.运行Playbook

然后,在项目根目录下运行Playbook:

ansible-playbook configure_apt.yml

出现以下结果就是成功了:

Playbook 执行成功后,你可以登录到任意一台目标服务器,验证 APT 源是否已更新。

登录到目标服务器

ssh root@node1

查看文件内容是否已更新

cat /etc/apt/sources.list

或者执行 apt update 查看输出

apt update

你应该能看到 apt update 的输出中显示正在从 mirrors.aliyun.com 下载软件包列表,并且 sources.list 文件的内容也已更新。

这个 Playbook 同样是可复用和幂等的,非常适合用于批量配置 Debian/Ubuntu 服务器的软件源。

相关推荐
K_i1346 天前
Ansible实战:VMware下K8s自动化部署指南
kubernetes·自动化·ansible
许泽宇的技术分享6 天前
Ansible核心架构深度剖析:从源码看IT自动化的“简单“哲学
python·ansible·自动化运维·devops·it基础设施
荣光波比6 天前
Ansible(三)—— 使用Ansible自动化部署LNMP环境实战指南
运维·自动化·云计算·ansible
tt666qq7 天前
运维自动化之 Ansible 核心知识点总结
运维·自动化·ansible
C-200212 天前
初探 ansible 部署 devops 持续集成持续交付
ci/cd·ansible·devops
东窗西篱梦12 天前
Ansible自动化运维:从入门到实战,告别重复劳动!
运维·自动化·ansible
weixin_5078479514 天前
Ansible
ansible
小白不想白a14 天前
【ansible/K8s】K8s的自动化部署源码分享
kubernetes·自动化·ansible
三坛海会大神55515 天前
Ansible详解(一)Ansible简介和基础命令及操作
运维·ansible