[root@ansible archivelogweblogic]# cat archivelogweblogicnew.yml
---
- hosts: all
remote_user: root
gather_facts: no
tasks:
- name: "查看是否已经挂载naslog"
shell: "df -h|grep naslog"
register: mountresult
ignore_errors: yes
- block:
- name: "创建归档目录"
file:
path: /home/weblogic/naslog
state: directory
owner: weblogic
group: weblogic
mode: 0755
- name: "挂载盘符"
shell: "mount 172.31.33.201:/log /home/weblogic/naslog"
- name: "添加新的重启后挂载命令"
lineinfile:
path: /etc/rc.local
line: "mount 172.31.33.201:/log /home/weblogic/naslog"
when: (mountresult.stdout == "")
- name: "获取hostname"
shell: "hostname"
register: nname
- name: "获取ip"
shell: "hostname -i"
register: nip
- name: "获取归档目录状态"
shell: "ls /home/weblogic/naslog/{{ nip.stdout }}/{{ nname.stdout }}"
register: archivestate
ignore_errors: yes
- name: "创建归档目录"
file:
path: "/home/weblogic/naslog/{{ nip.stdout }}/{{ nname.stdout }}"
state: directory
owner: weblogic
group: weblogic
mode: 0755
when: archivestate.stderr.find("No such") != -1
- name: "查找/var/applog的日志目录"
find:
path: "/var/applog"
file_type: directory
register: logdir
- name: "获取日志子目录名称"
shell: "basename {{ item.path }}"
with_items: "{{ logdir.files }}"
register: logdirnames
when: logdir.matched != 0
- name: "创建日志子目录"
file:
path: "/home/weblogic/naslog/{{ nip.stdout }}/{{ nname.stdout }}/{{ item.stdout }}"
state: directory
owner: weblogic
group: weblogic
mode: 0755
with_items: "{{ logdirnames.results }}"
when: logdir.matched > 0
- name: "查找/var/applog的日志二级子目录"
find:
path: "{{ item.path }}"
file_type: directory
register: logdir2
with_items: "{{ logdir.files }}"
- name: "创建日志二级子目录"
shell: "{% for k in item.files %}mkdir -p /home/weblogic/naslog/{{ nip.stdout }}/{{ nname.stdout }}/`echo '{{ k.path }}'|awk -F 'applog/' '{print $2}'`\n {% endfor %}"
become: true
become_user: weblogic
become_method: su
with_items: "{{ logdir2.results }}"
when: ( item.matched > 0 )
- name: "查找1天前的日志文件"
find:
paths: "/var/applog/"
recurse: yes
age: 1d
patterns: '\S+\d+.log$'
use_regex: true
register: archivelogs
- name: "压缩日志"
shell: "tar -zcvf {{ item.path }}.tar.gz {{ item.path }}"
become: true
become_user: weblogic
become_method: su
with_items: "{{ archivelogs.files }}"
when: archivelogs.matched != 0
- name: "删除压缩过的日志"
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ archivelogs.files }}"
when: archivelogs.matched != 0
- name: "归档压缩后的日志(有子目录状态)"
shell: "mv /var/applog/{{ item.stdout }}/*.tar.gz /home/weblogic/naslog/{{ nip.stdout }}/{{ nname.stdout }}/{{ item.stdout }}/"
with_items: "{{ logdirnames.results }}"
#when: logdir.matched > 0
ignore_errors: yes
- name: "归档压缩后的日志(没有子目录)"
shell: "mv /var/applog/*.tar.gz /home/weblogic/naslog/{{ nip.stdout }}/{{ nname.stdout }}/"
#when: logdir.matched == 0
ignore_errors: yes
- name: "二级子目录mv"
shell: "{% for k in item.files %}mv '{{ k.path }}'/*.tar.gz /home/weblogic/naslog/{{ nip.stdout }}/{{ nname.stdout }}/`echo '{{ k.path }}'|awk -F 'applog/' '{print $2}'`\n {% endfor %}"
with_items: "{{ logdir2.results }}"
when: ( item.matched > 0 )