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

使用一个主机组中的事实配置另一个具有Ansible的主机组,ansible,ansible-facts,Ansible,Ansible Facts,我正在尝试使用另一组主机[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

我正在尝试使用另一组主机[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 [船长] kubernetes ansible_ssh_主机=10.2.23.108 [节点] n1 ansible_ssh_host=10.2.23.192 n2 ansible_ssh_主机=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 --- -名称:配置公用 主机:节点 sudo:是的 任务: -名称:etcd端点 文件:dest=/etc/kubernetes state=目录 -名称:etcd端点 模板:src=files/k.j2 dest=/etc/kubernetes/apiserver 最后,这里是文件/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=“{%for hostin group['ETCD']%}https://{{hostvars[host]['ansible_eth0'][“ipv4”][“address”]}}:2380{%if not loop.last%},{%endif%}{%endfor%}” 目标是产生一个KUBE_ETCD_服务器值

--etcd_servers=https://10.2.23.11:2380,https://10.2.23.10:2380,https://10.2.23.10:2380 --etcd_服务器=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端点]********************************************************** 致命:[n1]:失败!=>{“更改”:false,“失败”:true,“消息”:“AnsibleUndefinedVariable:'dict object'没有属性'ansible_eth0'” 致命:[n2]:失败!=>{“更改”:false,“失败”:true,“消息”:“AnsibleUndefinedVariable:'dict object'没有属性'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

你能对其中一台服务器运行
安装程序
模块并粘贴输出吗?可能是它没有
ansible_eth0
可能etcd上没有
eth0
。或者,有一次我在将iptables更改为firewalld时出现同样的错误(一些依赖的包被删除为“未使用”)谢谢。这是在etcd服务器上运行的安装模块:.eth0确实存在。我应该包括我的Ansible版本:$Ansible--version Ansible 2.1.1.0配置文件=/Users/mpetrovic/Projects/ae6rt/Ansible other hosts/Ansible.cfg configured module search path=默认值w/o OverridesWorks。非常感谢。Followup问题:当我在etcd主机组上循环时,我希望能够在模板中使用etcd服务器的名称,因为该名称出现在清单主机文件中。也就是说,在模板中,我如何引用服务器,以便能够处理返回的值“etcd01”、“etcd02”和“etcd03”“?我刚刚计算出来,很明显:对于这个表达式,我需要的是主机值本身:{%forhost-in-groups['etcd']%}https://{{host}}:2379{%if-not-loop.last%},{%endif%}{%endfor%}