无法通过模板获取组ipv4地址

时间:2018-11-08 17:27:28

标签: ansible ansible-facts ansible-template

我正在尝试枚举我的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 个答案:

答案 0 :(得分:0)

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

此外,ansible_default_ipv4似乎不存在,因此交换到public_ip也是一样。

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