linux ansible剧本删除多个文件与搜索模式

hwamh0ep  于 2024-01-06  发布在  Linux
关注(0)|答案(2)|浏览(158)

使用Ansible,我如何删除下面的所有项目

  1. rm -rf /var/log/messages-*
  2. rm -rf /var/log/mail-*
  3. rm -rf /var/log/secure-*
  4. rm -rf /var/log/cron*
  5. rm -rf /var/log/spooler-*
  6. rm /var/log/up2date-*
  7. rm /var/log/maillog-*

字符串
我的playbook运行没有任何错误,但它不删除日志文件

  1. - hosts: all
  2. #remote_user: my id
  3. become: yes
  4. any_errors_fatal: false
  5. tasks:
  6. - name: take backup of syslog
  7. command: cp -pr /etc/logrotate.d/syslog /opt/syslog.bkp
  8. - name: Update syslog logrotate
  9. ansible.builtin.copy:
  10. src: /etc/ansible/sam/syslog_rhel7_8
  11. dest: /etc/logrotate.d/syslog
  12. owner: root
  13. group: root
  14. mode: '0644'
  15. - name: Delete old log-files
  16. command: "{{item}}"
  17. with_items:
  18. - rm -rf /var/log/messages-*
  19. - rm -rf /var/log/mail-*
  20. - rm -rf /var/log/secure-*
  21. - rm -rf /var/log/cron*
  22. - rm -rf /var/log/spooler-*
  23. - rm /var/log/up2date-*
  24. - rm /var/log/maillog-*

t9aqgxwy

t9aqgxwy1#

strings读取命令。

  1. shell> tree /tmp/ansible/log/
  2. /tmp/ansible/log/
  3. ├── mail-01
  4. ├── mail-02
  5. ├── mail-03
  6. ├── messages-01
  7. ├── messages-02
  8. └── messages-03
  9. directories, 6 files

字符串
下面任务

  1. - command: "{{ item }}"
  2. loop:
  3. - sh -c 'rm /tmp/ansible/log/messages-*'
  4. - sh -c 'rm /tmp/ansible/log/mail-*'


按预期工作

  1. TASK [command] ***************************************************
  2. changed: [localhost] => (item=sh -c 'rm /tmp/ansible/log/messages-*')
  3. changed: [localhost] => (item=sh -c 'rm /tmp/ansible/log/mail-*')


文件将被删除

  1. shell> tree /tmp/ansible/log/
  2. /tmp/ansible/log/
  3. 0 directories, 0 files

展开查看全部
2w2cym1i

2w2cym1i2#

在下面的最小示例中显示了一种简单的方法

  1. ~/test$ mkdir -p var/log/
  2. ~/test$ touch var/log/messages-0{1..9}
  3. ~/test$ touch var/log/mail-0{1..9}

字符串
Playbook

  1. ---
  2. - hosts: test
  3. become: false
  4. gather_facts: false
  5. tasks:
  6. - name: Delete old log files on Remote Node
  7. file:
  8. path: "{{ item }}"
  9. state: absent
  10. loop: "{{ lookup('ansible.builtin.fileglob', 'var/log/messages-*') | split(',') }}"


这将导致输出

  1. TASK [Delete old log files on Remote Node] ***************************************
  2. ok: [test2.example.com] => (item=/home/ansible_user/test/var/log/messages-02)
  3. changed: [test1.example.com] => (item=/home/ansible_user/test/var/log/messages-02)
  4. ok: [test2.example.com] => (item=/home/ansible_user/test/var/log/messages-07)
  5. changed: [test1.example.com] => (item=/home/ansible_user/test/var/log/messages-07)
  6. ok: [test2.example.com] => (item=/home/ansible_user/test/var/log/messages-09)
  7. ok: [test2.example.com] => (item=/home/ansible_user/test/var/log/messages-01)
  8. changed: [test1.example.com] => (item=/home/ansible_user/test/var/log/messages-09)
  9. ok: [test2.example.com] => (item=/home/ansible_user/test/var/log/messages-03)
  10. ok: [test2.example.com] => (item=/home/ansible_user/test/var/log/messages-08)
  11. changed: [test1.example.com] => (item=/home/ansible_user/test/var/log/messages-01)
  12. ok: [test2.example.com] => (item=/home/ansible_user/test/var/log/messages-04)
  13. ok: [test2.example.com] => (item=/home/ansible_user/test/var/log/messages-06)
  14. changed: [test1.example.com] => (item=/home/ansible_user/test/var/log/messages-03)
  15. ok: [test2.example.com] => (item=/home/ansible_user/test/var/log/messages-05)
  16. changed: [test1.example.com] => (item=/home/ansible_user/test/var/log/messages-08)
  17. changed: [test1.example.com] => (item=/home/ansible_user/test/var/log/messages-04)
  18. changed: [test1.example.com] => (item=/home/ansible_user/test/var/log/messages-06)
  19. changed: [test1.example.com] => (item=/home/ansible_user/test/var/log/messages-05)
  20. PLAY RECAP ***********************************************************************
  21. test1.example.com : ok=1 changed=1
  22. test2.example.com : ok=1 changed=0


根据您的需求进行调整和调整。

文件

展开查看全部

相关问题