Ansible主机的库存yml不支持重复ip with_items

问题描述 投票:1回答:2

下列:

  store_controller:
    hosts:
      SERVER:
        ansible_host: "{{ STORE_CTL }}"
        mgmt_ip: "{{ ansible_host }}"
  global_mgmt:
    hosts:
      SERVER:
        ansible_host: "{{ NOMAD_SERVER }}"
        mgmt_ip: "{{ ansible_host }}"
  node_exporter: ##if comment the part from here to end, it's ok####
    hosts:
      "{{ item }}":
        ansible_host: "{{ item }}"
        mgmt_ip: "{{ ansible_host }}"
    with_items:
      - "172.7.7.1"
      - "172.7.7.9"
      - "172.7.7.12"

但Ansible不允许我在这里使用'with_items'。似乎ansible不支持主机上的迭代器。

如何在组node_exporter中为我的三个IP定义hosts数组?

ansible yaml
2个回答
1
投票

您需要定义的是:

node_exporter:
  hosts:
    172.7.7.1:
    172.7.7.9:
    172.7.7.12:
  vars: 
    mgmt_ip: "{{ inventory_hostname }}"

说明:

mgmt_ip仅定义了一个模板(字符串值),该模板将在使用时解析。

对于每个目标机器,inventory_hostname(因此mgmt_ip)将解析为当前正在执行的主机的IP地址。

使用ansible_host为库存主机名分配相同的值是一个空操作,因此您根本不需要它。

我不认为它给代码带来任何价值/清晰度,但是既然你评论它对你有用,那就是用多个主机实现它的方法。所有你实现的是创建别名mgmt_ipinventory_hostname


关于问题的前提:

用于YAML的with_items:是字典键名(字符串值)。是Ansible可能会也可能不会使用它。

它在任务中指定时使用它(它具有语义含义)。

否则它会忽略它(从不使用此密钥),或报告错误。


-1
投票

快速查看代码,您会遇到缩进问题。尝试在“{{item}}之前添加2个空格:”

© www.soinside.com 2019 - 2024. All rights reserved.