无法访问 Ansible 中的模板组地址

无法访问 Ansible 中的模板组地址

我正在尝试枚举我的eth0地址(设置为默认值,因为我在某处读到 eth0 并非始终可用...)。在我的模板中,我尝试从组中调用它,如下所示...

[Unit]
Description=Kubernetes Kube Proxy
Documentation=https://github.com/GoogleCloudPlatform/kubernetes

[Service]
ExecStart=/usr/bin/kube-proxy \
  --master=https://{{ groups[controller][0]['ansible_default_ipv4']['address'] }}:6443 \
  --kubeconfig=/var/lib/kubelet/kubeconfig \
  --proxy-mode=iptables \
  --v=2

Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

这看起来应该可用,因为当我转储时-debug var=groups我得到了......

[18.234.92.196] => {
    "groups": {

      ......

        "controller": [
            "54.86.253.135",
            "52.205.43.24",
            "54.210.213.10"
        ],
        "controller.0": [
            "54.86.253.135"
        ],
        "controller.1": [
            "52.205.43.24"
        ],
        "controller.2": [
            "54.210.213.10"
        ],

        ......

        "name_controller-0": [
            "54.86.253.135"
        ],
        "name_controller-1": [
            "52.205.43.24"
        ],
        "name_controller-2": [
            "54.210.213.10"
        ],

        ......

    }
}

奇怪的是,当我尝试调试时,var=groups[controller]我得到了......

TASK [worker : debug] *******************************************************************************************************************************************
ok: [34.207.172.100] => {
    "groups[controller]": "VARIABLE IS NOT DEFINED!: 'controller' is undefined"
}

.....

TASK [worker : Add Kubelet configuration] ***********************************************************************************************************************
fatal: [34.207.172.100]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'controller' is undefined"}

我在这里遗漏了什么?

答案1

弄清楚了,需要先通过组指定 IP,然后将其用作索引。

此外,ansible_default_ipv4似乎不存在,所以换成public_ip是同一个东西。

- name: Print hostvars
  debug:
    msg: "{{ hostvars[groups.controller.0].public_ip }}"

相关内容