需求 1
下载apache2 进行部署网页
---
- hosts: all
become: yes # 使用become来提升权限,通常用于需要sudo权限的任务
tasks:
- name: 安装 apache2 服务器
apt:
name: apache2
state: present
update_cache: yes # 在安装之前更新apt缓存(可选,但推荐)
- name: 配置 web fu务器
copy:
content: "this is html for {{ ansible_hostname }}" # 修正了字符串的引号
dest: /var/www/html/index.html
owner: www-data # 设置文件所有者为www-data(Apache默认运行的用户)
group: www-data # 设置文件组为www-data
mode: '0644' # 设置文件权限为644(所有者读写,组和其他用户只读)
- name: 启动 apache2 服务器并设置开机自启
systemd:
name: apache2
state: started
enabled: yes # 设置apache2为开机自启
#执行
ansible-playbook ./apache2.yml
需求2
3台机器分发 一台主机为rsync的信息端 两台为rsync的备份端
#准备rsync配置文件
vim ./rsyncd.j2
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to oldboyedu backup!
path = /backup
---
- hosts: all
tasks:
#安装rsync
- name: Install Rsyncd Server
apt:
name: rsync
state: present
#创建www组
- name: Create www Group
group:
name: www
gid: 666
#创建www用户
- name: Create www User
user:
name: www
group: www
uid: 666
create_home: false
shell: /sbin/nologin
- hosts: aa_group
tasks:
#推送rsync配置文件
- name: Scp Rsync Config
copy:
src: ./rsyncd.j2
dest: /etc/rsyncd.conf
owner: root
group: root
mode: 0644
#创建密码文件并授权
- name: Create Passwd File
copy:
content: 'rsync_backup:123'
dest: /etc/rsync.passwd
owner: root
group: root
mode: 0600
#创建/backup目录
- name: Create backup Directory
file:
path: /backup
state: directory
mode: 0755
owner: www
group: www
recurse: yes
#启动rsync服务
- name: Start Rsyncd Server
systemd:
name: rsyncd
state: started
root@101:~# rsync -avz /etc/hosts [email protected]::backup
Password:
sending incremental file list
hosts
sent 257 bytes received 43 bytes 10.91 bytes/sec
total size is 217 speedup is 0.72
需求3
103 提供挂载点 101 102 挂载到/nfs 目录
cat ./nfs.j2
/data 10.0.0.0/24(rw,sync,all_squash,anonuid=668,anongid=668)
- hosts: bb_group
tasks:
#安装nfs
- name: Install nfs-utils
apt:
name: nfs-kernel-server
state: present
#创建nfs组
- name: Create nfs Group
group:
name: nfs
gid: 668
#创建nfs用户
- name: Create nfs User
user:
name: nfs
group: nfs
uid: 668
create_home: false
shell: /sbin/nologin
- hosts: bb_group
tasks:
#推送配置文件
- name: Scp NFS Server
copy:
src: ./nfs.j2
dest: /etc/exports
owner: root
group: root
mode: 0644
#创建挂载目录并授权
- name: Create data Directory
file:
path: /data
state: directory
owner: nfs
group: nfs
mode: 0755
recurse: yes
#启动nfs-server
- name: Start NFS Server
systemd:
name: nfs-server
state: started
enabled: yes
#web01和web02挂载目录
- hosts: aa_group
tasks:
- name: Install NFS Client Package
apt:
name: nfs-common
state: present
- name: Mount NFS Server
mount:
path: /opt
src: 10.0.0.103:/data
fstype: nfs
opts: defaults
state: mounted
#检查语法
ansible-playbook --syntax-check ./nfs.yml
playbook: ./nfs.yml
#执行
ansible-playbook nfs.yml