一、安装ansible服务工具
在安装服务之前先添加epel的yum源,因为ansible的安装依赖epel源
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
yum -y install ansible
ansible作为一款自动化软件工具,不需要修改配置文件就可以直接启动
ansible的目录结构:
/etc/ansible/ansible.cfg --- ansible服务配置文件
/etc/ansible/hosts --- 主机清单文件 定义可以管理的主机信息
/etc/ansible/roles --- 角色目录
二、创建roles角色目录
官方地址:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html
官方的目录定义:
[root@master1 /etc/ansible/roles]# tree
.
├── nfs #角色名称
│ ├── files #存放需要copy的文件
│ ├── handlers #触发任务剧本
│ ├── tasks #具体任务剧本
│ ├── templates #模版文件
│ └── vars #存放变量文件
1.创建目录
根据官方的目录规划,依次创建目录:
mkdir redis_cluster
cd redis_cluster/
mkdir files handlers tasks templates vars
cd files/
mkdir redis_{63{79,80},cmd}
mkdir redis_63{79,80}/{conf,logs,pid}
2.将redis的二进制文件复制到redis_cmd目录下
mkdir /opt/cluster/redis_cluster/src
cd /opt/cluster/redis_cluster/src
wget http://download.redis.io/releases/redis-3.2.9.tar.gz
tar xf redis-5.0.7.tar.gz -C /opt/cluster/redis_cluster/
cd /opt/cluster/redis_cluster/redis-5.0.7/src/
cp -a redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server /etc/ansible/roles/redis_cluster/files/redis_cmd/
3.生成ssh免交互
ssh-keygen -f /root/.ssh/id_rsa -N ''
ssh-copy-id 192.168.66.101
4.编写主机清单
vim /etc/ansible/hosts
...
[redis-cluster]
192.168.66.101
5.配置文件jinja模版
主节点配置模板
vim /etc/ansible/roles/redis_cluster/templates/redis_6379.conf.j2
bind {{ansible_facts.eth0.ipv4.address}}
port 6379
daemonize yes
pidfile "/opt/cluster/redis_cluster/redis_6379/pid/redis_6379.pid"
logfile "/opt/cluster/redis_cluster/redis_6379/logs/redis_6379.log"
dbfilename "redis_6379.rdb"
dir "/data/cluster/redis_cluster/redis_6379/"
appendonly yes
appendfilename "redis.aof"
appendfsync everysec
cluster-enabled yes
cluster-config-file nodes_6379.conf
cluster-node-timeout 15000
从节点配置模版
vim /etc/ansible/roles/redis_cluster/templates/redis_6380.conf.j2
bind {{ansible_facts.eth0.ipv4.address}}
port 6380
daemonize yes
pidfile "/opt/cluster/redis_cluster/redis_6380/pid/redis_6380.pid"
logfile "/opt/cluster/redis_cluster/redis_6380/logs/redis_6380.log"
dbfilename "redis_6380.rdb"
dir "/data/cluster/redis_cluster/redis_6380/"
appendonly yes
appendfilename "redis.aof"
appendfsync everysec
cluster-enabled yes
cluster-config-file nodes_6380.conf
cluster-node-timeout 15000
6.配置启动脚本
主节点启动脚本
vim /etc/ansible/roles/redis_cluster/templates/redis-master.service.j2
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-server /opt/cluster/redis_cluster/redis_6379/conf/redis_6379.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli -h {{ansible_facts.eth0.ipv4.address }} -p 6379 shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
从节点启动脚本
vim /etc/ansible/roles/redis_cluster/templates/redis-slave.service.j2
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-server /opt/cluster/redis_cluster/redis_6380/conf/redis_6380.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli -h {{ ansible_facts.eth0.ipv4.address}} -p 6380 shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
7.tasks清单
vim /etc/ansible/roles/redis_cluster/tasks/main.yaml
#01-创建用户组
- name: 01-create_group
group:
name: redis
gid: 777
#02-创建用户
- name: 02-create_user
user:
name: redis
group: redis
uid: 777
shell: /sbin/nologin
create_home: no
#03-复制可执行文件
- name: 03_copy_cmd
copy:
src: redis_cmd/
dest: /usr/local/bin/
mode: '0755'
#04-复制运行目录
- name: 04_copy_conf
copy:
src: "{{ item }}"
dest: /opt/cluster/redis_cluster/
owner: redis
group: redis
loop:
- redis_6380
- redis_6379
#05-创建数据目录
- name: 05_create_data
file:
dest: "/data/cluster/redis_cluster/{{ item }}"
state: directory
owner: redis
group: redis
loop:
- redis_6379
- redis_6380
#06-拷贝配置文件模板
- name: 06_copy_conf
# ignore_errors: yes
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
backup: yes
with_items:
- { src: 'redis_6379.conf.j2', dest: '/opt/cluster/redis_cluster/redis_6379/conf/redis_6379.conf' }
- { src: 'redis_6380.conf.j2', dest: '/opt/cluster/redis_cluster/redis_6380/conf/redis_6380.conf' }
- { src: 'redis-master.service.j2', dest: '/usr/lib/systemd/system/redis-master.service' }
- { src: 'redis-slave.service.j2', dest: '/usr/lib/systemd/system/redis-slave.service' }
#07-启动服务
- name: 07_start_redis_service
ignore_errors: yes
systemd:
name: "{{ item }}"
state: started
daemon_reload: yes
loop:
- redis-master
- redis-slave
notify:
- restart redis-master
- restart redis-slave
8.handles清单
vim /etc/ansible/roles/redis_cluster/handlers/main.yaml
- name: restart redis-master
service:
name: redis-master
state: restarted
- name: restart redis-slave
service:
name: redis-slave
state: restarted
9.执行任务清单
vim /etc/ansible/redis_cluster.yaml
- hosts: redis-cluster
roles:
- redis_cluster
10.检查剧本语法
ansible-playbook --syntax-check /etc/ansible/redis_cluster.yaml
11.模拟执行剧本
ansible-playbook -C /etc/ansible/redis_cluster.yaml
12.执行剧本
ansible-playbook /etc/ansible/redis_cluster.yaml
网友评论