Unverified Commit db0ecb25 authored by Ioannis Tsafaras's avatar Ioannis Tsafaras Committed by Avraam Tsantekidis
Browse files

Refactor host and group vars in inventory, correct implementation of ssh key,...

Refactor host and group vars in inventory, correct implementation of ssh key, correct http proxy configuration host_var
parent 6cc262ae
...@@ -6,6 +6,6 @@ ...@@ -6,6 +6,6 @@
- name: Configure squid http proxy - name: Configure squid http proxy
lineinfile: destfile="/etc/squid3/squid.conf" regexp="^acl localnet" insertafter="^#acl localnet" lineinfile: destfile="/etc/squid3/squid.conf" regexp="^acl localnet" insertafter="^#acl localnet"
line="acl localnet src {{ hostvars[master-node]["local_net"] }}" line="acl localnet src {{ hostvars[groups['all'][0]]['local_net'] }}"
state=present state=present
tags: config tags: config
...@@ -4,4 +4,4 @@ Host bastion ...@@ -4,4 +4,4 @@ Host bastion
ProxyCommand none ProxyCommand none
Host * Host *
ProxyCommand ssh root@snf-xxxxxx.vm.okeanos.grnet.gr nc %h %p ProxyCommand ssh -i /tmp/tmpxxxxxx -W %h:%p root@snf-xxxxxx.vm.okeanos.grnet.gr
import os
import tempfile import tempfile
import ansible import ansible
from ansible.playbook import PlayBook from ansible.playbook import PlayBook
from ansible import callbacks from ansible import callbacks
from ansible import utils from ansible import utils
class Manager: class Manager:
def __init__(self, provisioner_response): def __init__(self, provisioner_response):
...@@ -18,13 +20,15 @@ class Manager: ...@@ -18,13 +20,15 @@ class Manager:
'ip': response['internal_ip']}) 'ip': response['internal_ip']})
self.cidr = provisioner_response['subnet']['cidr'] self.cidr = provisioner_response['subnet']['cidr']
self.kf = tempfile.NamedTemporaryFile() with tempfile.NamedTemporaryFile(mode='w', delete=False) as kf:
self.kf.write(provisioner_response['pk']) kf.write(provisioner_response['pk'])
self.temp_file = kf.name
ansible.constants.ANSIBLE_SSH_ARGS = '-o "ProxyCommand ssh -A -W %%h:%%p root@%s" -i %s' \ print self.temp_file
% self.inventory['master']['name'] + 'vm.okeanos.grnet.gr' %self.kf.name ansible.constants.ANSIBLE_SSH_ARGS = '-o "ProxyCommand ssh -i %s -o StrictHostKeyChecking=no -W %%h:%%p root@%s.vm.okeanos.grnet.gr"' \
# ansible.constants.ANSIBLE_SSH_ARGS = '-o "ProxyCommand ssh root@%s nc %%h %%p"' \ % (self.temp_file, self.inventory['master']['name'])
# % self.inventory["master"]["name"] + "vm.okeanos.grnet.gr" # ansible.constants.ANSIBLE_SSH_ARGS = '-o "ProxyCommand ssh root@%s.vm.okeanos.grnet.gr nc %%h %%p"' \
# % self.inventory['master']['name']
# ansible.constants.DEFAULT_PRIVATE_KEY_FILE = self.temp_file
ansible.constants.HOST_KEY_CHECKING = False ansible.constants.HOST_KEY_CHECKING = False
ansible.constants.DEFAULT_GATHERING = 'explicit' ansible.constants.DEFAULT_GATHERING = 'explicit'
...@@ -35,26 +39,31 @@ class Manager: ...@@ -35,26 +39,31 @@ class Manager:
""" """
inventory_groups = [] inventory_groups = []
host_vars = {}
all_group = ansible.inventory.group.Group(name="all")
all_group.set_variable('local_net', self.cidr)
inventory_groups.append(all_group)
master_group = ansible.inventory.group.Group(name='master') master_group = ansible.inventory.group.Group(name='master')
all_group.add_child_group(master_group)
host = self.inventory['master'] host = self.inventory['master']
ansible_host = ansible.inventory.host.Host(name=host['name'] + '.vm.okeanos.grnet.gr') ansible_host = ansible.inventory.host.Host(name=host['name'] + '.vm.okeanos.grnet.gr')
host_vars['internal_ip'] = self.inventory['master']['ip'] # master_group.set_variable('local_net', self.cidr)
host_vars['local_net'] = self.cidr ansible_host.set_variable('ansible_ssh_private_key_file', self.temp_file)
for var_key, var_value in host_vars.iteritems(): ansible_host.set_variable('internal_ip', self.inventory['master']['ip'])
ansible_host.set_variable(var_key, var_value)
ansible_host.set_variable('id', 0) ansible_host.set_variable('id', 0)
master_group.add_host(ansible_host) master_group.add_host(ansible_host)
inventory_groups.append(master_group) inventory_groups.append(master_group)
slave_group = ansible.inventory.group.Group(name='slaves') slave_group = ansible.inventory.group.Group(name='slaves')
host_vars['proxy_env'] = {'http_proxy': 'http://' + self.inventory['master']['name']+'.local:3128'} all_group.add_child_group(slave_group)
slave_group.set_variable('proxy_env', {'http_proxy': 'http://' + self.inventory['master']['name'] + '.local:3128'})
for host_id, host in enumerate(self.inventory["slaves"], start=1): for host_id, host in enumerate(self.inventory["slaves"], start=1):
ansible_host = ansible.inventory.host.Host(name=host['name'] + '.local') ansible_host = ansible.inventory.host.Host(name=host['name'] + '.local')
host_vars['internal_ip'] = host['ip'] # host_vars['internal_ip'] = host['ip']
for var_key, var_value in host_vars.iteritems(): # for var_key, var_value in host_vars.iteritems():
ansible_host.set_variable(var_key, var_value) # ansible_host.set_variable(var_key, var_value)
ansible_host.set_variable('internal_ip', host['ip'])
ansible_host.set_variable('id', host_id) ansible_host.set_variable('id', host_id)
slave_group.add_host(ansible_host) slave_group.add_host(ansible_host)
inventory_groups.append(slave_group) inventory_groups.append(slave_group)
...@@ -62,6 +71,7 @@ class Manager: ...@@ -62,6 +71,7 @@ class Manager:
self.ansible_inventory = ansible.inventory.Inventory(host_list=None) self.ansible_inventory = ansible.inventory.Inventory(host_list=None)
for group in inventory_groups: for group in inventory_groups:
self.ansible_inventory.add_group(group) self.ansible_inventory.add_group(group)
print self.ansible_inventory.groups_list()
return self.ansible_inventory return self.ansible_inventory
...@@ -73,14 +83,16 @@ class Manager: ...@@ -73,14 +83,16 @@ class Manager:
stats = callbacks.AggregateStats() stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY) playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY) runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
# print self.ansible_inventory.get_group('all').get_hosts()
pb = PlayBook(playbook=playbook_file, inventory=self.ansible_inventory, stats=stats, pb = PlayBook(playbook=playbook_file, inventory=self.ansible_inventory, stats=stats,
callbacks=playbook_cb, callbacks=playbook_cb,
runner_callbacks=runner_cb, only_tags=tags) runner_callbacks=runner_cb, only_tags=tags)
pb.run() pb.run()
def cleanup(self):
os.remove(self.temp_file)
if __name__ == "__main__": if __name__ == "__main__":
response = {u'ips': [{u'floating_network_id': u'2216', response = {u'ips': [{u'floating_network_id': u'2216',
u'floating_ip_address': u'83.212.118.6', u'floating_ip_address': u'83.212.118.6',
u'id': u'686825'}], u'id': u'686825'}],
...@@ -90,3 +102,12 @@ if __name__ == "__main__": ...@@ -90,3 +102,12 @@ if __name__ == "__main__":
u'vpn': {u'type': u'MAC_FILTERED', u'id': u'143499'}, u'vpn': {u'type': u'MAC_FILTERED', u'id': u'143499'},
u'subnet': {u'cidr': u'192.168.0.0/24', u'gateway_ip': u'192.168.0.1', u'id': u'142564'}} u'subnet': {u'cidr': u'192.168.0.0/24', u'gateway_ip': u'192.168.0.1', u'id': u'142564'}}
manager = Manager(response)
manager.create_inventory()
manager.run_playbook(playbook_file="../../ansible/playbooks/testinventory.yml", tags=['hosts'])
# manager.run_playbook(playbook_file="../../ansible/playbooks/common/install.yml", tags=['master'])
# manager.run_playbook(playbook_file="../../ansible/playbooks/proxy/proxy.yml", tags=['config'])
# manager.run_playbook(playbook_file="../../ansible/playbooks/common/install.yml", tags=['slaves'])
manager.cleanup()
\ No newline at end of file
import argparse import argparse
import time
from provisioner import Provisioner from provisioner import Provisioner
from ansible_manager import Manager from ansible_manager import Manager
...@@ -44,9 +45,20 @@ if __name__ == "__main__": ...@@ -44,9 +45,20 @@ if __name__ == "__main__":
for i, slave in enumerate(provisioner_response['nodes']['slaves']): for i, slave in enumerate(provisioner_response['nodes']['slaves']):
slave_ip = provisioner.get_server_private_ip(slave['id']) slave_ip = provisioner.get_server_private_ip(slave['id'])
provisioner_response['nodes']['slaves'][i]['internal_ip'] = slave_ip provisioner_response['nodes']['slaves'][i]['internal_ip'] = slave_ip
provisioner_response['pk'] = provisioner.get_private_key(); provisioner_response['pk'] = provisioner.get_private_key()
print provisioner_response
time.sleep(15)
manager = Manager(provisioner_response) manager = Manager(provisioner_response)
manager.create_inventory() manager.create_inventory()
manager.run_playbook(playbook_file="../../ansible/playbooks/testinventory.yml", tags=["touch"]) manager.run_playbook(playbook_file="../../ansible/playbooks/testinventory.yml", tags=['hosts'])
# manager.run_playbook(playbook_file="../../ansible/playbooks/testproxy.yml", tags=["install"]) # manager.run_playbook(playbook_file="../../ansible/playbooks/testproxy.yml", tags=['install'])
# manager.run_playbook(playbook_file="../../ansible/playbooks/common/install.yml", tags=['master'])
# manager.run_playbook(playbook_file="../../ansible/playbooks/proxy/proxy.yml")
# manager.run_playbook(playbook_file="../../ansible/playbooks/common/install.yml", tags=['slaves'])
# INSERT PLAYBOOKS HERE
manager.cleanup()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment