playbook由ansible写成
文件写成xxx.yaml xxx.yml
1.一个play
- 任何一个playbook都会执行一个默认的任务
- 这个默认的任务:收集当前play对应的主机的相关信息,收集完才会进行操作
--- 表明这是playbook yaml语法
- hosts: testB 组名,别名
remote_user: root 使用哪个用户操作 对齐表示平级
tasks: 真正要执行的任务列表
- name: ping the host 每个任务列表都要用-开头 名字随便取
ping: 真正的操作 ping模块
- name: make a directory
file: file模块
path: /testdir/aplox
state: directory
playbook也具有幂等性
2.多个play
---
- hosts: testB
remote_user: root
tasks:
- name: ping the host
ping:
- name: make a directory
file:
path: /testdir/aplox
state: directory
- hosts:
testB
testA
remote_user: root
tasks:
- name: create user aplox
user:
name: aplox
- hosts:
testA,testB
remote_user: root
tasks:
- name: touch a file
file:
path: /mnt/rico
state: touch
检查写的是否正确
ansible-playbook --syntax-check 1.yaml
试演示 模拟
- 有的时候会有报错,因为就是模拟,不是真实环境 一般报错原因是后面的play会依赖前面play的结果 但是模拟不会产生真正的结果,所以会报错
ansible-playbook --check 1.yaml