大网数据广告活动
服务器运维问答

Ansible yum update then email me the results

· 52CE 我爱测

Writing a playbook to perform yum updates and then get an email from each server. I'd like the email to contain the changed contents of yum.log. IOW, I want the results of: grep [today's date] /var/log/yum.log to be emailed from …

问题原文

原帖链接 · Mike · CC BY-SA 3.0

Writing a playbook to perform yum updates and then get an email from each server. I'd like the email to contain the changed contents of yum.log.

IOW, I want the results of:

grep [today's date] /var/log/yum.log

to be emailed from each server.

I tried using shell: to perform the grep then send the mail :

    shell: grep '^`date +"%b %d"`' /var/log/yum.log | mail -s "updates applied to `hostname -s` today" [email omitted]

It just sends a blank email.

Also tried using the mail function but am struggling to dump a multi-line variable into the body of the message:

- name: test result
  ignore_errors: yes
  shell: grep "`date '+%b %d'`" /var/log/messages
  register: updated

- name: mail result
  mail:
    to: [email omitted]
    subject: "updates applied to {{ ansible_hostname }} today"
    body: "{{ item }}"
    with_items: "{{ updated.results|map(attribute='stdout_lines')|list }}"
  when: updated.stdout

It also sends, but prints the timestamp then generates a line of errors for each matched line in 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'

I found that fancy results|map code here but don't understand it enough to work without errors.

已采纳解答

原帖链接 · Zoredache · CC BY-SA 3.0

I am not certain if it is your only problem, but one problem is that your with_items is indented incorrectly. The with_items belongs to a task, not the mail.

- name: mail result
  mail:
    to: [email omitted]
    subject: "updates applied to {{ ansible_hostname }} today"
    body: "{{ item }}"
  with_items: "{{ updated.results|map(attribute='stdout_lines')|list }}"
  when: updated.stdout

I am not really sure if you need the with_items at all in this case though. You need to use with_items when you are looping over a collection of something.

Since I don't know the answer, if I where in your shoes I would simply start with some simple debug tasks instead of the mail. Once you see the results of the debug it should be a lot easier to see what you need to do.

- name: mail result
  debug:
    msg: "{{ updated }}"
- name: mail result
  debug:
    msg: ""{{ updated.results|map(attribute='stdout_lines')|list }}""

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-09-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。