簡體   English   中英

Ansible 主機組和過濾 IP 的問題

[英]Problem with Ansible host groups and filtering IPs

我在遍歷 Ansible 組中的主機列表時遇到問題,我得到了我想要的值,但我得到了虛假錯誤,我無法弄清楚。 本來以為如果有錯誤它根本不應該打印這些值:

fatal: [worker01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_eth1'\n\nThe error appears to be in 'roles/vagrant/tasks/main.yml': line 15, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug:\n  ^ here\n"}

從 vagrant 文件中提取

ansible.groups = {
            "workers" => ["worker01", "worker02", "worker03"],
            "controllers" => ["controller01", "controller02"],
            "kubernetes" => ["kubernetes"],
            "all_groups:children" => ["workers", "controllers", "kubernetes"]
        }

vagrant 使用的示例主機定義

{
  "name": "worker03",
  "alias": "worker03",
  "box": "bento/ubuntu-18.04",
  "memory": 2048,
  "vcpu": 2,
  "provider": "virtualbox",
  "autostart": "false",
  "cpus": 2,
  "cpu_percentage": 100,
  "Controller": "SATA Controller",
  "lv_disks": 4,
  "pv_size": 5,
  "infra_ip_addr": "10.2.15.60",
  "service_ip_addr": "10.96.0.60",
  "pod_ip_addr": "192.168.0.60",
  "port_forwards": {},
  "project": "wks",
  "tags": "all",
  "group": "workers",
  "extra_vars": {}
}

以下代碼:

- debug:
    msg: "External: {{ hostvars[item]['ansible_eth1']['ipv4']['address'] }} ; Service: {{ hostvars[inventory_hostname]['ansible_eth3']['ipv4']['address'] }} "
  with_items:
    - "{{ groups['workers'] }}"

產生 Output - 注意錯誤“ansible.vars.hostvars.HostVarsVars 對象”沒有屬性“ansible_eth1”

ok: [worker01] => (item=worker01) => {
    "msg": "External: 10.2.15.40 ; Service: 192.168.0.40 "
}
fatal: [worker01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_eth1'\n\nThe error appears to be in 'roles/vagrant/tasks/main.yml': line 15, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug:\n  ^ here\n"}

PLAY RECAP ****************************************************************************************************************************************************************************************************************************
worker01                   : ok=3    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

在現代 Ubuntu 圖像上,接口名稱很愚蠢(例如enp0s31f6wlp0s20f3 ); 您想要的是使用ansible_default_ipv4而不是命名特定接口(或者,當然,您可以進一步遍歷所有發現的接口,但對於您正在做的事情,這似乎不是您想要的)

- debug:
    msg: "External: {{ hostvars[item]['ansible_default_ipv4']['address'] }} ; Service: {{ hostvars[inventory_hostname]['ansible_eth3']['ipv4']['address'] }} "
  with_items:
    - "{{ groups['workers'] }}"

雖然這將解決您的特定問題,但對您未來的調試幫助更一般的建議是使用debug: var=hostvars以查看可用的值,因為錯誤'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_eth1'不可能更明確地說明問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM