一、Ansible-playbook实战
1.Ansible-playbook安装软件
```bash
#编写yml
[root@ansible ansible]# cat wget.yml
- hosts: backup
tasks:
- name: Install wget
yum:
name: wget
state: present
#检查playbook的语法
[root@ansible ansible]# ansible-playbook --syntax-check wget.yml
playbook: wget.yml
#执行playbook
[root@ansible ansible]# ansible-playbook wget.yml
2.Playbook重构backup服务
```bash
1.定义主机清单
[root@ansible ansible]# cat /etc/ansible/hosts
nfs ansible_ssh_host=10.0.0.31
backup ansible_ssh_host=10.0.0.41
2.写playbook重构
[root@ansible ansible]# cat backup.yml
- hosts: backup
tasks:
- name: Install Rsync Server
yum:
name: rsync
state: present
- name: Configure Rsync Server
copy:
src: rsyncd.conf
dest: /etc/rsyncd.conf
- name: Create www Group
group:
name: www
gid: 666
- name: Create User www
user:
name: www
uid: 666
group: www
shell: /sbin/nologin
create_home: false
- name: Configure passwd file
copy:
content: rsync_backup:123456
dest: /etc/rsync.passwd
mode: 0600
- name: Create Dir /backup
file:
path: /backup
state: directory
owner: www
group: www
- name: Start Rsync Server
systemd:
name: rsyncd
state: started
enabled: yes
3.playbook重构nfs服务
```bash
1.定义主机清单
[root@ansible ansible]# cat /etc/ansible/hosts
nfs ansible_ssh_host=10.0.0.31
backup ansible_ssh_host=10.0.0.41
2.打通免秘钥
[root@ansible ~]# ssh-copy-id 10.0.0.31
3.写playbook
[root@ansible ansible]# cat nfs.yml
- hosts: nfs
tasks:
- name: Install NFS Server
yum:
name: nfs-utils
state: present
- name: Configure nfs Server
copy:
src: exports
dest: /etc/
- name: Create www Group
group:
name: www
gid: 666
- name: Create User www
user:
name: www
uid: 666
group: www
shell: /sbin/nologin
create_home: false
- name: Create /data/wp
file:
path: /data/wp
state: directory
owner: www
group: www
- name: Start NFS Server
systemd:
name: nfs
state: started
enabled: yes
客户端挂载:
[root@ansible ansible]# cat web.yml
- hosts: web01
tasks:
- name: Install nfs-utils
yum:
name: nfs-utils
state: present
- name: mount nfs /data/wp-->wordpress
mount:
src: 172.16.1.31:/data/wp
path: /code/wordpress/wp-content/uploads/
state: mounted
fstype: nfs
```