共计 3627 个字符,预计需要花费 10 分钟才能阅读完成。
一、playbook 介绍
playbook(剧本): 是 ansible 用于配置, 部署, 和管理被控节点的剧本。
参考:https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html
使用的格式为 yaml 格式(saltstack,elk,docker 等也都会用到 yaml 格式)
树明的理解:playbook:ansible 格式的脚本。将所有需要执行的操作按照 ansible 的编程语法,放到文件中执行。playbook 替代方案 | |
1)、完全可以用 shell 脚本来替代 playbook | |
将所有的 ansible 命令放入脚本 shell 脚本中写的是 ansible 指令 | |
#!/bin/bash | |
for IP in `seq 201 203` | |
do | |
ansible -m hostname 192.168.98.$IP -a "name=node${IP}" | |
done | |
2)、ansible+shell 脚本 使用 script 模块 | |
ansible -m script group2 '/etc/ansible/srcipts/nginx_install.sh' |
1.1、YAML 格式规则
- 文件的第一行以 “—” 开始,表明 YMAL 文件的开始.
- 以 #号开头为注释
- 列表中的所有成员都开始于相同的缩进级别, 并且使用一个
"-"
作为开头(一个横杠和一个空格) - 一个字典是由一个简单的
键: 值
的形式组成(这个冒号后面必须是一个空格) - 注意: 写这种文件不要使用 tab 键,都使用空格
参考: https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html#yaml-syntax
下面看一个官方的示例感受一下
# 一位职工记录 | |
name: Example Developer | |
job: Developer | |
skill: Elite | |
employed: True | |
foods: | |
- Apple | |
- Orange | |
- Strawberry | |
- Mango | |
languages: | |
ruby: Elite | |
python: Elite | |
dotnet: Lame |
playbook 实例
先直接来看一个实例 apache 安装及业务初始化
第 1 步: 创建一个存放 playbook 的目录(路径自定义)
[root@manage01 ~]# mkdir -p /etc/ansible/playbook/web
第 2 步: 准备 httpd 配置文件, 并修改成你想要的配置
[root@manage01 ~]# yum install httpd -y | |
按需要修改你想要的配置(为了测试可以随意改动标记一下) | |
[root@manage01 ~]# vim /etc/httpd/conf/httpd.conf |
第 3 步: 写一个 playbook 文件(后缀为.yml 或.yaml)
[root@manage01 web]# cat apache.yaml | |
- hosts: group1 | |
remote_user: root | |
vars: | |
- user: zutuanxue | |
tasks: | |
- name: create user use variable | |
user: user=zutuanxue state=present | |
- name: install httpd server | |
yum: name={{item}} state=latest | |
with_items: | |
- httpd | |
- httpd-devel | |
- name: start httpd service | |
service: name=httpd state=started enabled=yes | |
- name: copy httpd.conf to group1:/etc/httpd/conf/ | |
copy: src=/etc/ansible/playbook/web/httpd.conf dest=/etc/httpd/conf | |
notify: | |
- restart httpd service | |
handlers: | |
- name: restart httpd service | |
service: name=httpd state=restarted | |
#tasks | |
#1、创建 apache 管理用户 | |
#2、安装 httpd | |
#3、服务启动管理 | |
#4、拷贝配置文件,业务初始化 | |
#5、触发重启服务 httpd |
第 4 步: 执行写好的 palybook
- 会显示出执行的过程,并且执行的每一步都有 ok,changed,failed 等标识
- 执行如果有错误 (failed) 会回滚,解决问题后,直接再执行这条命令即可, 并会把 failed 改为 changed(幂等性)
[root@manage01 web]# ansible-playbook /etc/ansible/playbook/web/apache.yaml
1.2、Playbook 常见语法
hosts: 用于指定要执行任务的主机,其可以是一个或多个由冒号分隔主机组.
remote_user: 用于指定远程主机上的执行任务的用户.
- hosts: group1 | |
remote_user: root |
tasks: 任务列表, 按顺序执行任务.
- 如果一个 host 执行 task 失败, 整个 tasks 都会回滚, 修正 playbook 中的错误, 然后重新执行即可.
tasks: | |
- name: create user use variable | |
user: name={{user}} state=present | |
- name: install httpd server | |
yum: name=httpd state=latest name=httpd-devel state=latest | |
- name: start httpd service | |
service: name=httpd state=started enabled=yes | |
- name: copy httpd.conf to group1:/etc/httpd/conf/ | |
copy: src=/opt/httpd.conf dest=/etc/httpd/conf/ | |
handlers: 类似 task,但需要使用 notify 通知调用,实现按需调用。
- 不管有多少个通知者进行了 notify,等到 play 中的所有 task 执行完成之后,handlers 也只会被执行一次.
- handlers 最佳的应用场景是用来重启服务, 或者触发系统重启操作. 除此以外很少用到了.
notify: | |
- restart httpd service | |
handlers: | |
- name: restart httpd service | |
service: name=httpd state=restarted | |
# 注意: handlers 需要 notify 调用,他和 tasks 不同的是 tasks 每次都会调用,heandlers 触发才调用,比如配置文件修改了,在执行 playbook 的时候,就会将管理机上的新改的 copy 到被管理机,那么就会触发 headlers 重启服务,否则不会执行 heanlers |
练习: 修改 httpd 的端口为 8080, 再执行 playbook 测试
variables: 变量
- 定义变量可以被多次方便调用
vars: | |
- user: zutuanxue |
with_items: 迭代列表
- 其使用格式为将需要迭代的内容定义为 item 变量引用,并通过 with_items 语句指明迭代的元素列表即可。
例如安装多个软件包
yum: name={{item}} state=latest | |
with_items: | |
- httpd | |
- httpd-devel |
执行后有如下警告
解决方法:
在 /etc/ansible/ansible.cfg 配置文件里的 [default] 配置段下面加上 deprecation_warnings=False 参数即可
二、练习案例
写一个 playbook 实现
- 配置 yum
- 安装 vsftpd 包
- 修改配置文件(要求拒绝匿名用户登录)
- 启动服务并实现 vsftpd 服务开机自动启动
- hosts: group1 | |
remote_user: root | |
tasks: | |
- name: rm yum repository | |
file: path=/etc/yum.repos.d/ state=absent | |
- name: 同步 master 上的 yum 源到 group1 | |
copy: src=/etc/yum.repos.d dest=/etc/ | |
- name: ensure vsftpd is at the latest version | |
yum: name=vsftpd state=latest | |
- name: write the apache config file | |
copy: src=/etc/vsftpd/vsftpd.conf dest=/etc/vsftpd/vsftpd.conf | |
notify: | |
- restart vsftpd | |
- name: ensure vsftpd is running (and enable it at boot) | |
service: name=vsftpd state=started enabled=yes | |
handlers: | |
- name: restart vsftpd | |
service: name=vsftpd state=restarted |
