ansible 配置阿里源 实例

目录结构

hosts
deployment
    - aliyuan.yml
roles
    - aliyuan
        - tasks
              -main.yml
        - templater
        - files
              -centos7_base_epel_aliyun_repo.sh
              -rockylinux9_base_epel_aliyun_repo.sh
              -ubuntu_source_list.sh

hosts

[aliyuan]
192.168.106.130 hostname=rocky90-106-130
[init]
192.168.106.130 hostname=rocky90-106-130

[all:vars]
ansible_ssh_user=root
ansible_ssh_pass=kc@123456
ansible_ssh_port=22

vim roles/aliyuan/tasks/main.yml

---
# centos 7 + epel
- name: Set centos7.x aliyuan
  copy: src=centos7_base_epel_aliyun_repo.sh dest=/tmp/ mode=0777
  when:
    - ansible_distribution == "CentOS"
    - ansible_distribution_major_version == "7"
- name: Run centos repo script
  shell: /bin/bash /tmp/centos7_base_epel_aliyun_repo.sh
  when:
    - ansible_distribution == "CentOS"
    - ansible_distribution_major_version == "7"

# rockylinux9.0 +epel
- name: Set rockylinux9.x aliyuan
  copy: src=rockylinux9_base_epel_aliyun_repo.sh dest=/tmp/ mode=0777
  when:
    - ansible_distribution == "Rocky"
    - ansible_distribution_major_version == "9"
- name: Run rockylinux repo script
  shell: /bin/bash /tmp/rockylinux9_base_epel_aliyun_repo.sh
  when:
    - ansible_distribution == "Rocky"
    - ansible_distribution_major_version == "9"
    
# ubuntu 16.04 + 18.04 + 20.04 + 22.04
- name: Set ubuntuXX.04 aliyuan
  copy: src=ubuntu_source_list.sh dest=/tmp/ mode=0777
  when:
    - ansible_distribution == "Ubuntu"
- name: Run ubuntu source list script
  shell: /bin/bash /tmp/ubuntu_source_list.sh
  when:
    - ansible_distribution == "Ubuntu"

roles/aliyuan/files/centos7_base_epel_aliyun_repo.sh

#!/bin/bash
#添加阿里源和epel源
yum install curl
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache fast

roles/aliyuan/files/rockylinux9_base_epel_aliyun_repo.sh

#!/bin/bash
#上海交大源
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
    -i.bak \
    /etc/yum.repos.d/[Rr]ocky*.repo
dnf install -y https://mirrors.aliyun.com/epel/epel-release-latest-9.noarch.rpm
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
# 生成缓存
dnf makecache

ubuntu_source_list.sh

#!/bin/bash
ubuntu_version_name=`lsb_release -c|awk '{print $NF}'`
echo """#ubuntu_aliyun
deb https://mirrors.aliyun.com/ubuntu/ ${ubuntu_version_name}           main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ ${ubuntu_version_name}-security  main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ ${ubuntu_version_name}-updates   main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ ${ubuntu_version_name}-backports main restricted universe multiverse
"""> /etc/apt/sources.list
apt update

执行 playbook 命令

ansible-playbook -i hosts deployment/aliyuan.yml

posted on 2025-10-28 11:16  luokeli  阅读(0)  评论(0)    收藏  举报

导航