Switch to dynamic ansible inventory

We don't need to install and use kargo cli anymore.
This commit is contained in:
Aleksandr Didenko 2016-07-08 17:08:09 +02:00
parent e89f4ac7ee
commit 8d3abdb489
4 changed files with 69 additions and 17 deletions

View file

@ -12,17 +12,11 @@ ansible --version || (
apt-get install -y ansible
)
# Kargo-cli
kargo --version || (
git clone https://github.com/kubespray/kargo-cli.git /root/kargo-cli
pushd /root/kargo-cli
python setup.py install
popd
)
# Copy/create nodes list
test -f ./nodes || cp /var/tmp/nodes ./nodes
# Either pull or copy microservices repos
cp -a /var/tmp/microservices* ./ccp/ || touch /var/tmp/ccp-download
# Pull kargo
git clone https://github.com/kubespray/kargo ~/kargo

View file

@ -1,6 +1,6 @@
#!/bin/bash
INVENTORY="/root/kargo/inventory/inventory.cfg"
INVENTORY="nodes_to_inv.py"
echo "Createing repository and CCP images, it may take a while..."
ansible-playbook -i $INVENTORY playbooks/ccp-build.yaml

View file

@ -1,6 +1,6 @@
#!/bin/bash
INVENTORY="/root/kargo/inventory/inventory.cfg"
INVENTORY="nodes_to_inv.py"
nodes=""
i=1
@ -9,13 +9,6 @@ for nodeip in `cat nodes` ; do
nodes+=" node${i}[ansible_ssh_host=${nodeip},ip=${nodeip}]"
done
if [ -f "$INVENTORY" ] ; then
echo "$INVENTORY already exists, if you want to recreate, pls remove it and re-run this script"
else
echo "Preparing inventory..."
kargo prepare -y --nodes $nodes
fi
echo "Running deployment..."
#kargo deploy -y --ansible-opts="-e @custom.yaml"
ansible-playbook -i $INVENTORY /root/kargo/cluster.yml -e @custom.yaml

65
nodes_to_inv.py Executable file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env python
# A simple dynamic replacemant of 'kargo prepare'
# Generates ansible inventory from a list of IPs in 'nodes' file.
import argparse
import json
def read_nodes_from_file(filename):
f = open(filename, 'r')
content = [x.strip('\n') for x in f.readlines()]
return content
def nodes_to_hash(nodes_list, masters):
nodes = {
'all': {
'hosts': [],
},
'etcd': {
'hosts': [],
},
'kube-master': {
'hosts': [],
},
'kube-node': {
'hosts': [],
},
'k8s-cluster': {
'children': ['kube-node', 'kube-master']
}
}
for node_ip in nodes_list:
nodes['all']['hosts'].append("%s" % node_ip)
nodes['kube-node']['hosts'].append("%s" % node_ip)
if i <= masters:
nodes['kube-master']['hosts'].append("%s" % node_ip)
if i <= 3:
nodes['etcd']['hosts'].append("%s" % node_ip)
return nodes
def main():
parser = argparse.ArgumentParser(description='Ansible dynamic inventory')
parser.add_argument('--nodes', help='File with list of nodes, one IP per line', default='nodes')
parser.add_argument('--masters', type=int, help='Number of master nodes, will be taken from the top of list', default=2)
parser.add_argument('--list', action='store_true')
parser.add_argument('--host', default=False)
args = parser.parse_args()
nodes_list = read_nodes_from_file(args.nodes)
if len(nodes_list) < 3:
print "Error: requires at least 3 nodes"
return
nodes = nodes_to_hash(nodes_list, args.masters)
if args.host:
print "{}"
else:
print json.dumps(nodes)
if __name__ == "__main__":
main()