Playbook 分析
---
- name: 确保 CUDA 和 MPI 环境变量正确设置并立即生效
hosts: pod2 # 指定目标主机组或具体主机名
become: yes # 使用特权提升(sudo),以root权限执行某些需要权限的任务
remote_user: canopy # 远程连接使用的用户名
vars: # 定义全局变量,用于Playbook中的参数化
ansible_user: test # 远程连接使用的用户名
ansible_password: "123" # 远程连接使用的密码(注意:建议使用Ansible Vault加密)
tasks: # 任务列表,定义要执行的具体操作
# 任务 1:确保 PATH 包含 CUDA bin 路径
- name: "确保 PATH 包含 CUDA bin 路径"
lineinfile:
path: /etc/profile # 目标文件路径
regexp: '^export PATH=/usr/local/cuda-12.2/bin.*' # 正则表达式匹配现有行
line: 'export PATH=/usr/local/cuda-12.2/bin${PATH:+:${PATH}}' # 要添加或确保存在的行内容
state: present # 确保该行存在
create: false # 不创建文件(因为 `/etc/profile` 应该已经存在)
register: path_result # 将任务结果注册到变量 `path_result`
# 任务 2:确保