playbook 介绍
1、什么是playbook
- 将多个要执行ansible操作安装顺序整合到一个文件中,
- 这个文件就是yaml 执行yaml文件的方法是通过ansible-playbook执行
yaml文件中的元素
- 变量
- 循环
- 判断
2、变量
变量命名规则
- 只能以字母为开头
- 构成只能有数字、字母、下划线
变量类别
- 自定义
- 内置变量
获取内置变量
ansible all -m setup
执行a.yaml,同时传递变量name=tom,age=20
# ansible-playbook a.yaml --extra-vars "name=tom, age=20"
案例:安装httpd,要求设置apache的端口为808,网站跟目录/myweb
- host: all # 指定要操作的主机组
remoute_user: root # 指定在远程主机上以哪个用户身份执行tasks
vars: # 定义变量
- port: 808 # 变量名、变量值
- root: /myweb # 变量名、变量值
tasks: # 指定要执行的操作列表
- name: install httpd # 设置一个名称,用于给用户提示正在执行操作
yum: name=httpd state=latest # yum是模块名,后面是参数
- name: start httpd
service: name=httpd state=started # service是模块名,后面的参数
注意:playbook对格式缩进要求十分严格。
yaml 文件中的主要构成
- host 主机组
- tasks 要执行的操作列表
- vars 定义变量
- remote_user 在远程主机上以什么用户执行
3、触发器
- 让一个task在特定的情况下才会被执行
案例:在所有被管理节点安装httpd,然后启动httpd,要求httpd启动端口是8080
- hosts: all
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
- name: start httpd
service: name=httpd state=started enabled=true
- name: send httpd.conf
copy: src=/root/httpd.conf.template dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
handlers:
- name: restart httpd
service: name=httpd state=restarted
4、yaml使用变量
(1)自定义变量
- hosts: all
remote_user: root
vars:
- package_name: mysql-server # mariadb-server
- service_name: mysqld # mariadb
tasks:
- name: install mysql server
yum: name={{ package_name }} state=latest
- name: start mysql server
service: name={{ service_name }} state=started
(2)使用ansible的内置变
在每个被管理主机上创建一个用户,用户名和主机名相同
查询内置变量 [root@centos6-1 ~]# ansible all -m setup | grep fqdn "ansible_fqdn": "centos6-2", [root@centos6-1 ~]# cat c.yaml - hosts: all tasks: - name: create user user: name={{ ansible_fqdn }} state=present - name: create file file: name=/tmp/{{ ansible_fqdn }} state=touch
(3)主机清单变量
# 定义主机变量 [webservers] 192.168.31.64 userid=1050 192.168.31.66 userid=1060 # 定义主机组变量 [webservers:vars] username=jerry
- hosts: all tasks: - name: create user user: name={{ username }} uid={{ userid }} state=present
5、template
-
template称为模板
-
模板的用处是用来动态生成服务的配置文件
- hosts: all
tasks:
- name: delete old epel repo file
shell: rm -rf /etc/yum.repos.d/epel*
- name: create new epel repo file
shell: wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
- name: install nginx
yum: name=nginx state=latest
- name: send nginx.conf
template: src=/root/template.j2 dest=/etc/nginx/nginx.conf
notify:
- restart nginx
- name: start nginx
service: name=nginx state=started enabled=true
handlers:
- name: restart nginx
service: name=nginx state=restarted
6.判断
- 格式 :when
- 作用:判断满足条件,才执行操作
案例:用playbook实现如下功能
1:在所有被管理主机上安装zsh
2:在主机名为centos7-2的主机上创建用户tom3,其他主机不创建
- hosts: all
tasks:
- name: install zsh
yum: name=zsh state=present
- name: create user tom3
user: name=tom3 state=present
when: ansible_fqdn == "centos7-5"
7.判断
- 内置变量:item
- 循环格式:with_items 列表
- hosts: all
tasks:
- name: create user
user: name={{ item }} state=present
with_items:
- u1
- u2
- u3
- u4
- u5
8 tags
作用:给某个task设置一个表情,用于仅仅执行某个task
- hosts: all
tasks:
- name: isntall
shell: yum install httpd -y
- name: send file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
tags:
- senfile
- name: start httpd
shell: systemctl start httpd
handlers:
- name: restart httpd
shell: systemctl restart httpd
[root@centos7-1 ~]# ansible-playbook 4.yaml --tags="senfile"

浙公网安备 33010602011771号