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

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

本文介绍了使用一个主机组中的事实使用Ansible配置另一个主机组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用另一组主机 [etcd] 中的事实来配置一组主机 [nodes] .这是我的主机文件

I am trying to configure one set of hosts [nodes] using facts from another set of hosts [etcd]. 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

请注意,组 [etcd] 不是配置的目标- [nodes] 是.但是配置 [节点] 需要了解 [etcd] 的事实.

Note that the group [etcd] is not the target of provisioning - [nodes] is. But provisioning [nodes] requires knowledge of the facts of [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的模板

Finally, here is the template for 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的值

The goal is to produce a KUBE_ETCD_SERVERS value that looks like


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

使etcd事实可用于节点播放的惯用的Ansible方法是什么?

What is the idiomatic Ansible way to make the etcd facts available to the node play?

推荐答案

如果要使用某些主机的事实,应首先收集它们.
[etcd] 主机上运行setup任务以填充hostvars.

If you want to use facts of some host, you should gather them first.
Run setup task on [etcd] hosts to populate 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

这篇关于使用一个主机组中的事实使用Ansible配置另一个主机组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 03:56