Revision 6902788e gen-ssh-config.py

View differences:

gen-ssh-config.py
2 2
import xml.etree.ElementTree as ET
3 3
from argparse import ArgumentParser
4 4
import sys
5
from ssh_config_template import SSHConfig
5
from config_templates import SSHConfig, AnsibleConfig
6 6

  
7 7
from const import COMPONENT_ID, NODE, SSH_CONFIG_TEMPLATE, \
8
    HOST_CONFIG_TEMPLATE, IDENTITY_FILE, NODE_NAME
8
    HOST_CONFIG_TEMPLATE, IDENTITY_FILE, NODE_NAME, ANSIBLE_CONFIG_TEMPLATE, \
9
    INVENTORY_CONFIG_TEMPLATE, ANSIBLE_HOST_TEMPLATE
9 10

  
10 11
parser = ArgumentParser()
11 12
parser.add_argument("-r", "--rspec", dest="rspec",
......
15 16
                    default="ssh-config", action="store", metavar="FILENAME",
16 17
                    help="Output file onto which the SSH configuration is "
17 18
                         "written")
19
parser.add_argument("-a", "--ansible-config", dest="ansible_config",
20
                    default="ansible.cfg", action="store", metavar="FILENAME",
21
                    help="Output file onto which the ansible configuration is "
22
                         "written")
23
parser.add_argument("-i", "--inventory", dest="ansible_inventory",
24
                    default="ansible-hosts", action="store", metavar="FILENAME",
25
                    help="Output file onto which the ansible inventory is "
26
                         "written")
27

  
18 28
args = parser.parse_args()
19 29

  
20 30
if not args.rspec:
......
23 33

  
24 34
rspec_file = args.rspec
25 35
ssh_config_file = args.ssh_config
36
ansible_config_file = args.ansible_config
37
ansible_inventory_file = args.ansible_inventory
26 38

  
27 39
config_file = open(ssh_config_file, "w")
40
ansible_file = open(ansible_config_file, "w")
41
inventory_file = open(ansible_inventory_file, "w")
28 42

  
29 43
ssh_config = SSHConfig(SSH_CONFIG_TEMPLATE, HOST_CONFIG_TEMPLATE, IDENTITY_FILE)
44
ansible_config = AnsibleConfig(ANSIBLE_CONFIG_TEMPLATE,
45
                               INVENTORY_CONFIG_TEMPLATE,
46
                               ANSIBLE_HOST_TEMPLATE, IDENTITY_FILE,
47
                               ansible_inventory_file, ssh_config_file)
30 48

  
31 49
xml_file = ET.iterparse(rspec_file)
32 50
for _, el in xml_file:
......
43 61
    domain = components[1]
44 62
    name = components[3]
45 63
    hostname = "{}.{}".format(name, domain)
46
    ssh_config.add_host(name_template.format(n), hostname)
64
    friendly_name = name_template.format(n)
65
    ssh_config.add_host(friendly_name, hostname)
66
    ansible_config.add_host(friendly_name)
47 67
    n += 1
48 68

  
49 69
config_file.write(str(ssh_config))
70
ansible_file.write(ansible_config.get_ansible_config())
71
inventory_file.write(ansible_config.get_ansible_inventory())
72

  
50 73
config_file.close()
74
ansible_file.close()
75
inventory_file.close()

Also available in: Unified diff