将主机变量写入ansible循环到文件中

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

我正在尝试建立一个配置文件,其中包含我的清单主机服务器及其字段的列表。 IP,FQDN等

这是我的清单文件的一部分:

ocp_cluster:
   hosts:
     anbootstrap.ocp.hattusas.tst:
         fqdn: anbootstrap.ocp.hattusas.tst
         ip: 10.72.217.92
     anmaster1.ocp.hattusas.tst:
         fqdn: anmaster1.ocp.hattusas.tst
         ip: 10.72.217.93
     anmaster2.ocp.hattusas.tst:
         fqdn: anmaster2.ocp.hattusas.tst
         ip: 10.72.217.94
     anmaster3.ocp.hattusas.tst:

这是我的剧本:

  - name: Adding OCP Clusters to DHCP configuration
    debug:
      "{{ hostvars[item][fqdn] }}"
    loop: "{{ groups['ocp_cluster'] }}"

((我将很快使用blockinfile)

[当我运行剧本时,出现了未定义的错误fqdn。我尝试使用for循环,但没有帮助。有什么建议么?非常感谢。

ansible ansible-inventory
1个回答
0
投票

固定任务在下面

- debug:
    msg: "{{ hostvars[item]['fqdn'] }}"
  loop: "{{ groups['ocp_cluster'] }}"
  • 缺少调试参数msg
  • fqdn是字典的属性,必须加引号。类似于ocp_cluster
  • 可以简化对字典属性的引用
- debug:
    msg: "{{ hostvars[item].fqdn }}"
  loop: "{{ groups.ocp_cluster }}"
© www.soinside.com 2019 - 2024. All rights reserved.