将字典传递给子模块角色时未定义的变量



我的易感角色遇到了一个非常奇怪的问题。我向这样的子模块发送了两个字典:

import_role:
name: .submodules/monitoring-plugins
vars:
monitoring_plugins:
check_content:
command: "files/icinga/commands/check_content"
dest: "{{ icinga_server_plugin_directory }}"
group: "root"
owner: "root"
mode: "0755"
package: "curl"
src: "files/plugins/server/check_content"
check_http_response_time:
command: "files/icinga/commands/check_http_response_time"
dest: "{{ icinga_server_plugin_directory }}"
group: "root"
owner: "root"
mode: "0775"
src: "files/plugins/server/check_http_response_time"
check_https_response_time:
command: "files/icinga/commands/check_https_response_time"
dest: "{{ icinga_server_plugin_directory }}"
group: "root"
owner: "root"
mode: "0775"
src: "files/plugins/server/check_https_response_time"
check_port:
command: "files/icinga/commands/check_port"
dest: "{{ icinga_server_plugin_directory }}"
group: "root"
owner: "root"
mode: "0775"
src: "files/plugins/server/check_port"
check_ssl_cert:
command: "files/icinga/commands/check_ssl_cert"
dest: "{{ icinga_server_plugin_directory }}"
group: "root"
owner: "root"
mode: "0775"
src: "files/plugins/server/check_ssl_cert"
custom_services:
content:
service-preamble: 'apply Service "content"'
configuration: |
{{ common_service_header }}
assign where host.vars.ansible.system == "Linux"
definition:
"{{ lookup('template', 'config/services/content.conf') }}"
http_response_time:
service-preamble: 'apply Service "http_response_time"'
configuration: |
{{ common_service_header }}
assign where host.vars.ansible.system == "Linux"
definition:
"{{ lookup('template', 'config/services/http_response_time.conf') }}"
https_response_time:
service-preamble: 'apply Service "https-response-time"'
configuration: |
{{ common_service_header }}
assign where host.vars.ansible.system == "Linux"
definition:
"{{ lookup('template', 'config/services/https_response_time.conf') }}"
http_port:
service-preamble: 'apply Service "http-port"'
configuration: |
{{ common_service_header }}
assign where host.vars.ansible.system == "Linux"
definition:
"{{ lookup('template', 'config/services/http_port.conf') }}"
https_port:
service-preamble: 'apply Service "https-port"'
configuration: |
{{ common_service_header }}
assign where host.vars.ansible.system == "Linux"
definition:
"{{ lookup('template', 'config/services/https_port.conf') }}"
ssl_cert:
service-preamble: 'apply Service "ssl-cert"'
configuration: |
{{ common_service_header }}
assign where host.vars.ansible.system == "Linux"
definition:
"{{ lookup('template', 'config/services/ssl_cert.conf') }}"

在子模块上,我创建了两个调试任务:

- debug:
var: monitoring_plugins
- debug:
var: custom_services

输出:

ok: [server.test] => {
"monitoring_plugins": {
"check_content": {
"command": "files/icinga/commands/check_content",
"dest": "/usr/lib/nagios/plugins",
"group": "root",
"mode": "0755",
"owner": "root",
"package": "curl",
"src": "files/plugins/server/check_content"
},
"check_http_response_time": {
"command": "files/icinga/commands/check_http_response_time",
"dest": "/usr/lib/nagios/plugins",
"group": "root",
"mode": "0775",
"owner": "root",
"src": "files/plugins/server/check_http_response_time"
},
"check_https_response_time": {
"command": "files/icinga/commands/check_https_response_time",
"dest": "/usr/lib/nagios/plugins",
"group": "root",
"mode": "0775",
"owner": "root",
"src": "files/plugins/server/check_https_response_time"
},
"check_port": {
"command": "files/icinga/commands/check_port",
"dest": "/usr/lib/nagios/plugins",
"group": "root",
"mode": "0775",
"owner": "root",
"src": "files/plugins/server/check_port"
},
"check_ssl_cert": {
"command": "files/icinga/commands/check_ssl_cert",
"dest": "/usr/lib/nagios/plugins",
"group": "root",
"mode": "0775",
"owner": "root",
"src": "files/plugins/server/check_ssl_cert"
}
}
}
TASK [.submodules/monitoring-plugins : debug] ************************************
ok: [server.test] => {
"custom_services": "VARIABLE IS NOT DEFINED!"
}

有人知道可能出什么问题吗?我从同一个任务中派出了他们两个,但其中一个看起来很好,另一个则不然。我甚至试图删除子模块并再次添加它,但仍然不起作用。任何其他建议都将不胜感激!

背景

包含未定义的其他变量扩展的变量不能被解释,并且是未定义的。

$ ansible localhost -e undef="{{ i_dont_exist }}" -m debug -a var=undef
localhost | SUCCESS => {
"undef": "VARIABLE IS NOT DEFINED!"
}

要查看出了什么问题,您需要在调试时实际扩展var

$ ansible localhost -e undef="{{ i_dont_exist }}" -m debug -a msg="{{ undef }}"
localhost | FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'i_dont_exist' is undefined"
}

您的具体案例

根本原因是common_service_header未定义。由于它是在角色include的变量定义中使用的,ansible试图解释它,但失败了,var保持未定义状态。

如果取消对icinga_server_plugin_directory的定义,则实际上对于monitoring_plugins会看到相同的结果。

为了获得更多关于实际发生的事情的信息;技巧";是强制ansible在调试中解释变量。有两种可能的方法:

- name: Use a simple jinja2 expansion
debug:
msg: "{{ custom_services }}"
- name: Use a the 'vars' lookup
debug:
msg: "{{ lookup('vars', 'custom_services') }}"

然后给出一个更有意义的信息(为了易读而重新格式化…(:

TASK [.submodules/monitoring-plugins : debug] *****************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an
undefined variable. The error was: 'common_service_header' is undefinednn
The error appears to be in 
'/some/path/.submodules/monitoring-plugins/tasks/main.yml': line 4, column 3, 
but maynbe elsewhere in the file depending on the exact syntax problem.nn
The offending line appears to be:nn    
msg: "{{ lookup('vars', 'monitoring_plugins') }}"n- debug:n  ^ heren"}

最新更新