我们有一个 Ansible 脚本,当服务器更新时会发送电子邮件。在脚本中,我想使用来自两个不同位置的变量作为发送地址。第一个位置来自库存本身,称为owner_email;第二个位置来自角色的vars部分,称为mb_addresses。在脚本中,我试图弄清楚如何设置它。相关部分如下:
- name: Send the report
mail:
host: our.mailserver.com
port: 25
to:
- "{{ owner_email }}"
- "{{ mb_addresses }}"
from: [email protected]
subject: Linux monthly patch results
body: "{{ lookup('file', '/depot/reports/patch/{{inventory_hostname}}_patch_report.txt') }}"
delegate_to: localhost
become: no
在 main.yml 的 vars 部分中,mb_address 设置如下:
mb_addresses: "[email protected],[email protected]"
问题在于我收到了第一封电子邮件,但第二封却被忽略了。
如果我这样设置,也会发生同样的情况:
mb_addresses: '"[email protected]" "[email protected]"'
我曾尝试过这样的:
mb_addresses:
- "[email protected]"
- "[email protected]"
当以这种方式完成时,不会拾取任何电子邮件。
我想避免的是进入每个库存项目并添加一个库存组来接收完成电子邮件。
我错过了什么?
答案1
给定变量(无论它们来自哪里)
owner_email: '"[email protected]"'
mb_addresses: '"[email protected]" "[email protected]"'
创建地址列表自行测试。例如,先测试一下
- debug:
var: to
vars:
to: "{{ [owner_email] + mb_addresses.split(' ') }}"
给出
to:
- '"[email protected]"'
- '"[email protected]"'
- '"[email protected]"'
例如在本地主机上测试
- mail:
host: localhost
port: 25
to: "{{ to }}"
from: Tower@localhost
subject: Linux monthly patch results
body: Some of the 160 vulnerabilities might have been patched.
vars:
owner_email: '"admin@localhost"'
mb_addresses: '"first.email@localhost" "second.email@localhost"'
to: "{{ [owner_email] + mb_addresses.split(' ') }}"
发送电子邮件
From: Tower@localhost
To: admin@localhost, first.email@localhost, second.email@localhost
Cc:
Subject: Linux monthly patch results
Date: Tue, 06 Sep 2022 03:05:52 +0200
X-Mailer: Ansible mail module
Some of the 160 vulnerabilities might have been patched.