首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不可用动态文件名发送电子邮件

不可用动态文件名发送电子邮件
EN

Stack Overflow用户
提问于 2020-09-23 15:45:48
回答 2查看 2.5K关注 0票数 2

我需要使用Ansible从远程服务器复制日志文件,重命名它们并通过电子邮件作为附件发送。

我创造了这个剧本:

代码语言:javascript
复制
---
 - hosts: WebAccessServers
   vars:
    date: "{{ lookup('pipe', 'date +%Y%m%d') }}"
   tasks:
     - name: copy web access log file from Web servers to Ansible Server and rename it
       fetch:
        src: /var/www/html/mywebapp/logs/access.log
        dest: /tmp/{{ date }}_{{ inventory_hostname }}_access.log
        flat: yes
        ignore_errors: true
        delegate_to: localhost

 - hosts: localhost
   vars:
    date: "{{ lookup('pipe', 'date +%Y%m%d') }}"
   tasks:
     - name: create variable for the attachments
       shell: "ls /tmp/{{ date }}_*.log"
       register: command_output
     - debug: msg="{{ command_output.stdout_lines }}"

     - name: Send Emails to a bunch of users, with Playbook Report as an attachment.
       mail:
        host: mysmtp.mydomain.com
        port: 25
        subject: Ansible Playbook Report
        body: This is an Email generated using Ansible after execution of task.
        from: ansibbe@gmydomain.com (Ansible Automates)
        to:
        - John Brown <j.ba@mydomain.com>
        attach:
        - "{{  command_output.stdout_lines }}"
        headers:
        - Reply-To=ansible@domain.com
        - X-Special="Write something special about this Email"
        charset: us-ascii

如果运行它,就会收到以下输出:

PLAY WebAccess

任务收集事实**********************************************************************************************************************确定:服务器-1 ok: Server-2 ok: Server-3

任务从web服务器复制自定义的Web访问日志文件********************************************************************************************* ok: Server-1 ok: Server 2 ok: Server-3

播放本地主机

任务收集事实**********************************************************************************************************************

任务创建附件的变量****************************************************************************************************************已更改: localhost

任务调试***********************************************************************************************************************,“/tmp/20200923_Server-2_ as .tmp”、“/tmp/20200923_Server-3-Access.log”}任务向一群用户发送电子邮件,并将Playbook报告作为附件。****************************************************************************任务执行过程中发生异常。要查看完整的跟踪,请使用-vvv。错误是: TypeError:预期的str、字节或os.PathLike对象,不列出致命的: localhost:=> {"changed":false,"msg":“未能发送邮件:无法附加文件‘/tmp/20200923_Server-1_access.log. not’,‘/tmp/20200923_Server-2_access.log. not’,‘/tmp/20200923_Server-3_access.log.not’:预期的str、字节或os.PathLike对象,而不是列表”,"rc":1}播放RECAP ****************************************************************************************************************服务器-1: ok= changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 Server-2 : ok=2 changed=0 unreachable=0

failed=0 skipped=0 rescued=0 ignored=0 Server-3 : ok=2

changed=0 unreachable=0 failed=0 skipped=0 rescued=0

ignored=0 localhost : ok=3 changed=1 unreachable=0 failed=1

skipped=0 rescued=0 ignored=0

在电子邮件中动态和递归地分配附加内容的最佳方法是什么?谢谢你的支持。马可

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-29 13:34:46

这是我的工作解决方案:

代码语言:javascript
复制
---
 - hosts: myapp
   vars:
    date: "{{ lookup('pipe', 'date +%Y%m%d') }}"
   tasks:
     - name: copy access_allowed log file from MyApp servers
       fetch:
        src: /var/www/html/myapp/logs/access_allowed.log
        dest: /tmp/{{ date }}_{{ inventory_hostname }}_access_allowed.log
        flat: yes
        ignore_errors: true
        delegate_to: localhost
     - name: copy access_denied log file from MyApp servers
       fetch:
        src: /var/www/html/myapp/logs/access_denied.log
        dest: /tmp/{{ date }}_{{ inventory_hostname }}_access_denied.log
        flat: yes
        ignore_errors: true
        delegate_to: localhost

     - name: Ansible delete log file
       shell: sudo rm -rf /var/www/html/myapp/logs/access*.log

 - hosts: localhost
   vars:
    date: "{{ lookup('pipe', 'date +%Y%m%d') }}"
   tasks:
     - name: create variable for the attachments
       shell: "ls /tmp/{{ date }}_*.log"
       register: command_output

     - name: Send Emails to a bunch of users, with Playbook Report as an attachment.
       mail:
        host: my.smtp.local
        port: 25
        subject: Ansible Playbook Report
        body: This is an Email generated using Ansible after execution of task.
        from: ansible@mydomain.com (Ansible Automates)
        to: John Brow <JB@mydomain.com>
        attach: "{{ command_output.stdout_lines | join(',') }}"
        headers:
        - Reply-To=ansible@mydomain.com
        - X-Special="Write something special about this Email"
        charset: us-ascii

     - name: Ansible delete log file
       shell: sudo rm -rf /tmp/*access*.log
票数 0
EN

Stack Overflow用户

发布于 2022-01-05 18:39:36

代码语言:javascript
复制
  tasks:

    - name: Create variable for the attachments
      shell: "ls *.log"
      register: log_files
    - debug: msg="{{ log_files.stdout_lines | list | join(',') }}"
代码语言:javascript
复制
- name: Email logs
  community.general.mail:
代码语言:javascript
复制
        attach: "{{ log_files.stdout_lines | list | join(',') }}"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64031654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档