剧本要会"看情况办事"。本文讲条件、循环、标签三大流程控制,结尾附批量用户创建小案例。
一、when 条件
tasks:
- name: 只在 CentOS 装 yum 包
yum: name=nginx state=present
when: ansible_distribution == "CentOS"
- name: Debian 系用 apt
apt: name=nginx state=present
when: ansible_distribution in ["Ubuntu", "Debian"]
- name: 多条件(and)
debug: msg="生产环境的 web"
when: deploy_env == "production" and inventory_hostname in groups["web"]
- name: 判断变量已定义
debug: msg="{{ db_host }}"
when: db_host is defined
- name: 用 register 结果判断
shell: ls /opt/app
register: app_check
ignore_errors: yes
failed_when: false # 永不判失败
- debug: msg="已部署"
when: app_check.rc == 0
二、loop 循环
tasks:
- name: 批量装包
yum:
name: "{{ item }}"
state: present
loop:
- nginx
- git
- vim
- name: 字典循环
user:
name: "{{ item.name }}"
groups: "{{ item.group }}"
loop:
- { name: tom, group: dev }
- { name: jerry, group: ops }
- name: 带索引循环
debug: msg="{{ item.idx }}: {{ item.val }}"
loop: [a, b, c]
loop_control:
index_var: idx
- name: 循环+条件
debug: msg="{{ item }}"
loop: [1, 2, 3, 4]
when: item | int > 2
| 写法 | 特点 |
|---|---|
| loop | 现代写法(推荐) |
| with_items | 旧写法(兼容遗留) |
| loop_control.label | 输出只显示关键字段(刷屏救星) |
| loop_control.pause | 每轮间隔秒数 |
- name: 长对象循环输出精简
debug: msg="{{ item }}"
loop: "{{ big_objects }}"
loop_control:
label: "{{ item.name }}" # 输出只显示 name
三、tags 标签
tasks:
- name: 安装依赖
yum: name=nginx state=present
tags: [install, nginx]
- name: 推配置
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
tags: [config]
- name: 重启服务
systemd: name=nginx state=restarted
tags: [service]
ansible-playbook site.yml --tags config # 只跑 config
ansible-playbook site.yml --skip-tags install # 跳过 install
ansible-playbook site.yml --list-tags # 看有哪些 tag
ansible-playbook site.yml --list-tasks # 看任务清单
四、错误控制
- name: 允许失败
shell: /opt/check.sh
ignore_errors: yes # 失败不中断
- name: 自定义失败条件
shell: /opt/check.sh
register: check
failed_when: "'ERROR' in check.stdout" # 出现 ERROR 才算失败
- name: changed 判定(自定义)
shell: /opt/reload.sh
changed_when: false # 永远不算 changed(只是查询类)
五、注意
- when 里写 Jinja 表达式不加 {{ }}(when 本身就是模板);
- loop 的 item 是保留变量,嵌套循环用 loop_control.loop_var 改名;
- ignore_errors 别滥用(失败应该停下来);
- tag 打在 role 调用上可整组标记。
六、小案例:批量创建用户
# create_users.yml
- name: 批量用户管理
hosts: all
become: yes
vars:
users:
- { name: tom, groups: [dev, docker], shell: /bin/bash, key: "ssh-ed25519 AAAA...tom" }
- { name: jerry, groups: [ops], shell: /bin/bash, key: "ssh-ed25519 AAAA...jerry" }
- { name: svc, groups: [app], shell: /sbin/nologin }
tasks:
- name: 创建用户
user:
name: "{{ item.name }}"
groups: "{{ item.groups | join(',') }}"
shell: "{{ item.shell }}"
create_home: yes
state: present
loop: "{{ users }}"
loop_control:
label: "{{ item.name }}"
tags: [users]
- name: 推送公钥
authorized_key:
user: "{{ item.name }}"
key: "{{ item.key }}"
loop: "{{ users }}"
when: item.key is defined # 没有 key 的跳过
loop_control:
label: "{{ item.name }}"
tags: [users, keys]
- name: 校验结果
command: "id {{ item.name }}"
loop: "{{ users }}"
register: id_check
changed_when: false # 纯查询不算变更
loop_control:
label: "{{ item.name }}"
tags: [verify]
ansible-playbook create_users.yml --tags users # 只建用户
ansible-playbook create_users.yml --tags keys --check # 演练推钥
字典循环 + label 精简输出 + when 条件跳过 + changed_when 纯查询 + tag 分段 —— 流程控制全用上。
七、总结
when 是 Jinja 表达式(无花括号);loop 循环字典配 label;tags 分段执行(--tags/--skip-tags);failed_when/changed_when 精确控制成败语义。