问题描述
在 Ansible 2.7.11 中并想使用模块 waif_for 与 loops 一起用于连接测试到我的 System Center Operations Manager (SCOM) 服务器.
In Ansible 2.7.11 and wanted to use the module waif_for together with loops for connection test to my System Center Operations Manager (SCOM) servers.
目前我正在使用一个任务
Currently I'm using a task
- name: "Test connection to SCOM_MGMT_SRV_PROD: {{ SCOM_MGMT_SRV_PROD }}"
wait_for:
host: "{{ item }}"
port: "{{ SCOM_PORT }}"
state: drained # Port should be open
delay: 0 # No wait before first check (sec)
timeout: 3 # Stop checking after timeout (sec)
active_connection_states: SYN_RECV
with_items:
- server1
- server2
- server3
- server4
ignore_errors: yes
tags: connectionTest,testSCOM
其中变量 SCOM_PORT
设置为 1270
,SCOM_MGMT_SRV_PROD
设置为服务器列表 "server1,server2,server3,server4"
.
where the variable SCOM_PORT
is set to 1270
and SCOM_MGMT_SRV_PROD
to a list of servers "server1,server2,server3,server4"
.
这种方法有效,但我希望在一个中心位置维护一个服务器变量列表,就像库存文件中的全局变量 SCOM_MGMT_SRV_PROD
.
This approach is working but I wanted to have a variable list of servers maintained at a central place, like a global variable SCOM_MGMT_SRV_PROD
in inventory file.
可以通过
- debug:
msg="{{ item }}"
loop: "{{ [SCOM_MGMT_SRV_PROD] }}"
但是在任务中使用这种方法时
but when using this approach in the task
- name: "Test connection to SCOM_MGMT_SRV_PROD: {{ SCOM_MGMT_SRV_PROD }}"
wait_for:
host: "{{ item }}"
port: "{{ SCOM_PORT }}"
state: drained # Port should be open
delay: 0 # No wait before first check (sec)
timeout: 3 # Stop checking after timeout (sec)
active_connection_states: SYN_RECV
loop: "{{ [SCOM_MGMT_SRV_PROD] }}"
ignore_errors: yes
tags: connectionTest,testSCOM
我收到一个错误
failed: [host] (item=server1,server2,server3,server4) => ... /tmp/ansible_wait_for_payload_zNj2ac/__main__.py\", line 380, in _convert_host_to_hex\r\n File \"/tmp/ansible_wait_for_payload_zNj2ac/__main__.py\", line 354, in _convert_host_to_ip\r\nsocket.gaierror: [Errno -2] Name or service not known\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
这对我来说表明模块 wait_for
将完整列表作为项目而不是列表中的一台服务器.所描述行为的原因似乎得到了回答@在特定主机组中检查 Ansible 中的 wait_for 不起作用.
which for me indicates that the module wait_for
got the full list as item instead of one server from the list. The reason for the described behavior seems to be answered @Checking wait_for in Ansible in a particular host group does not work.
知道如何让 wait_for
模块与服务器的变量列表一起工作吗?
Any idea how to get the module wait_for
working with a variable list of servers?
其他组件
- RHEL 7.6
python 版本 = 2.7.5(默认,2019 年 6 月 11 日,12:19:05)[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
- 模块 dig 已安装,但未使用它不会与 lookup 一起改变行为
- RHEL 7.6
python version = 2.7.5 (default, Jun 11 2019, 12:19:05) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
- Module dig is installed, but not used since it doesn't change the behavior together with lookup
推荐答案
与同事协商后发现清单中的(服务器)列表没有正确定义,只是一个经典的语法错误.请参阅Ansible,如何在主机清单中定义列表?.
After consulting with colleagues it turned out that the list (of servers) wasn't defined correctly in inventory, just a classical syntax error. Please see Ansible, how to define a list in host inventory?.
要使其工作,必须在清单文件中将变量定义为
To get it working it is necessary to define in inventory file the variable as
SCOM_MGMT_SRV_PROD="['server1','server2','server3','server4']"
而不是逗号分隔的字符串列表并使用 for 循环
instead of an just comma separated list of strings and use for loop
loop: "{{ SCOM_MGMT_SRV_PROD }}"
通过这样做,我意识到实际上调试消息也没有遍历列表.现在它工作正常
By doing this I've realized that actually the debug message wasn't iterating over the list either. Now it is working properly
TASK [debug] ************************************************************************************************************************
ok: [host] => (item=server1) => {
"msg": "server1"
}
ok: [host] => (item=server2) => {
"msg": "server2"
}
ok: [host] => (item=server3) => {
"msg": "server3"
}
ok: [host] => (item=server4) => {
"msg": "server4"
}
TASK [Test connection to SCOM_MGMT_SRV_PROD: ['server1','server2','server3','server4']] ***
ok: [host] => (item=server1)
ok: [host] => (item=server2)
ok: [host] => (item=server3)
ok: [host] => (item=server4)
这篇关于如何将 Ansible 模块 wait_for 与循环一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!