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

问题描述

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

[掌握]Kubernetes ansible_ssh_host=10.2.23.108[节点]n1 ansible_ssh_host=10.2.23.192n2 ansible_ssh_host=10.2.23.47[等]etcd01 ansible_ssh_host=10.2.23.11etcd02 ansible_ssh_host=10.2.23.10etcd03 ansible_ssh_host=10.2.23.9

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

这是我的剧本:

---- 名称:配置通用主机:节点须藤:真的任务:- 名称:etcd 端点文件:dest=/etc/kubernetes state=目录- 名称:etcd 端点模板:src=files/k.j2 dest=/etc/kubernetes/apiserver

最后,这里是 files/k.j2 的模板

KUBE_ETCD_SERVERS="--etcd_servers="{% for host in group['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

当我运行这个剧本时,我得到控制台输出

任务 [etcd 端点] **********************************************************致命:[n1]:失败!=> {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'dict object' 没有属性 'ansible_eth0'"}致命:[n2]:失败!=> {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'dict object' 没有属性 'ansible_eth0'"}

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

解决方案

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

---- 名称:收集 etcd 事实主机:etcd任务:- 设置:- 名称:配置通用主机:节点须藤:真的任务:- 名称:etcd 端点文件:dest=/etc/kubernetes state=目录- 名称:etcd 端点模板:src=files/k.j2 dest=/etc/kubernetes/apiserver

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

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

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

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

解决方案

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 配置另一个主机组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 20:45