linRichielinRichie
前端
Python
Linux
ChatGPT
  • B 站
  • 500px
前端
Python
Linux
ChatGPT
  • B 站
  • 500px
  • Ansible

    • Ansible: 基本操作
    • Ansible: 安装
    • Ansible: 简介
    • YAML: 文件格式
    • Playbook

      • Playbook: 介绍
      • Playbook: 操作
    • 模块

      • Yum模块常用参数
    • 实战

      • Ansible 实战
  • Anaconda

    • Anaconda命令
  • Iptables

    • Iptable: 防火墙
    • iptables 用法
  • Systemd

    • 系统服务配置
    • 系统服务启动文件
    • 性能优化

      • Linux 系统监控
      • Linux 系统性能优化
      • Linux 系统故障诊断
      • Linux 系统日志管理
  • Network

    • 用一张图解释 8 种流行的网络协议
    • 反子网掩码
    • 交换机端口模式
    • eNSP 软件
    • 华为交换机配置命令
    • eNSP静态路由实验
  • Commands

    • 命令别名:alias
    • 多类型资源统计工具: dstat
    • history配置
    • unzip命令
    • Linux用户到期登录时间和随机密码
    • 常用 Command
    • ssh

      • ssh-keygen
      • linux ssh命令
  • CI/CD

    • Jenkins CI/CD 管道
  • Kubernetes

    • Docker系列学习

      • 01. 什么是Docker
      • 02. Docker安装
      • 03. 使用Docker镜像
      • 04. 利用commit理解镜像构成
      • 05. 操作Docker容器
      • 06. 使用Dockerfile定制镜像
      • 07. Dockerfile指令详解
      • 08. Dockerfile多阶段创建
      • 09. 访问仓库
      • 10. 修改docker的启动项
      • 11. Nexus3.x的私有仓库
      • 12. docker-hub加速器
      • 13. 数据管理
      • 14. 使用网络
  • Shell编程

    • Shell 编程基础
    • Shell 脚本执行消耗的时间
    • Shell 自动生成简介

Playbook: 操作

  • 条件语句
    • 单一条件
    • 多重条件
    • 注册变量条件
    • 变量存在性检查
  • 循环语句
    • 基本循环
    • 字典循环
    • 文件匹配循环
    • 序列循环
  • 错误处理
    • 忽略错误
    • 条件判断
    • 块错误处理
  • 任务控制
    • 标签(Tags)
    • 任务委派
    • 任务超时
  • 最佳实践

条件语句

when 语句用于控制任务的执行条件。支持使用 Ansible facts、注册变量和自定义变量进行判断。

单一条件

使用单个条件判断:

tasks:
  - name: 安装 Apache
    yum: name=httpd state=present
    when: ansible_os_family == "RedHat"

多重条件

支持使用多个条件组合:

tasks:
  # 使用 and 条件(隐式)
  - name: 配置 CentOS 7 服务
    template: src=service.conf.j2 dest=/etc/service.conf
    when:
      - ansible_distribution == "CentOS"
      - ansible_distribution_major_version == "7"

  # 使用 or 条件
  - name: 安装 RHEL 系包
    yum: name=httpd state=present
    when: >
      ansible_distribution == "CentOS" or
      ansible_distribution == "RedHat"

  # 使用 not 条件
  - name: 非 Debian 系统跳过
    debug: msg="不是 Debian"
    when: not ansible_os_family == "Debian"

注册变量条件

tasks:
  - name: 检查服务状态
    command: systemctl status httpd
    register: service_status

  - name: 重启服务
    service: name=httpd state=restarted
    when:
      - service_status.rc != 0

变量存在性检查

# 设置变量
vars:
  db_password: "your_secure_password"

tasks:
  - name: 配置数据库              # 任务描述
    template:                     # 使用模板模块
      src: db.conf.j2            # 源模板文件
      dest: /etc/db.conf         # 目标配置文件
    when: db_password is defined  # 执行条件:db_password变量已定义

循环语句

基本循环

with_items 用于遍历列表项,常用于:

  • 批量安装软件包
  • 创建多个用户/目录
  • 复制多个文件
tasks:
  - name: 安装多个包
    yum: name={{ item }} state=present
    with_items:
      - httpd
      - php
      - mysql-server

字典循环

with_dict 用于遍历字典数据,可以:

  • 访问键值对
  • 处理嵌套数据
  • 配置复杂选项
tasks:
  - name: 创建用户
    user:
      name: "{{ item.key }}"
      groups: "{{ item.value.groups }}"
      shell: "{{ item.value.shell }}"
    with_dict:
      admin:
        groups: wheel
        shell: /bin/bash
      dev:
        groups: developers
        shell: /bin/zsh

文件匹配循环

with_fileglob 用于文件操作:

  • 匹配文件模式
  • 批量处理文件
  • 支持通配符
tasks:
  - name: 复制配置文件
    copy:
      src: "{{ item }}"
      dest: /etc/app/
    with_fileglob:
      - "files/*.conf"
      - "templates/*.j2"

序列循环

with_sequence 生成数字序列:

  • 指定起始值
  • 设置结束值
  • 定义步长
tasks:
  - name: 创建多个目录
    file:
      path: "/data/dir{{ item }}"
      state: directory
    with_sequence: start=1 end=5 stride=1

错误处理

忽略错误

tasks:
  - name: 尝试操作
    command: /bin/false
    ignore_errors: yes

条件判断

tasks:
  - name: 检查服务状态
    command: systemctl status httpd
    register: result
    failed_when: "'active' not in result.stdout"

块错误处理

tasks:
  - block:
      - name: 安装应用
        yum: name=app state=present
      - name: 启动服务
        service: name=app state=started
    rescue:
      - name: 清理安装
        yum: name=app state=absent
    always:
      - name: 通知管理员
        mail:
          to: admin@example.com
          subject: 部署状态

任务控制

标签(Tags)

tasks:
  - name: 安装包
    yum: name=httpd state=present
    tags: install

  - name: 配置服务
    template: src=httpd.conf.j2 dest=/etc/httpd.conf
    tags:
      - config
      - httpd

任务委派

tasks:
  - name: 添加到负载均衡
    shell: /usr/local/bin/add_to_lb.sh {{ inventory_hostname }}
    delegate_to: localhost

任务超时

tasks:
  - name: 长时间运行的任务
    command: /usr/bin/long_running_operation
    async: 3600  # 1小时超时
    poll: 0      # 立即返回

最佳实践

  1. 任务设计

    • 保持任务幂等性
    • 合理使用条件判断
    • 适当处理错误
  2. 代码组织

    • 使用有意义的标签
    • 合理分组任务
    • 注意执行顺序
  3. 错误处理

    • 使用 block/rescue
    • 设置合理超时
    • 记录错误信息

注意:

  • 避免过度复杂的条件
  • 谨慎使用 ignore_errors
  • 合理使用任务委派

参考资料:

  • Ansible 条件判断
  • Ansible 循环指南
  • Ansible 错误处理
最近更新时间:
Prev
Playbook: 介绍