198 lines
6.3 KiB
YAML
198 lines
6.3 KiB
YAML
---
|
|
# Ansible Playbook: 修复 warden 节点的 zsh 配置
|
|
- name: Fix zsh configuration on warden node
|
|
hosts: warden
|
|
become: yes
|
|
vars:
|
|
target_user: ben # 或者你想修复的用户名
|
|
|
|
tasks:
|
|
- name: 检查当前 shell
|
|
shell: echo $SHELL
|
|
register: current_shell
|
|
changed_when: false
|
|
|
|
- name: 显示当前 shell
|
|
debug:
|
|
msg: "当前 shell: {{ current_shell.stdout }}"
|
|
|
|
- name: 检查 zsh 是否已安装
|
|
package:
|
|
name: zsh
|
|
state: present
|
|
|
|
- name: 备份现有的 zsh 配置文件
|
|
shell: |
|
|
if [ -f ~/.zshrc ]; then
|
|
cp ~/.zshrc ~/.zshrc.backup.$(date +%Y%m%d_%H%M%S)
|
|
echo "已备份 ~/.zshrc"
|
|
fi
|
|
if [ -f ~/.zsh_history ]; then
|
|
cp ~/.zsh_history ~/.zsh_history.backup.$(date +%Y%m%d_%H%M%S)
|
|
echo "已备份 ~/.zsh_history"
|
|
fi
|
|
register: backup_result
|
|
changed_when: backup_result.stdout != ""
|
|
|
|
- name: 显示备份结果
|
|
debug:
|
|
msg: "{{ backup_result.stdout_lines }}"
|
|
when: backup_result.stdout != ""
|
|
|
|
- name: 检查 oh-my-zsh 是否存在
|
|
stat:
|
|
path: ~/.oh-my-zsh
|
|
register: ohmyzsh_exists
|
|
|
|
- name: 重新安装 oh-my-zsh (如果损坏)
|
|
shell: |
|
|
if [ -d ~/.oh-my-zsh ]; then
|
|
rm -rf ~/.oh-my-zsh
|
|
fi
|
|
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
|
|
when: not ohmyzsh_exists.stat.exists or ansible_check_mode == false
|
|
|
|
- name: 创建基本的 .zshrc 配置
|
|
copy:
|
|
content: |
|
|
# Path to your oh-my-zsh installation.
|
|
export ZSH="$HOME/.oh-my-zsh"
|
|
|
|
# Set name of the theme to load
|
|
ZSH_THEME="robbyrussell"
|
|
|
|
# Which plugins would you like to load?
|
|
plugins=(git docker docker-compose kubectl)
|
|
|
|
source $ZSH/oh-my-zsh.sh
|
|
|
|
# User configuration
|
|
export PATH=$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
|
|
|
|
# Aliases
|
|
alias ll='ls -alF'
|
|
alias la='ls -A'
|
|
alias l='ls -CF'
|
|
alias ..='cd ..'
|
|
alias ...='cd ../..'
|
|
|
|
# Nomad/Consul aliases
|
|
alias nomad-status='nomad status'
|
|
alias consul-members='consul members'
|
|
|
|
# History settings
|
|
HISTSIZE=10000
|
|
SAVEHIST=10000
|
|
setopt HIST_IGNORE_DUPS
|
|
setopt HIST_IGNORE_SPACE
|
|
setopt HIST_VERIFY
|
|
setopt SHARE_HISTORY
|
|
dest: ~/.zshrc
|
|
owner: "{{ target_user }}"
|
|
group: "{{ target_user }}"
|
|
mode: '0644'
|
|
backup: yes
|
|
|
|
- name: 设置 zsh 为默认 shell
|
|
user:
|
|
name: "{{ target_user }}"
|
|
shell: /usr/bin/zsh
|
|
|
|
- name: 检查 zsh 配置语法
|
|
shell: zsh -n ~/.zshrc
|
|
register: zsh_syntax_check
|
|
failed_when: zsh_syntax_check.rc != 0
|
|
changed_when: false
|
|
|
|
- name: 测试 zsh 启动
|
|
shell: zsh -c "echo 'zsh 配置测试成功'"
|
|
register: zsh_test
|
|
changed_when: false
|
|
|
|
- name: 显示修复结果
|
|
debug:
|
|
msg:
|
|
- "zsh 配置修复完成"
|
|
- "语法检查: {{ 'PASS' if zsh_syntax_check.rc == 0 else 'FAIL' }}"
|
|
- "启动测试: {{ zsh_test.stdout }}"
|
|
|
|
- name: 清理损坏的历史文件
|
|
shell: |
|
|
if [ -f ~/.zsh_history ]; then
|
|
# 尝试修复历史文件
|
|
strings ~/.zsh_history > ~/.zsh_history.clean
|
|
mv ~/.zsh_history.clean ~/.zsh_history
|
|
echo "已清理 zsh 历史文件"
|
|
fi
|
|
register: history_cleanup
|
|
changed_when: history_cleanup.stdout != ""
|
|
|
|
- name: 修复 DNS 配置问题
|
|
shell: |
|
|
# 备份现有DNS配置
|
|
sudo cp /etc/resolv.conf /etc/resolv.conf.backup.$(date +%Y%m%d_%H%M%S)
|
|
|
|
# 添加备用DNS服务器
|
|
echo "# 备用DNS服务器配置" | sudo tee -a /etc/resolv.conf
|
|
echo "nameserver 8.8.8.8" | sudo tee -a /etc/resolv.conf
|
|
echo "nameserver 8.8.4.4" | sudo tee -a /etc/resolv.conf
|
|
echo "nameserver 1.1.1.1" | sudo tee -a /etc/resolv.conf
|
|
|
|
echo "已添加备用DNS服务器"
|
|
register: dns_fix
|
|
changed_when: dns_fix.stdout != ""
|
|
|
|
- name: 测试 DNS 修复
|
|
shell: nslookup github.com
|
|
register: dns_test
|
|
changed_when: false
|
|
|
|
- name: 显示 DNS 测试结果
|
|
debug:
|
|
msg: "{{ dns_test.stdout_lines }}"
|
|
|
|
- name: 修复 zsh completion 权限问题
|
|
shell: |
|
|
# 修复系统 completion 目录权限
|
|
sudo chown -R root:root /usr/share/zsh/vendor-completions/ 2>/dev/null || true
|
|
sudo chown -R root:root /usr/share/bash-completion/ 2>/dev/null || true
|
|
sudo chown -R root:root /usr/share/fish/vendor_completions.d/ 2>/dev/null || true
|
|
sudo chown -R root:root /usr/local/share/zsh/site-functions/ 2>/dev/null || true
|
|
|
|
# 设置正确的权限
|
|
sudo chmod -R 755 /usr/share/zsh/vendor-completions/ 2>/dev/null || true
|
|
sudo chmod -R 755 /usr/share/bash-completion/ 2>/dev/null || true
|
|
sudo chmod -R 755 /usr/share/fish/vendor_completions.d/ 2>/dev/null || true
|
|
sudo chmod -R 755 /usr/local/share/zsh/site-functions/ 2>/dev/null || true
|
|
|
|
# 修复 oh-my-zsh completion 目录权限(如果存在)
|
|
if [ -d ~/.oh-my-zsh ]; then
|
|
chmod -R 755 ~/.oh-my-zsh/completions
|
|
chmod -R 755 ~/.oh-my-zsh/plugins
|
|
chmod -R 755 ~/.oh-my-zsh/lib
|
|
echo "已修复 oh-my-zsh 目录权限"
|
|
fi
|
|
|
|
# 重新生成 completion 缓存
|
|
rm -f ~/.zcompdump* 2>/dev/null || true
|
|
echo "已修复系统 completion 目录权限并清理缓存"
|
|
register: completion_fix
|
|
changed_when: completion_fix.stdout != ""
|
|
|
|
- name: 显示 completion 修复结果
|
|
debug:
|
|
msg: "{{ completion_fix.stdout_lines }}"
|
|
when: completion_fix.stdout != ""
|
|
|
|
- name: 测试 zsh completion 修复
|
|
shell: zsh -c "autoload -U compinit && compinit -D && echo 'completion 系统修复成功'"
|
|
register: completion_test
|
|
changed_when: false
|
|
|
|
- name: 重新加载 zsh 配置提示
|
|
debug:
|
|
msg:
|
|
- "修复完成!请执行以下命令重新加载配置:"
|
|
- "source ~/.zshrc"
|
|
- "或者重新登录以使用新的 shell 配置"
|
|
- "completion 权限问题已修复" |