Ansible 基础操作指南(Windows 10 环境下)

一、Ansible 安装(Windows 10 + WSL2 + Ubuntu)

1. 安装 WSL2

  • 管理员身份打开 PowerShell,执行命令:

    powershell

    css 复制代码
    wsl --install
  • 安装完成后重启电脑

2. 启动并配置 WSL2(Ubuntu)

  • 从开始菜单打开 "Ubuntu",首次启动需设置用户名和密码

3. 在 WSL2 中安装 Ansible

依次执行以下命令:

sql 复制代码
sudo apt update
sudo apt install python3-pip -y
sudo apt install ansible-core

二、Ansible 基础配置

1. 创建工作目录与配置文件

创建工作目录

bash 复制代码
mkdir ansible-upload && cd ansible-upload

创建主机清单(inventory.ini

ini 复制代码
[linux_servers]
your_server_ip ansible_user=your_ssh_username ansible_ssh_pass=your_ssh_password

(替换 your_server_ipyour_ssh_usernameyour_ssh_password 为实际值)

编写 Playbook(upload.yaml

yaml 复制代码
- name: 从 Windows 上传 1.txt 到 Linux 服务器
  hosts: linux_servers
  gather_facts: no
  become: yes  

  tasks:
    - name: 确保目标目录存在
      ansible.builtin.file:
        path: /www/wwwroot
        state: directory
        mode: '0755'
        owner: root
        group: root
    
    - name: 上传 1.txt 文件
      ansible.builtin.copy:
        src: /mnt/c/files/1.txt  # WSL2 访问 Windows 文件路径
        dest: /www/wwwroot/1.txt 
        mode: '0644'
        owner: root
        group: root

三、关键模块与命令说明(ansible.builtin

模块 / 命令 功能说明
ansible.builtin.file 管理文件 / 目录(创建、删除、权限、所有者等)
ansible.builtin.copy 本地文件复制到远程主机
ansible.builtin.fetch 远程文件下载到本地
ansible.builtin.template 用 Jinja2 模板生成远程文件(支持变量替换)
ansible.builtin.lineinfile 修改文件特定行(替换配置、添加行)
ansible.builtin.blockinfile 向文件插入 / 替换整块内容(带标记防重复)
ansible.builtin.unarchive 解压压缩包(本地传远程 / 远程直接解压)
ansible.builtin.archive 远程文件 / 目录打包压缩(支持 tar、zip)
ansible.builtin.command 远程执行命令(不经过 shell,不支持管道 / 变量)
ansible.builtin.shell 通过 shell 执行远程命令(支持管道、重定向)
ansible.builtin.script 本地脚本传远程并执行(自动处理权限)
ansible.builtin.service 管理系统服务(启动、停止、开机自启等)
ansible.builtin.package 通用包管理(适配 yum/apt,安装 / 卸载软件)
ansible.builtin.user 管理系统用户(创建、删除、密码、所属组)
ansible.builtin.group 管理用户组(创建、删除、属性修改)
ansible.builtin.stat 获取远程文件 / 目录信息(是否存在、权限、校验和)
ansible.builtin.find 远程查找文件(按名称、大小、时间筛选)
ansible.builtin.replace 批量替换文件字符串(支持正则)
ansible.builtin.get_url 从 URL 下载文件到远程主机(HTTP/HTTPS/FTP)
ansible.builtin.set_fact 定义自定义变量(Playbook 跨任务使用)

四、进阶场景示例

1. 多服务器差异化操作

yaml 复制代码
- name: 多服务器差异化上传
  hosts: linux_servers
  gather_facts: no
  become: yes

  tasks:
    - name: 101.37.28.194 创建 pay 目录
      ansible.builtin.file:
        path: /www/wwwroot/pay
        state: directory
        mode: 0755
        owner: root
        group: root
      when: inventory_hostname == '101.37.28.194'

    - name: 101.37.28.194 上传 cesi.zip
      ansible.builtin.copy:
        src: /mnt/d/tool/phpstudy_pro/www/project/python/ansible-upload/cesi.zip
        dest: /www/wwwroot/pay/cesi.zip
        mode: 0644
        owner: root
        group: root
      when: inventory_hostname == '101.37.28.194'

2. 上传并解压压缩包

yaml 复制代码
- name: 上传并解压
  hosts: linux_servers
  gather_facts: no
  become: yes

  tasks:
    - name: 101.37.28.194 创建 pay 目录
      ansible.builtin.file:
        path: /www/wwwroot/pay
        state: directory
        mode: 0755
        owner: root
        group: root
      when: inventory_hostname == '101.37.28.194'

    - name: 上传 cesi.zip 到 pay 目录
      ansible.builtin.copy:
        src: /mnt/d/tool/phpstudy_pro/www/project/python/ansible-upload/cesi.zip
        dest: /www/wwwroot/pay/cesi.zip
        mode: 0644
        owner: root
        group: root
      when: inventory_hostname == '101.37.28.194'

    - name: 解压 cesi.zip 到 pay 目录
      ansible.builtin.unarchive:
        src: /www/wwwroot/pay/cesi.zip
        dest: /www/wwwroot/pay/
        remote_src: yes
      when: inventory_hostname == '101.37.28.194'

3. 本地压缩 → 上传 → 解压 → 删除压缩包

yaml 复制代码
- name: 本地压缩并上传解压
  hosts: linux_servers
  gather_facts: no
  become: yes

  pre_tasks:
    - name: 本地压缩 opencart 为 opencart.zip
      delegate_to: localhost
      become: false
      ansible.builtin.archive:
        path: /mnt/d/tool/phpstudy_pro/www/project/opencart
        dest: /mnt/d/tool/phpstudy_pro/www/project/python/ansible-upload/opencart.zip
        format: zip

  tasks:
    - name: 101.37.28.194 创建 pay 目录
      ansible.builtin.file:
        path: /www/wwwroot/pay
        state: directory
        mode: 0755
        owner: root
        group: root
      when: inventory_hostname == '101.37.28.194'

    - name: 上传 opencart.zip 到 pay 目录
      ansible.builtin.copy:
        src: /mnt/d/tool/phpstudy_pro/www/project/python/ansible-upload/opencart.zip
        dest: /www/wwwroot/pay/opencart.zip
        mode: 0755
        owner: root
        group: root
      when: inventory_hostname == '101.37.28.194'

    - name: 解压 opencart.zip 到 pay 目录
      ansible.builtin.unarchive:
        src: /www/wwwroot/pay/opencart.zip
        dest: /www/wwwroot/pay/
        remote_src: yes
      when: inventory_hostname == '101.37.28.194'

    - name: 删除 opencart.zip 压缩包
      ansible.builtin.file:
        path: /www/wwwroot/pay/opencart.zip
        state: absent
      when: inventory_hostname == '101.37.28.194'

4. 基于主机变量的批量定制操作

  1. 创建 host_vars 目录 ,按服务器 IP 定义变量文件(如 101.37.28.40.yml101.37.28.194.yml)。

    • 101.37.28.40.yml

      yaml

      javascript 复制代码
      operation: upload_file
      src_file: /mnt/d/tool/phpstudy_pro/www/project/python/ansible-upload/测试.txt
      target_file: /www/wwwroot/测试.txt 
    • 101.37.28.194.yml

      yaml

      javascript 复制代码
      operation: upload_and_unzip
      src_folder: /mnt/d/tool/phpstudy_pro/www/project/opencart
      zip_name: opencart.zip
      target_dir: /www/wwwroot/pay
      src_file: /mnt/d/tool/phpstudy_pro/www/project/python/ansible-upload/测试.txt
      target_file: /www/wwwroot/pay/测试.txt 
  2. 编写 Playbook

yaml 复制代码
- name: 多主机定制操作
  hosts: linux_servers
  gather_facts: no
  become: yes

  pre_tasks:
    - name: 本地压缩 opencart(仅 101.37.28.194)
      delegate_to: localhost
      become: false
      ansible.builtin.archive:
        path: "{{ src_folder | default('') }}"
        dest: "/mnt/d/tool/phpstudy_pro/www/project/python/ansible-upload/{{ zip_name | default('') }}"
        format: zip
      when:
        - operation == 'upload_and_unzip'
        - inventory_hostname == '101.37.28.194'

  tasks:
    - name: 上传并解压 zip(仅 101.37.28.194)
      ansible.builtin.copy:
        src: "/mnt/d/tool/phpstudy_pro/www/project/python/ansible-upload/{{ zip_name }}"
        dest: "{{ target_dir }}/{{ zip_name }}"
        mode: 0755
        owner: root
        group: root
      when: operation == 'upload_and_unzip'

    - name: 解压 zip 到目标目录(仅 101.37.28.194)
      ansible.builtin.unarchive:
        src: "{{ target_dir }}/{{ zip_name }}"
        dest: "{{ target_dir }}/"
        remote_src: yes
      when: operation == 'upload_and_unzip'

    - name: 删除 zip 包(仅 101.37.28.194)
      ansible.builtin.file:
        path: "{{ target_dir }}/{{ zip_name }}"
        state: absent
      when: operation == 'upload_and_unzip'

    - name: 通用文件上传(任意主机)
      ansible.builtin.copy:
        src: "{{ src_file }}"
        dest: "{{ target_file }}"
        mode: 0644
        owner: root
        group: root
      when:
        - src_file is defined
        - target_file is defined 

五、关键注意事项

  1. SSH 认证问题

    • 若服务器禁用密码登录,需改用密钥认证,修改 inventory.ini

      ini

      ini 复制代码
      your_server_ip ansible_user=your_ssh_username ansible_ssh_private_key_file=/path/to/private_key
  2. 主机密钥检查

    • 永久禁用可修改 ~/.ansible.cfg

      ini

      ini 复制代码
      [defaults]
      host_key_checking = False
  3. 权限问题

    • 确保 become: yes 获得足够权限,避免因权限不足导致操作失败。
  4. 路径一致性

    • 本地文件路径需通过 WSL2 访问(如 /mnt/c/ 映射 Windows C 盘),确保路径拼写、大小写一致。

通过以上步骤,可在 Windows 10 环境下利用 Ansible 完成从基础文件上传到复杂多服务器、多场景的自动化操作。

相关推荐
devlei6 小时前
从源码泄露看AI Agent未来:深度对比Claude Code原生实现与OpenClaw开源方案
android·前端·后端
努力的小郑8 小时前
Canal 不难,难的是用好:从接入到治理
后端·mysql·性能优化
Victor3568 小时前
MongoDB(87)如何使用GridFS?
后端
Victor3569 小时前
MongoDB(88)如何进行数据迁移?
后端
小红的布丁9 小时前
单线程 Redis 的高性能之道
redis·后端
GetcharZp9 小时前
Go 语言只能写后端?这款 2D 游戏引擎刷新你的认知!
后端
宁瑶琴10 小时前
COBOL语言的云计算
开发语言·后端·golang
普通网友11 小时前
阿里云国际版服务器,真的是学生党的性价比之选吗?
后端·python·阿里云·flask·云计算
IT_陈寒11 小时前
Vue的这个响应式问题,坑了我整整两小时
前端·人工智能·后端
Soofjan12 小时前
Go 内存回收-GC 源码1-触发与阶段
后端