1.什么是playbook
定义一个文本文件,一yml为后缀结尾
playbook是由一个或多个play组成,一个play可以包含多个task任务,可以理解为使用不同的模块共同完成一件事
2.playbook和AD-Hoc的关系
1)playbook是对AD-Hoc的一种编排方式
2)playbook是可以持久化运行,而Ad-hoc只能临时运行
3)playbook适合复杂的任务,而Ad-Hoc适合快速简单的任务
4)playbook能控制任务执行的先后顺序
3.语法格式
缩进:YAML使用固定的缩进风格表示层级结构,每个缩进由每个空格组成,不能使用tab键
冒号:以冒号结尾的除外,其他所有冒号后面必须有空格
短横线:表示列表项,使用一个短横杠加一个空格,多个项使用同样的缩进级别作为同一列表
--syntax #检测语法
-C #测试
案例一、使用ansible安装配置nfs服务
172.16.1.31 nfs
172.16.1.7 client
172.16.1.8 client
1)新增一台nfs服务器
[root@m01 project1]# cat hosts
[nfsservers]
172.16.1.31
2)推送秘钥
ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8
3)检测畅通
[root@m01 project1]# ansible all -m ping -i hosts
4)编写nfs-server的配置文件
[root@m01 project1]# cat nfs_server.yml
- hosts: nfsservers
tasks:
- name: Installed NFS Server
yum:
name: nfs-utils
state: present
- name: Configure NFS Server
copy:
src: ./file/exports.j2
dest: /etc/exports
owner: root
group: root
mode: 0644
backup: yes
- name: Create NFS group www
group:
name: www
gid: 666
- name: Create NFS User www
user:
name: www
group: www
uid: 666
create_home: no
shell: /sbin/nologin
- name: Create NFS Share Directory
file:
path: /ansible_data
state: directory
owner: www
group: www
mode: 0755
recurse: yes
- name: Systemd NFS Server
systemd:
name: nfs
state: restarted
enabled: yes
在当前项目目录内创建一个file目录
mkdir file
[root@m01 project1]# cat file/exports.j2
/ansible_data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
[root@m01 project1]# ansible-playbook nfs_server.yml -i hosts
![](https://img.haomeiwen.com/i18917578/21fda836f36957b5.png)
image.png
[root@nfs ~]# cat /etc/exports
/ansible_data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
![](https://img.haomeiwen.com/i18917578/5555473cf8f00140.png)
image.png
![](https://img.haomeiwen.com/i18917578/679a2ff041da3093.png)
image.png
![](https://img.haomeiwen.com/i18917578/bffe0670df724322.png)
image.png
网友评论