Ansible 从特定组的易变库存中获取变量

Ansible 从特定组的易变库存中获取变量,ansible,Ansible,我试图从特定的组中获取特定的变量,但这似乎会导致奇怪的结果 主机文件示例: [server_machine] zeus account=app1 [agent_machine] machine1 account=agent port_a=1000 port_b=1001 machine2 account=agent port_a=1200 port_b=1201 machine3 account=agent port_a=1300 port_b=1301 我

我试图从特定的组中获取特定的变量,但这似乎会导致奇怪的结果

主机文件示例:

[server_machine]
zeus     account=app1

[agent_machine]

machine1   account=agent port_a=1000  port_b=1001 
machine2   account=agent port_a=1200  port_b=1201 
machine3   account=agent port_a=1300  port_b=1301 
我要做的是在
server\u机器
组上运行脚本,使用从
agent\u机器
组获得的参数

这样,脚本将在所有端口组合的服务器上运行3次

所以基本上我需要的剧本可能是这样的:

- hosts: server_machine

- tasks:
    - command: test.py --port_1 {{item}}  --port_2 {{item}}
      with_items:{{group.agent_machine.port_a, group.agent_machine.port_b }}
但是,我不能让它工作。

试试这个:

- command: test.py --port_1 {{ hostvars[item]["port_a"] }} --port_2 {{ hostvars[item]["port_b"] }}
  with_items: groups["agent_machine"]

这是一个很好的用例:


我还是不能去上班。我得到了以下错误:TASK:[shell echo一个端口名为{{hostvars[item][“port_a”]}]******致命:[vl-jd-ctm01]=>with_items需要一个列表或集合,通过调试任务尝试输出
组[“agent_machine”]
。理论上,它应该包含一个主机列表,如清单中所定义的。太棒了。作品我的错误是组名。非常感谢你。
  - hosts: agent_machine
    tasks:
      - command: test.py --port_1 {{port_a}}  --port_2 {{port_b}}
        delegate_to: zeus