Ansible YAML Inventory
At this point we have a fairly useful setup. We can take fresh servers and get them into some sort of shape that meets our basic needs. We aren't quite production ready just yet, but we're on our way.
Along the way we've accumulated some areas of our configuration that could be improved.
You don't need to do any of this. But you might want to. I do, and I think it's useful to know about.
As it stands at the moment we have a file called
production which details all our production inventory. This file tells Ansible all about our servers, what names they have, what IP address should be used to connect, what groups we want to associate these servers with, and so on.
rk8s-lb-1 ansible_host=188.8.131.52 ansible_python_interpreter=/usr/bin/python3 rk8s-node-1 ansible_host=184.108.40.206 ansible_python_interpreter=/usr/bin/python3 rk8s-node-2 ansible_host=220.127.116.11 ansible_python_interpreter=/usr/bin/python3 [rancher-2-kubernetes-nodes] rk8s-node-1 rk8s-node-2 [rancher-2-load-balancers] rk8s-lb-1
For me, this is starting to get a little long winded.
As it stands we repeat
ansible_python_interpreter every time we define a new host entry, and then we're going to have various bits of custom data added, and over time the lines will become long and hard to read.
We can remove the
ansible_python_interpreter entry. Given that at the moment, all of our hosts use Python 3, I'm going to move this entry into
But before I do, I'm going to switch to YAML syntax for all my configuration and inventories.
I prefer the syntax highlighting my editor gives me when working with
yml files, over the plain text / ini files.
This comes at the expense of verbosity, as we shall soon see.
And into this file, add the line:
--- ansible_python_interpreter: "/usr/bin/python3"
So far, so good.
Then the same for
mv production production.yml
And this file needs totally changing:
--- all: hosts: rk8s-lb-1: ansible_host: 18.104.22.168 rk8s-node-1: ansible_host: 22.214.171.124 rk8s-node-2: ansible_host: 126.96.36.199 children: rancher-2-kubernetes-nodes: hosts: rk8s-node-1: rk8s-node-2: rancher-2-load-balancers: hosts: rk8s-lb-1:
Ok, the weird parts are
children. And there's another top level entry that I haven't used:
Working with this stuff is fairly intuitive after a while. You just have to get tactile - type it out.
Groups of Groups
We have the concept of
rancher-2-kubernetes-nodes, which we know will be any node provisioned for use inside our Rancher 2 Kubernetes cluster.
Think of this as a generic, catch all approach to node management.
We also need three sub groups. We've touch on these already:
These are more specific types of nodes. We should model this concept in our inventory. And to do that, we can use groups of groups:
--- all: hosts: loadbalancer: ansible_host: 188.8.131.52 etcd-0: ansible_host: 184.108.40.206 etcd-1: ansible_host: 220.127.116.11 controlplane-0: ansible_host: 18.104.22.168 controlplane-1: ansible_host: 22.214.171.124 worker-0: ansible_host: 126.96.36.199 worker-1: ansible_host: 188.8.131.52 children: rancher-2-kubernetes-nodes: children: etcd: hosts: etcd-0: etcd-1: controlplane: hosts: controlplane-0: controlplane-1: worker: hosts: worker-0: worker-1: rancher-2-load-balancers: hosts: loadbalancer:
It doesn't matter if you use additional lines between the blocks. I've shown two variations here. I think the
all.hosts block is easier to read with additional line breaks between the groups. Indentation is what matters.
Lastly we need to update our
Makefile to use the new
run_playbook: @docker run --rm \ -v $(CURDIR):/crv-ansible \ -v ~/.ssh/id_rsa:/root/.ssh/id_rsa \ -v ~/.ssh/id_rsa.pub:/root/.ssh/id_rsa.pub \ -w /crv-ansible \ williamyeh/ansible:alpine3 \ - ansible-playbook -i production site.yml $(cmd) + ansible-playbook -i production.yml site.yml $(cmd)
At this stage we have cleaned up our configuration, and learned a few extra useful techniques along the way.
In the next Rancher 2 / Kubernetes tutorial video we will move on to installing Kubernetes with Rancher Kubernetes Engine (RKE).