[英]Using facts from one host group to configure another host group with Ansible
我正在嘗試使用另一組主機[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'"}
使etcd事實可用於節點播放的慣用Ansible方法是什么?
如果要使用某些主機的事實,則應首先收集它們。
在[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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.