3
votes

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?

1
can you run the setup module against one of the server and paste the output. May be it doesn't have ansible_eth0Arbab Nazar
Possible there's no eth0 on etcd. Or, once time i've become same error when changing iptables to firewalld (some depended packages were removed as 'unused')A K
Thanks. Here is the setup module run against an etcd server: gist.github.com/ae6rt/44f2567e287dd502e714b811bcd0ba92. eth0 does in fact exist.ae6rt
I should have included my Ansible version: $ ansible --version ansible 2.1.1.0 config file = /Users/mpetrovic/Projects/ae6rt/ansible-other-hosts/ansible.cfg configured module search path = Default w/o overridesae6rt

1 Answers

4
votes

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