Mik*_*ike 3 email grep yum ansible
编写剧本以执行 yum 更新,然后从每个服务器获取电子邮件。我希望电子邮件包含yum.log的更改内容。
IOW,我想要以下结果:
grep [today's date] /var/log/yum.log
Run Code Online (Sandbox Code Playgroud)
从每个服务器通过电子邮件发送。
我尝试使用shell:
执行 grep 然后发送邮件:
shell: grep '^`date +"%b %d"`' /var/log/yum.log | mail -s "updates applied to `hostname -s` today" updatereports@mydomain.com
Run Code Online (Sandbox Code Playgroud)
它只是发送一封空白电子邮件。
还尝试使用邮件功能,但我正在努力将多行变量转储到邮件正文中:
- name: test result
ignore_errors: yes
shell: grep "`date '+%b %d'`" /var/log/messages
register: updated
- name: mail result
mail:
to: updatereports@mydomain.com
subject: "updates applied to {{ ansible_hostname }} today"
body: "{{ item }}"
with_items: "{{ updated.results|map(attribute='stdout_lines')|list }}"
when: updated.stdout
Run Code Online (Sandbox Code Playgroud)
它也发送,但打印时间戳,然后为 yum.log 中的每个匹配行生成一行错误:
['Sep 12 16:15:28 host-ng ansible-command: Invoked with warn=True executable=None _uses_shell=True _raw_params=grep "`date \'+%b %d\'`" /var/log/messages | tail removes=None creates=None chdir=None'
Run Code Online (Sandbox Code Playgroud)
我不确定这是否是您唯一的问题,但一个问题是您with_items
的缩进不正确。在with_items
属于任务,不是mail
。
- name: mail result
mail:
to: updatereports@mydomain.com
subject: "updates applied to {{ ansible_hostname }} today"
body: "{{ item }}"
with_items: "{{ updated.results|map(attribute='stdout_lines')|list }}"
when: updated.stdout
Run Code Online (Sandbox Code Playgroud)
不过,我不确定with_items
在这种情况下您是否完全需要。with_items
当您循环遍历某些东西的集合时,您需要使用它。
因为我不知道答案,如果我在你的位置上,我只会从一些简单的调试任务而不是邮件开始。一旦您看到调试结果,就应该更容易看到您需要做什么。
- name: mail result
debug:
msg: "{{ updated }}"
- name: mail result
debug:
msg: ""{{ updated.results|map(attribute='stdout_lines')|list }}""
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5984 次 |
最近记录: |