简体   繁体   English

使用一个主机组中的事实使用Ansible配置另一个主机组

[英]Using facts from one host group to configure another host group with Ansible

I am trying to configure one set of hosts [nodes] using facts from another set of hosts [etcd] . 我正在尝试使用另一组主机[etcd]中的事实来配置一组主机[nodes ] Here is my hosts file 这是我的主机文件

[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

Note that the group [etcd] is not the target of provisioning - [nodes] is. 请注意,组[etcd]不是供应的目标- [nodes]是。 But provisioning [nodes] requires knowledge of the facts of [etcd] . 但是供应[nodes]需要[etcd]的事实的知识。

Here is my playbook: 这是我的剧本:

---
- 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

Finally, here is the template for files/k.j2 最后,这是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 %}"

The goal is to produce a KUBE_ETCD_SERVERS value that looks like 目标是产生一个看起来像KUBE_ETCD_SERVERS的值

--etcd_servers=https://10.2.23.11:2380,https://10.2.23.10:2380,https://10.2.23.10:2380

When I run this playbook I get console output 当我运行此剧本时,会获得控制台输出

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'"}

What is the idiomatic Ansible way to make the etcd facts available to the node play? 使etcd事实可用于节点播放的惯用Ansible方法是什么?

If you want to use facts of some host, you should gather them first. 如果要使用某些主机的事实,则应首先收集它们。
Run setup task on [etcd] hosts to populate hostvars . [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