Statistics
| Branch: | Revision:

iof-tools / config_templates.py @ 6902788e

History | View | Annotate | Download (2.02 KB)

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