簡體   English   中英

使用一個主機組中的事實使用Ansible配置另一個主機組

[英]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.

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