我正在尝试使用另一组主机 [etcd] 中的事实来配置一组主机 [nodes] 。这是我的主机文件
[master] kubernetes ansible_ssh_host=10.2.23.108 [nodes] n1 ansible_ssh_host=10.2.23.192 n2 ansible_ssh_host=10.2.23.47 [etcd] etcd01 ansible_ssh_host=10.2.23.11 etcd02 ansible_ssh_host=10.2.23.10 etcd03 ansible_ssh_host=10.2.23.9
请注意, [etcd] 组不是配置的目标 - [nodes] 。但是配置 [nodes] 需要了解 [etcd] 的事实。
这是我的剧本:
--- - name: Configure common hosts: nodes sudo: True tasks: - name: etcd endpoints file: dest=/etc/kubernetes state=directory - name: etcd endpoints template: src=files/k.j2 dest=/etc/kubernetes/apiserver
最后,这是files / k.j2的模板
KUBE_ETCD_SERVERS="--etcd_servers="{% for host in groups['etcd'] %}https://{{hostvars[host]['ansible_eth0']["ipv4"]["address"]}}:2380{% if not loop.last %},{% endif %}{% endfor %}"
目标是生成类似于
的KUBE_ETCD_SERVERS值--etcd_servers=https://10.2.23.11:2380,https://10.2.23.10:2380,https://10.2.23.10:2380
当我运行这个剧本时,我得到了控制台输出
TASK [etcd endpoints] ********************************************************** fatal: [n1]: FAILED! => {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_eth0'"} fatal: [n2]: FAILED! => {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_eth0'"}
使节点播放可以使用etd事实的惯用Ansible方法是什么?
答案 0 :(得分:3)
如果你想使用某些主持人的事实,你应该先收集它们
在 [etcd] 主机上运行setup
任务以填充hostvars
。
---
- name: Gather etcd facts
hosts: etcd
tasks:
- setup:
- name: Configure common
hosts: nodes
sudo: True
tasks:
- name: etcd endpoints
file: dest=/etc/kubernetes state=directory
- name: etcd endpoints
template: src=files/k.j2 dest=/etc/kubernetes/apiserver