Revision 6902788e

View differences:

config_templates.py
1
class SSHConfig:
2

  
3
    def __init__(self, ssh_config, host_config, identity_file):
4
        self.ssh_config = ssh_config
5
        self.host_config = host_config
6
        with open(ssh_config, "r") as ssh_file:
7
            self.ssh_template = ssh_file.read()
8
        with open(host_config, "r") as host_file:
9
            self.host_template = host_file.read()
10
        self.identity_file = identity_file
11
        self.config = self.ssh_template.format(identity=identity_file)
12

  
13
    def add_host(self, name, hostname):
14
        self.config += self.host_template.format(name=name, hostname=hostname,
15
                                                 identity=self.identity_file)
16

  
17
    def __str__(self):
18
        return self.config
19

  
20

  
21
class AnsibleConfig:
22
    def __init__(self, ansible_config, inventory_config, host_config,
23
                 identity_filename, inventory_filename, ssh_config_filename):
24
        self.ansible_config = ansible_config
25
        self.inventory_config = inventory_config
26
        self.host_config = host_config
27
        self.inventory_filename = inventory_filename
28
        self.ssh_config_filename = ssh_config_filename
29
        with open(ansible_config, "r") as ansible_file:
30
            self.ansible_template = ansible_file.read()
31
        with open(inventory_config, "r") as inventory_file:
32
            self.inventory_template = inventory_file.read()
33
        with open(host_config, "r") as host_file:
34
            self.host_template = host_file.read()
35
        self.identity_filename = identity_filename
36
        config = self.ansible_template.format(identity=identity_filename,
37
                                              inventory=inventory_filename,
38
                                              ssh_config=ssh_config_filename)
39
        inventory = self.inventory_template
40
        self.config = config
41
        self.inventory = inventory
42

  
43
    def add_host(self, name):
44
        self.inventory += self.host_template.format(name=name)
45

  
46
    def get_ansible_config(self):
47
        return self.config
48

  
49
    def get_ansible_inventory(self):
50
        return self.inventory
const.py
16 16

  
17 17
NODE_NAME = "node{{:0{}d}}"
18 18

  
19
SSH_CONFIG_TEMPLATE = "./ssh-templates/ssh-config.template"
20
HOST_CONFIG_TEMPLATE = "./ssh-templates/host.template"
19
SSH_CONFIG_TEMPLATE = "./templates/ssh-config.template"
20
HOST_CONFIG_TEMPLATE = "./templates/host.template"
21 21
IDENTITY_FILE = "~/.ssh/twist.cert"
22

  
23
ANSIBLE_CONFIG_TEMPLATE = "./templates/ansible-cfg.template"
24
INVENTORY_CONFIG_TEMPLATE = "./templates/ansible-inventory.template"
25
ANSIBLE_HOST_TEMPLATE = "./templates/ansible-host.template"
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()
ssh-templates/host.template
1
Host {name}
2
    HostName {hostname}
3
    Port 22
4
    User segata
5
    IdentityFile {identity}
6
    ForwardAgent yes
7
    ProxyCommand            ssh -F ssh-config proxy0 -W %h:%p
8
    TCPKeepAlive            yes
9
    Compression             no
10
    CheckHostIP no
11
    StrictHostKeyChecking no
ssh-templates/ssh-config.template
1
Host bastion.test.iminds.be proxy0
2
    User segata
3
    HostName bastion.test.iminds.be
4
    Port 22
5
    IdentityFile {identity}
6
    ProxyCommand none
7
    BatchMode yes
8
    PasswordAuthentication no
9
    EscapeChar none
10
    CheckHostIP no
11
    StrictHostKeyChecking yes
templates/ansible-cfg.template
1
[defaults]
2
private_key_file = {identity}
3
host_key_checking = false
4
inventory = {inventory}
5

  
6
[ssh_connection]
7
ssh_args = -F {ssh_config}
8
scp_if_ssh = True
templates/ansible-host.template
1
{name}
templates/ansible-inventory.template
1
# Sample commands:
2
# ansible nodes -m ping
3
# ansible nodes -m shell -a "uptime"
4

  
5
[nodes]
templates/host.template
1
Host {name}
2
    HostName {hostname}
3
    Port 22
4
    User segata
5
    IdentityFile {identity}
6
    ForwardAgent yes
7
    ProxyCommand            ssh -F ssh-config proxy0 -W %h:%p
8
    TCPKeepAlive            yes
9
    Compression             no
10
    CheckHostIP no
11
    StrictHostKeyChecking no
templates/ssh-config.template
1
Host bastion.test.iminds.be proxy0
2
    User segata
3
    HostName bastion.test.iminds.be
4
    Port 22
5
    IdentityFile {identity}
6
    ProxyCommand none
7
    BatchMode yes
8
    PasswordAuthentication no
9
    EscapeChar none
10
    CheckHostIP no
11
    StrictHostKeyChecking yes

Also available in: Unified diff