创建目录:
### 创建剧本规范目录: mkdir -p /etc/ansible/roles/redis/{files,templates,vars,tasks,defaults,handlers} ### 目录简介: vars: 变量定义存放 tasks: 任务列表所有执行步骤都在tasks目录下的main.yaml定义.如剧本太长,可以分成几个步骤,最后在mani.yaml里面include XXX.yaml即可. defaults: 设置默认变量时使用此目录 handler: 放置tasks里面定义的配置变更之后“notify” 重启的脚本 meta: 定义当前角色的特殊依赖关系,其他文件需要在此文件中- include *** 以上所有目录至少都应该存在一个main.yaml 文件, 目录内main.yaml内容根据功能归类编写,ansible 会自动加载所有目录内的所有manin.yaml*** files: 默认存放copy和scripts模块调用的文件 templates: templates模块调用病查找配置模板文件的目录 ### 例: [root@redis-2 redis]# tree . ├── defaults │ └── main.yaml ├── files │ └── main.yaml ├── handlers │ └── main.yaml ├── tasks │ └── main.yaml ├── templates │ ├── redis.conf.j2 │ └── redis.conf.j2_bak └── vars └── main.yaml 6 directories, 7 files
准备redis配置文件:
### 定义hosts主机: [web] t1 ansible_ssh_host=10.10.4.209 ansible_ssh_port=22 ansible_ssh_user=root ### 主机列表可使用的参数: ansible_ssh_host ansible_ssh_port ansible_ssh_user ansible_ssh_pass ansible_python_interpreter=/usr/bin/python2.6.6 ...... ### 准备redis.conf 配置文件(自行获取): cp /etc/redis.conf /etc/ansible/roles/redis/templates/redis.conf.j2
准备redis.yaml ansible-playbook入口文件:
### redis.yaml 剧本文件: cd /etc/ansible/ vim redis.yaml - hosts: t1 # 根据自己的需求定义主机 or 主机组 remote_user: root gather_facts: True roles: - redis ***采用这样的方式后,就不能把tasks列表都写在同一个文件内,要按照规范的层级目录和结构去编写. ***
tasks 剧本所有步骤:
vim main.yaml - name: System Add group {{ redisgroup }} group: gid={{ usergid }} name={{ redisgroup }} state=present system=yes - name: System Add user {{ redisuser }} user: name: '{{ redisuser }}' password: "$6$yIKPGtAX$acGYiPD08gIwwnwGIN1LhUc9JKaN5sMHrJX6VrBNvcrPBwDdiX4yZrzbKX5D6ndQorX35MlCmYekFlkIYc/" shell: /sbin/nologin group: '{{ usergid }}' uid: '{{ useruid }}' create_home: True state: present - name: create redis database directory file: path='/data/redis_data' state=directory mode='0755' owner={{ redisuser }} group={{ redisgroup }} - name: create logs directory file: path='/data/logs/redis' state=directory mode='0755' owner={{ redisuser }} group={{ redisgroup }} - name: yum install {{ pkgname }} yum: name={{ pkgname }} state=present - name: Template Set {{ pkgname }} Config Files template: src='redis.conf.j2' dest='/etc/redis.conf' owner={{ redisuser }} group={{ redisgroup }} mode='0755' notify: - restart redis service - name: set {{ pkgname }} service status service: name={{ item }} state=started enabled=yes with_items: - redis
vars 变量的定义:
此处的变量定义供本次剧本执行定义的内容,变量的使用即可用ansible自带的setup模块获取的系统变量,也可以使用自己定义的内容,根据需求自定义即可 pkgname: 'redis' # iotop,iftop,htop' bindip: '127.0.0.1' bindip_eth0: "{{ ansible_eth0.ipv4.address }}" mode: 'yes' redislog: '/data/logs/redis/redis.log' datadb: '30' datadir: '/data/redis_data' redispass: '666666' maxnum: '1024' appendo: 'yes' redisuser: 'redis' redisgroup: 'redis' usergid: '600' useruid: '600'
templates redis.conf.j2 模板文件的定义:
***此处仅仅展示部分配置, 主要演示变量和模板在playbook中的应用. 变量在vars/main.yaml定义,变量在调用使用 '{{ 变量名 }}' *** ***tasks 里面写了tamplates:后 自动到这个目录下寻找 *** [root@redis-2 redis]# egrep -vE '^#|^$' templates/redis.conf.j2 bind {{ bindip }} bind {{ bindip_eth0 }} protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize {{ mode }} supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile {{ redislog }} databases {{ datadb }} save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir {{ datadir }} slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 requirepass {{ redispass }} maxclients {{ maxnum }} maxmemory 1024m appendonly {{ appendo }} 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 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 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes
handlers 目录,当配置文件有变更,tasks里面的notify就执行重启步骤:
注意: 此处name后的任务描述要和tasks/main.yaml内templates项改变后,调用的notify后的描述一致. vim main.yaml - name: restart redis service service: name={{ pkgname }} state=restarted
执行剧本 开始安装:
[root@redis-2 ansible]# ansible-playbook redis.yaml PLAY [t2] *************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************** ok: [t2] TASK [redis : System Add group redis] *********************************************************************************************** ok: [t2] TASK [redis : System Add user redis] ************************************************************************************************ ok: [t2] TASK [redis : create redis database directory] ************************************************************************************** ok: [t2] TASK [redis : create logs directory] ************************************************************************************************ ok: [t2] TASK [redis : yum install redis] **************************************************************************************************** ok: [t2] TASK [redis : Template Set redis Config Files] ************************************************************************************** ok: [t2] TASK [redis : set redis service status] ********************************************************************************************* ok: [t2] => (item=redis) PLAY RECAP ************************************************************************************************************************** t2 : ok=8 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0