1.什么是playbook?
playbook 剧本 <---文件 YAML
play 找谁 明星 找那个主机 web01
task 做什么 干什么事情 yum copy service
找多个明星,做多件事情
找一个明星,做多件事情
3.Playbook三板斧? 缩进 冒号 短横线 (语法格式)
1.使用playbook编写一个创建文件的yml
案例一、使用ansible安装并配置nfs服务
#172.16.1.31 nfs
#172.16.1.7 clinet
#172.16.1.8 clinet
#1.新增一台nfs服务器
[root@manager project1]# cat hosts
[nfsservers]
172.16.1.31
[webservers]
172.16.1.7
172.16.1.8
[root@manager project1]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
#2.测试三台主机是否通
[root@manager project1]# ansible all -m ping -i hosts
#3.编写一个nfs-server的yml
1.安装nfs yum
2.配置nfs copy
3.初始化环境
用户 group user
目录 file
授权 file
4.启动服务 systemd
#2.测试三台主机是否通
[root@manager project1]# ansible all -m ping -i hosts
#3.编写一个nfs-server的yml
1.安装nfs yum
2.配置nfs copy
3.初始化环境
用户 group user
目录 file
授权 file
4.启动服务 systemd
[root@manager 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
#4.编写一个nfs-clinet的yml
[root@manager project1]# cat nfs_client.yml
- hosts: webservers
tasks:
- name: Mount NFS Server share directory
mount:
src: 172.16.1.31:/ansible_data
path: /mnt
fstype: nfs
opts: defaults
state: mounted
案例二、使用ansible安装并配置nginx服务
1.安装 yum
2.配置 copy
3.启动 systmd
handlers
[root@manager project1]# cat nginx.yml
- hosts: webservers
tasks:
- name: Installed Nginx Server
yum:
name: nginx
state: present
- name: Configure Nginx Server
copy:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
owner: root
group: root
mode: 0644
backup: yes
notify: Restart Nginx Server
- name: Systmd nginx Server
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
修改配置文件重启nginx
执行ansible-playbook 可以看到配置文件的改变并重启了nginx
到被控制端验证
*案例三、使用AnsiblePlaybook方式构建LAP架构,具体操作步骤如下: *
1.使用yum安装 httpd、php、firewalld等 7.1 5.3
2.使用get_url下载http://fj.xuliangwei.com/public/index.php文件
3.启动httpd、firewalld、等服务
4.添加防火墙规则,放行http的流量*