Last active 1769237311

setup,yml Raw
1---
2- name: 个人开发环境一键配置
3 hosts: localhost
4 connection: local
5 become: true
6
7 vars:
8 go_version: "1.25.6"
9 user_home: "/home/{{ ansible_env.SUDO_USER }}"
10
11 tasks:
12 - name: 更新 apt 缓存
13 apt:
14 update_cache: yes
15
16 - name: 安装基础工具
17 apt:
18 name:
19 - curl
20 - git
21 - wget
22 - vim
23 - build-essential
24 - nano
25 state: present
26
27 - name: 下载 Go 语言包
28 get_url:
29 url: "https://golang.google.cn/dl/go{{ go_version }}.linux-amd64.tar.gz"
30 dest: "/tmp/go.tar.gz"
31
32 - name: 解压 Go 到 /usr/local
33 unarchive:
34 src: "/tmp/go.tar.gz"
35 dest: /usr/local
36 remote_src: yes
37 creates: "/usr/local/go/bin/go" # 如果存在就不重复解压
38
39 - name: 配置 GOPROXY (写入 .bashrc)
40 lineinfile:
41 path: "{{ user_home }}/.bashrc"
42 line: "{{ item }}"
43 create: yes
44 loop:
45 - 'export PATH=$PATH:/usr/local/go/bin'
46 - 'export GOPROXY=https://goproxy.cn,direct'
47
48 - name: 下载并运行 NodeSource 安装脚本 (安装 Node 20 LTS)
49 shell: curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
50 args:
51 creates: /etc/apt/sources.list.d/nodesource.list
52
53 - name: 安装 Node.js (包含 npm)
54 apt:
55 name: nodejs
56 state: present
57
58 # ---------------------------------------------------
59 # 5. Python 极速包管理器:UV (Rust编写)
60 # ---------------------------------------------------
61 - name: 安装 UV
62 shell: curl -LsSf https://astral.sh/uv/install.sh | sh
63 args:
64 creates: "{{ ansible_env.HOME }}/.cargo/bin/uv"
65 # UV 默认装在 ~/.cargo/bin 下,如果没有 rust 会装在 ~/.local/bin
66
67 - name: 配置 UV 国内镜像 (写入 .bashrc)
68 lineinfile:
69 path: "{{ ansible_env.HOME }}/.bashrc"
70 line: "{{ item }}"
71 create: yes
72 loop:
73 - '# UV 配置'
74 - 'export UV_PYPI_MIRROR=https://pypi.tuna.tsinghua.edu.cn/simple'
75 # 让 UV 安装 Python 版本时也走国内/加速镜像 (如果有的话,目前 UV 主要是 PyPI 镜像)
76
77 # ---------------------------------------------------
78 # 6. Mamba (通过 Miniforge 安装,代替 Anaconda)
79 # ---------------------------------------------------
80 - name: 下载 Miniforge 安装脚本
81 get_url:
82 url: "https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh"
83 dest: "/tmp/Miniforge3.sh"
84 mode: '0755'
85
86 - name: 自动安装 Miniforge (Mamba)
87 shell: /tmp/Miniforge3.sh -b -p "{{ ansible_env.HOME }}/miniforge3"
88 args:
89 creates: "{{ ansible_env.HOME }}/miniforge3"
90
91 - name: 初始化 Conda/Mamba Shell
92 shell: "{{ ansible_env.HOME }}/miniforge3/bin/conda init bash"
93 register: conda_init_result
94 # 只有当输出中包含 "modified" 字样时,Ansible 才认为系统被修改了
95 # 如果 Conda 说 "no change",Ansible 就会显示绿色 (OK)
96 changed_when: "'modified' in conda_init_result.stdout"
97
98 - name: 配置 Conda/Mamba 国内源 (.condarc)
99 copy:
100 dest: "{{ ansible_env.HOME }}/.condarc"
101 content: |
102 channels:
103 - defaults
104 show_channel_urls: true
105 default_channels:
106 - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
107 - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
108 - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
109 custom_channels:
110 conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
111 msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
112 bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
113 menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
114 pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
115 pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
116 simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
117
118 # ---------------------------------------------------
119 # 7. Pixi (下一代 Conda 项目管理工具)
120 # ---------------------------------------------------
121 - name: 安装 Pixi
122 shell: curl -fsSL https://pixi.sh/install.sh | bash
123 args:
124 creates: "{{ ansible_env.HOME }}/.pixi/bin/pixi"
125
126 - name: 创建 Pixi 全局配置目录
127 file:
128 path: "{{ ansible_env.HOME }}/.config/pixi"
129 state: directory
130
131 - name: 配置 Pixi 镜像 (config.toml)
132 copy:
133 dest: "{{ ansible_env.HOME }}/.config/pixi/config.toml"
134 content: |
135 # 强制 Pixi 使用国内 Conda Forge 镜像
136 default_channels = ["https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge"]
137
138 # ---------------------------------------------------
139 # 8. R 语言环境及全局换源
140 # ---------------------------------------------------
141 - name: 安装 R 基础包
142 apt:
143 name:
144 - r-base
145 - r-base-dev
146 state: present
147
148 - name: 配置 R 全局 CRAN 镜像 (清华源)
149 lineinfile:
150 path: /etc/R/Rprofile.site
151 line: 'local({r <- getOption("repos"); r["CRAN"] <- "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"; options(repos = r)})'
152 create: yes
153 state: present
154 # 注意:这步需要 sudo 权限,become: true 会处理