Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 如何在Ansible中通过通配符标识的主机上循环_Loops_Ansible_Syntax Error_Wildcard_Hosts - Fatal编程技术网

Loops 如何在Ansible中通过通配符标识的主机上循环

Loops 如何在Ansible中通过通配符标识的主机上循环,loops,ansible,syntax-error,wildcard,hosts,Loops,Ansible,Syntax Error,Wildcard,Hosts,下面是我的剧本,效果很好 --- - hosts: "PROD_SERVERS" user: "{{USER}}" tasks: - name: Check if all hosts are reachable fail: msg: "Server is UNREACHABLE." when: "hostvars[item].ansible_fa

下面是我的剧本,效果很好

---
- hosts: "PROD_SERVERS"
  user: "{{USER}}"
  tasks:

       - name: Check if all hosts are reachable
         fail:
           msg: "Server is UNREACHABLE."
         when: "hostvars[item].ansible_facts|list|length == 0"
         with_items: "{{ groups.PROD_SERVERS }}"
但是,当主机以通配符形式显示时,即
{{{ENV}}{uu*
,您能帮助我了解语法吗

---
- hosts: "{{ENV}}_*"
  user: "{{USER}}"
  tasks:

       - name: Check if all hosts are reachable
         fail:
           msg: "Server is UNREACHABLE."
         when: "hostvars[item].ansible_facts|list|length == 0"
         with_items: "{{ groups.{{ENV}}_* }}"   <------- Need help with this part of the code
---
-主机:“{{ENV}}}
用户:{{user}}
任务:
-名称:检查是否可以访问所有主机
失败:
msg:“无法访问服务器。”
当:“hostvars[item].ansible_facts | list | length==0”
_项:“{groups.{{ENV}}}{u*}}”也被称为“魔术变量”,用于识别当前播放中的主机。它们是:

  • ansible\u play\u主机
  • ansible\u play\u batch
您可以使用其中一个变量来循环,而不是使用库存组名称

ansible\u play\u主机的示例

-失败:
msg:“无法访问服务器。”
当:hostvars[item].ansible_facts | list | length==0
带_项:“{{ansible_play_hosts}”
更新:

更改了Ansible版本“2.4.x”的示例,循环应使用
和_items
执行,而不是
循环
。引用

我们在Ansible 2.5中添加了
循环
。它还不是用
完全替代
,但我们建议在大多数用例中使用它


注意:对于Ansible“2.9.16”和“2.10.2”
循环同样有效。

我在尝试您的建议时遇到以下错误:
任务[检查是否所有主机都可访问]******************************************致命:[usdfw33as75v.mrshmc.com]:失败!=>{“msg”:“在可用的查找插件中查找名为{{ansible_play_hosts}}的查找时意外失败”
我尝试了
collect_facts:true
以及删除
collect_facts
{ansible_play_hosts}}
但我在查找名为
的查找时一直遇到意外失败。答案没有问题。根据文档,这对我有效,我已尝试使用Ansible版本“2.10.2”和“2.9.16”。试着查看插件列表。
ansible doc-t lookup-l
。我应该查找哪个插件才能工作?我的ansible版本是
2.4.2.0
不确定是哪个插件,但
目录\u主机名
似乎相关。也可能是由于ansible版本。这个
var
debug
是否给出了任何output?