==Roles小技巧:==
1.创建roles目录结构,手动或使用ansible-galaxy init test roles
2.编写roles的功能,也就是tasks。 nginx rsyncd memcached
3.最后playbook引用roles编写好的tasks
1.创建标准roles目录结构
mkdir /root/roles/nginx/{tasks,templates,handlers}
2.编写tasks
[root@manager ~]# cat /root/roles/nginx/tasks/main.yml
- name: Install Nginx Server
yum:
name: nginx
state: present
- name: Configure Nginx Server
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart Nginx Server
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
3.template
[root@manager roles]# cat /root/roles/nginx/templates/nginx.conf.j2
user www;
worker_processes {{ ansible_processor_vcpus }};
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections {{ ansible_processor_vcpus * 1024 }};
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
4.handlers
[root@manager ~]# cat /root/roles/nginx/handlers/main.yml
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
5.调用playbook
[root@manager roles]# cat /root/roles/site.yml
- hosts: webservers
roles:
- nginx
PS:hosts ansible.cfg 提前需准备好
==实战一:roles部署memcached==
#安装
#配置
#启动
1.创建roles的目录结构
[root@manager roles]# mkdir memcached/{tasks,templates,handlers} -p
2.编写对应的tasks (1.安装 2配置(templates) 3.启动 4.重启(handlers) )
[root@manager roles]# cat memcached/tasks/main.yml
- name: Installed Memecached Server
yum:
name: memcached
state: present
- name: Configure Memcached Server
template:
src: memcached.j2
dest: /etc/sysconfig/memcached
notify: Restart Memcached Server
- name: System Memcached Server
systemd:
name: memcached
state: started
enabled: yes
3.配置jinja2
[root@manager roles]# cat memcached/templates/memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ ansible_memtotal_mb //2 }}"
OPTIONS=""
4.handlers
[root@manager roles]# cat memcached/handlers/main.yml
- name: Restart Memcached Server
systemd:
name: memcached
state: restarted
5.playbook调用roles
[root@manager roles]# cat site.yml
- hosts: webservers
roles:
- { role: nginx, tags: web }
- { role: memcached, tags: cache }
==实战二:部署NFS存储服务==
#1.创建项目目录结构 --->
[root@manager roles]# mkdir nfs/{tasks,templates,handlers} -p
#2.编写task任务
#3.playbook调用roles项目
[root@ansible roles]# tree nfs/
nfs/
├── handlers
│ └── main.yml
├── tasks
│ └── main.yml
└── templates
└── exports.j2
1.tasks编写
[root@ansible nfs]# cat tasks/main.yml
- name: Installed NFS Server
yum:
name: nfs-utils
state: present
- name: Configure NFS Server
template:
src: exports.j2
dest: /etc/exports
notify: Restart NFS Server
- name: Create NFS Server Share Directory
file:
path: "{{ nfs_word }}"
state: directory
owner: "{{ nfs_user }}"
group: "{{ nfs_user }}"
mode: '0755'
recurse: yes
- name: Started NFS Server
systemd:
name: nfs
state: started
enabled: yes
2.触发重启,配置handlers
[root@ansible nfs]# cat handlers/main.yml
- name: Restart NFS Server
systemd:
name: nfs
state: restarted
3.调用roles项目
[root@ansible roles]# cat site.yml
- hosts: nfsservers
roles:
- { role: nfs, tags: nfs }
实战三:roles部署LB
==1.nginxProxy+keepalived 10.0.0.5 10.0.0.6 10.0.0.3
2.nginx静态网站 172.16.1.7 172.16.1.8==
1.web编写tasks任务
[root@manager web]# cat tasks/main.yml
#1.安装nginx服务
- name: Install nginx Server
yum:
name: nginx
state: present
#2.创建用户组
- name: Create group
group:
name: "{{ user }}"
gid: "{{ uid }}"
#3.创建用户
- name: create user
user:
name: "{{ user }}"
uid: "{{ uid }}"
group: "{{ user }}"
create_home: no
shell: /sbin/nologin
#4.创建站点目录
- name: Create code directory
file:
path: "{{ code }}"
owner: "{{ user }}"
group: "{{ user }}"
mode: 0755
recurse: yes
#5.配置nginx
- name: Configure Nginx Server
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode }}"
with_items:
- { src: nginx.conf.j2, dest: /etc/nginx/nginx.conf, mode: '0644' }
- { src: index.html, dest: /code, mode: '0644' }
- { src: web.conf.j2, dest: /etc/nginx/conf.d/web.conf, mode: '0644' }
notify: Restart nginx Server
#6.启动nginx服务
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
2.handlers触发重启nginx
[root@manager web]# cat handlers/main.yml
- name: Restart nginx Server
systemd:
name: nginx
state: restarted
3.准备j2文件
[root@manager roles]# tree web/
web/
├── handlers
│ └── main.yml
├── tasks
│ └── main.yml
└── templates
├── index.html
├── nginx.conf.j2
└── web.conf.j2
4.LB编写tasks任务
[root@manager lb]# cat tasks/main.yml
- name: Intalled Nginx Server
yum:
name: "{{ packages }}"
state: present
vars:
packages:
- nginx
- keepalived
- name: Create shell share directory
file:
path: "{{ script }}"
state: directory
- name: Configure nginx server
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode }}"
with_items:
- { src: nginx.conf.j2, dest: /etc/nginx/nginx.conf, mode: '0644' }
- { src: keepalived.conf.j2, dest: /etc/keepalived/keepalived.conf, mode: '0644' }
- { src: check_server.sh, dest: /server/scripts/check_server.sh, mode: '0777' }
- { src: proxy_web.conf, dest: /etc/nginx/conf.d/proxy_web.conf, mode: '0644' }
- { src: proxy_params, dest: /etc/nginx/proxy_params, mode: '0644' }
notify: Restart Nginx Server
- name: Systemd Nginx server
systemd:
name: "{{ item }}"
state: started
enabled: yes
with_items:
- nginx
- keepalived
5.handlers触发重启nginx服务
[root@manager lb]# cat handlers/main.yml
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
6.配置keepalived
[root@manager lb]# cat files/keepalived.conf.j2
global_defs {
router_id {{ ansible_fqdn }}
}
vrrp_instance VI_1 {
{% if ansible_fqdn == 'lb01' %}
#lb01的配置如下:
state MASTER
priority 150
{% elif ansible_fqdn == 'lb02' %}
#lb02的配置如下:
state BACKUP
priority 100
{% endif %}
interface eth0
virtual_router_id 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
7.准备对应的j2文件
[root@manager roles]# tree lb/
lb/
├── files
│ └── keepalived.conf.j2
├── handlers
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
│ ├── check_server.sh
│ ├── keepalived.conf.j2
│ ├── nginx.conf.j2
│ ├── proxy_params
│ └── proxy_web.conf