创建role的步骤
创建role的目录结构.在以roles命名的目录下分别创建以各角色名称命名的目录,如mysql等,在每个角色命名的目录中分别创建相关的目录和文件,比如tasks、files、handlers、templates和vars等目录;用不到的目录可以创建为空目录,也可以不创建
编写和准备指定role的功能文件,包括: tasks,templates,vars等相关文件
编写playbook文件调用上面定义的role,应用到指定的主机
利用 ansible-galaxy 创建角色目录的结构
ansible-galaxy role init redis
主机清单文件可以放到role同级
cat hosts_redis
[redis:vars]
uid=6379
user=redis
gid=6379
group=redis
redis_version="6.0.15"
redis_file="redis-{{redis_version}}.tar.gz"
redis_data_dir="/data/redis"
redis_base_dir="/usr/local"
redis_password=123456
cd roles/redis/files/
pwd
/data/ansible/roles/redis/files
wget http://download.redis.io/releases/redis-6.0.15.tar.gz
cat templates/redis.conf.j2
bind 0.0.0.0
requirepass {{ redis_password }}
masterauth {{ redis_password }}
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile {{ redis_base_dir }}/bin/redis.pid
loglevel notice
logfile "{{ redis_data_dir }}/log/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir {{ redis_data_dir }}/data
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
cat templates/redis.service.j2
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart={{redis_base_dir}}/redis/bin/redis-server {{redis_base_dir}}/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=forking
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=1000000
[Install]
WantedBy=multi-user.target
cat /data/ansible/redis_role.yml
- hosts: redis
remote_user: root
roles:
- redis
cat roles/redis/tasks/main.yml
- name: yum install packages
yum:
name: "{{ item }}"
loop:
- gcc
- make
- jemalloc-devel
- systemd-devel
- name: create group {{ group }}
group:
name: "{{ group }}"
gid: "{{ gid }}"
- name: create user {{ user }}
user:
name: "{{ user }}"
uid: "{{ uid }}"
group: "{{ group }}"
system: yes
shell: /sbin/nologin
create_home: yes
home: "{{ redis_data_dir }}"
- name: create redis rdb_data log directory
file:
path: "{{ redis_data_dir }}/{{ item }}"
state: directory
group: "{{ group }}"
owner: "{{ user }}"
mode: '0755'
loop:
- "data"
- "log"
- name:
unarchive:
src: "{{ redis_file }}"
dest: "{{ redis_base_dir }}/src"
creates: "{{ redis_base_dir }}/src/redis-{{ redis_version }}"
- name: install redis-{{ redis_version }}
shell:
chdir: "{{ redis_base_dir }}/src/redis-{{ redis_version }}"
cmd: "make -j 2 USE_SYSTEMD=yes PREFIX={{ redis_base_dir }}/redis install"
creates: "{{ redis_base_dir }}/redis"
- name: Create an etc directory for storing redis.conf
file:
path: "{{ redis_base_dir }}/redis/etc"
state: directory
mode: '0755'
- name: Copy the compiled redis.conf file to etc
template:
src: redis.conf.j2
dest: "{{ redis_base_dir }}/redis/etc/redis.conf"
tags: redis_conf
notify:
- restart redis
- name: chown {{ user }} {{ redis_base_dir}}/redis
shell:
cmd: "chown -R {{ redis_base_dir}}/redis;chmod 755 -R {{ redis_data_dir }}"
- name: copy redis.service
template:
src: redis.service.j2
dest: /lib/systemd/system/redis.service
notify:
- daemon-reload
- name: redis PATH variable
copy:
content: 'PATH={{ redis_base_dir }}/redis/bin:$PATH'
dest: /etc/profile.d/redis.sh
- name: Chanage net.core.somaxconn
sysctl:
name: net.core.somaxconn
value: '1024'
sysctl_set: yes
state: present
- name: vm.overcommit_memory
sysctl:
name: vm.overcommit_memory
value: '1'
sysctl_set: yes
state: present
- name: set transparent_hugepage
shell:
cmd: "echo never > /sys/kernel/mm/transparent_hugepage/enabled"
- name: start redis
service:
name: redis
state: started
enabled: yes
cat roles/redis/handlers/main.yml
- name: restart redis
systemd:
name: redis
state: restarted
- name: daemon-reload
systemd:
daemon_reload: yes
#-C模拟执行,但是unarchive会报错并不会真的解包
ansible -i hosts_redis redis_role.yml -C
#这个redis_role可以复用,redis.conf地方也加入tag课单独执行模块
#列出tags
ansible-playbook -i hosts_redis --list-tags redis_role.yml
playbook: redis_role.yml
play #1 (redis): redis TAGS: []
TASK TAGS: [redis_conf]
# 单独执行拷贝redis.conf模块
ansible-playbook -i hosts_redis -t redis_conf httpd.yml
开始执行redis作业
ansible-playbook -i hosts_redis redis_role.yml
PLAY [redis] ******************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [10.1.10.113]
ok: [10.1.10.114]
TASK [redis : yum install packages] *******************************************************************************************************************************
ok: [10.1.10.114] => (item=gcc)
ok: [10.1.10.113] => (item=gcc)
ok: [10.1.10.114] => (item=make)
ok: [10.1.10.113] => (item=make)
ok: [10.1.10.114] => (item=jemalloc-devel)
ok: [10.1.10.113] => (item=jemalloc-devel)
ok: [10.1.10.113] => (item=systemd-devel)
ok: [10.1.10.114] => (item=systemd-devel)
TASK [redis : create group redis] *********************************************************************************************************************************
ok: [10.1.10.113]
ok: [10.1.10.114]
TASK [redis : create user redis] **********************************************************************************************************************************
ok: [10.1.10.113]
ok: [10.1.10.114]
TASK [redis : create redis rdb_data log directory] ***************************************************************************************************************
ok: [10.1.10.114] => (item=data)
ok: [10.1.10.113] => (item=data)
ok: [10.1.10.114] => (item=log)
ok: [10.1.10.113] => (item=log)
TASK [redis : unarchive] ******************************************************************************************************************************************
skipping: [10.1.10.113]
skipping: [10.1.10.114]
TASK [redis : install redis-6.0.15] *******************************************************************************************************************************
ok: [10.1.10.113]
ok: [10.1.10.114]
TASK [redis : Create an etc directory for storing redis.conf] *****************************************************************************************************
ok: [10.1.10.114]
ok: [10.1.10.113]
TASK [redis : Copy the compiled redis.conf file to etc] ***********************************************************************************************************
ok: [10.1.10.114]
ok: [10.1.10.113]
TASK [redis : chown redis /usr/local/redis] ***********************************************************************************************************************
changed: [10.1.10.113]
changed: [10.1.10.114]
TASK [redis : copy redis.service] *********************************************************************************************************************************
ok: [10.1.10.113]
ok: [10.1.10.114]
TASK [redis : redis PATH variable] ********************************************************************************************************************************
ok: [10.1.10.113]
ok: [10.1.10.114]
TASK [redis : Chanage net.core.somaxconn] *************************************************************************************************************************
ok: [10.1.10.113]
ok: [10.1.10.114]
TASK [redis : vm.overcommit_memory] *******************************************************************************************************************************
ok: [10.1.10.113]
ok: [10.1.10.114]
TASK [redis : set transparent_hugepage] ***************************************************************************************************************************
changed: [10.1.10.113]
changed: [10.1.10.114]
TASK [redis : start redis] ****************************************************************************************************************************************
ok: [10.1.10.113]
ok: [10.1.10.114]
PLAY RECAP ********************************************************************************************************************************************************
10.1.10.113 : ok=15 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
10.1.10.114 : ok=15 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0