本文将继续介绍ansible-playbook中roles的各种用法
组件 | 版本 |
---|---|
操作系统 | Ubuntu 22.04.4 LTS |
ansible | 2.17.6 |
.
├── deploy.hosts
├── deploy.yaml
└── roles
└── base
└── tasks
└── main.yaml
新增group_vars目录,并且新增文件
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
└── tasks
└── main.yaml
▶ cat group_vars/all.yaml
info: IT paiqiu
▶ cat roles/base/tasks/main.yaml
- name: first
debug:
msg: "hello world {{ info }}"
运行:
▶ ansible-playbook -i deploy.hosts deploy.yaml
...
TASK [base : first] **********************************************************************************************
ok: [10.22.11.166] => {
"msg": "hello world IT paiqiu"
}
...
可以覆盖全局变量
在role base
下面新增vars目录,并且新增main.yaml文件
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
├── tasks
│ └── main.yaml
└── vars
└── main.yaml
▶ cat roles/base/tasks/main.yaml
- name: first
debug:
msg: "hello world {{ info }}"
▶ cat roles/base/vars/main.yaml
info:
name: wilson
addr: cd
运行:
▶ ansible-playbook -i deploy.hosts deploy.yaml
...
TASK [base : first] **********************************************************************************************
ok: [10.22.11.166] => {
"msg": "hello world {'name': 'wilson', 'addr': 'cd'}"
}
...
相当于定义role级别的根目录,更好的管理需要传输的文件,在role base
下面创建目录files
,并且在files
下面放入需要传输的文件test.img
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
├── files
│ └── test.img
├── tasks
│ └── main.yaml
└── vars
└── main.yaml
传输test.img
至目标机器
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
└── tasks
└── main.yaml
0
test.img默认回去roles/base/files当中寻找,所以前面不需要添加路径
C++opy模块会自动检查待传输文件的md5,如果没有变化,那就不会再传输了
每次传输文件都会通过输入的变量重新渲染之后再传输,在role base
下面创建目录templates
,并且创建文件 test.conf
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
└── tasks
└── main.yaml
1
在模版文件中定义了两个变量,一个是手动设置的变量version,一个是ansible内建变量inventory_hostname
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
└── tasks
└── main.yaml
2
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
└── tasks
└── main.yaml
3
运行:
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
└── tasks
└── main.yaml
4
登录到目标机器查看:
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
└── tasks
└── main.yaml
5
2
是本次任务传入的变量,10.22.11.166
则是目标机器的ip地址
通常用于某些任务完成之后需要触发的动作,比如:发布完某个配置文件之后需要重启服务。在role base
下创建目录handlers
,并创建文件main.yaml
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
└── tasks
└── main.yaml
6
创建事件first handler
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
└── tasks
└── main.yaml
7
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
└── tasks
└── main.yaml
8
运行:
.
├── deploy.hosts
├── deploy.yaml
├── group_vars
│ └── all.yaml
└── roles
└── base
└── tasks
└── main.yaml
9
同样的,如果事件前驱没有发生,那事件通知也不会发生了,比如事件前驱是传输一个文件,如果文件没有变化,就不会再传输,那事件也不会再发生了
当前只有main.yaml
,在增加一个专门用于传输文件的任务files.yaml
▶ cat group_vars/all.yaml
info: IT paiqiu
0
▶ cat group_vars/all.yaml
info: IT paiqiu
1
运行:
▶ cat group_vars/all.yaml
info: IT paiqiu
2
如果文件过多,可以通过循环来处理:
▶ cat group_vars/all.yaml
info: IT paiqiu
3
shell
▶ cat group_vars/all.yaml
info: IT paiqiu
4
copy
copy模块会自动检查待传输文件的md5,如果没有变化,那就不会再传输了
▶ cat group_vars/all.yaml
info: IT paiqiu
5
lineinfile
通过正则匹配行之后修改当前行
▶ cat group_vars/all.yaml
info: IT paiqiu
6
blockinfile
1)在某一行下面增加新的内容,并且将增加的内容打上标记
▶ cat group_vars/all.yaml
info: IT paiqiu
7
运行之后查看结果
▶ cat group_vars/all.yaml
info: IT paiqiu
8
2)删除已标记的内容
▶ cat group_vars/all.yaml
info: IT paiqiu
9
添加内容块打上标记之后会让修改与删除都变得非常方便
▶ cat roles/base/tasks/main.yaml
- name: first
debug:
msg: "hello world {{ info }}"
0
file
▶ cat roles/base/tasks/main.yaml
- name: first
debug:
msg: "hello world {{ info }}"
1
with_items
with_items
给多个目录修改权限
▶ cat roles/base/tasks/main.yaml
- name: first
debug:
msg: "hello world {{ info }}"
2
loop
当然使把with_items
替换成 loop
也是可以的
▶ cat roles/base/tasks/main.yaml
- name: first
debug:
msg: "hello world {{ info }}"
2
with_nested
输出每一种组合,相当于笛卡尔积
▶ cat roles/base/tasks/main.yaml
- name: first
debug:
msg: "hello world {{ info }}"
4
输出结果:
▶ cat roles/base/tasks/main.yaml
- name: first
debug:
msg: "hello world {{ info }}"
5
with_file
逐行打印文件内容
▶ cat roles/base/tasks/main.yaml
- name: first
debug:
msg: "hello world {{ info }}"
6
使用roles去管理多设备的时候,编写脚本需要时刻注意幂等性,即每一次执行都要保证同样的结果
联系我,做深入的交流
至此,本文结束
在下才疏学浅,有撒汤漏水的,请各位不吝赐教...