yaml_to_ini.py 7.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

import ansible.constants as C
from ansible.inventory.host import Host
from ansible.inventory.group import Group
from ansible import errors
from ansible import utils
import os
import yaml
import sys

class InventoryParserYaml(object):
    ''' Host inventory parser for ansible '''

    def __init__(self, filename=C.DEFAULT_HOST_LIST):

        sys.stderr.write("WARNING: YAML inventory files are deprecated in 0.6 and will be removed in 0.7, to migrate" +
            " download and run https://github.com/ansible/ansible/blob/devel/examples/scripts/yaml_to_ini.py\n")

        fh = open(filename)
        data = fh.read()
        fh.close()
        self._hosts = {}
        self._parse(data)

    def _make_host(self, hostname):

        if hostname in self._hosts:
            return self._hosts[hostname]
        else:
            host = Host(hostname)
            self._hosts[hostname] = host
Michael DeHaan committed
48
            return host
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    # see file 'test/yaml_hosts' for syntax

    def _parse(self, data):
        # FIXME: refactor into subfunctions

        all = Group('all')

        ungrouped = Group('ungrouped')
        all.add_child_group(ungrouped)

        self.groups = dict(all=all, ungrouped=ungrouped)
        grouped_hosts = []

        yaml = utils.parse_yaml(data)

        # first add all groups
        for item in yaml:
            if type(item) == dict and 'group' in item:
                group = Group(item['group'])

                for subresult in item.get('hosts',[]):

                    if type(subresult) in [ str, unicode ]:
                        host = self._make_host(subresult)
                        group.add_host(host)
                        grouped_hosts.append(host)
                    elif type(subresult) == dict:
                        host = self._make_host(subresult['host'])
                        vars = subresult.get('vars',{})
                        if type(vars) == list:
                            for subitem in vars:
                                for (k,v) in subitem.items():
Michael DeHaan committed
82
                                    host.set_variable(k,v)
83 84 85 86 87 88 89 90 91 92 93
                        elif type(vars) == dict:
                            for (k,v) in subresult.get('vars',{}).items():
                                host.set_variable(k,v)
                        else:
                            raise errors.AnsibleError("unexpected type for variable")
                        group.add_host(host)
                        grouped_hosts.append(host)

                vars = item.get('vars',{})
                if type(vars) == dict:
                    for (k,v) in item.get('vars',{}).items():
Michael DeHaan committed
94
                        group.set_variable(k,v)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
                elif type(vars) == list:
                    for subitem in vars:
                        if type(subitem) != dict:
                            raise errors.AnsibleError("expected a dictionary")
                        for (k,v) in subitem.items():
                            group.set_variable(k,v)

                self.groups[group.name] = group
                all.add_child_group(group)

        # add host definitions
        for item in yaml:
            if type(item) in [ str, unicode ]:
                host = self._make_host(item)
                if host not in grouped_hosts:
                    ungrouped.add_host(host)

            elif type(item) == dict and 'host' in item:
                host = self._make_host(item['host'])

                vars = item.get('vars', {})
                if type(vars)==list:
                    varlist, vars = vars, {}
                    for subitem in varlist:
                        vars.update(subitem)
                for (k,v) in vars.items():
                    host.set_variable(k,v)

                groups = item.get('groups', {})
                if type(groups) in [ str, unicode ]:
                    groups = [ groups ]
                if type(groups)==list:
                    for subitem in groups:
                        if subitem in self.groups:
                            group = self.groups[subitem]
                        else:
                            group = Group(subitem)
                            self.groups[group.name] = group
                            all.add_child_group(group)
                        group.add_host(host)
                        grouped_hosts.append(host)

                if host not in grouped_hosts:
                    ungrouped.add_host(host)

        # make sure ungrouped.hosts is the complement of grouped_hosts
        ungrouped_hosts = [host for host in ungrouped.hosts if host not in grouped_hosts]

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print "usage: yaml_to_ini.py /path/to/ansible/hosts"
        sys.exit(1)

    result = ""

    original = sys.argv[1]
    yamlp = InventoryParserYaml(filename=sys.argv[1])
    dirname = os.path.dirname(original)

    group_names = [ g.name for g in yamlp.groups.values() ]

    for group_name in sorted(group_names):

        record = yamlp.groups[group_name]

        if group_name == 'all':
            continue
Michael DeHaan committed
162

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        hosts = record.hosts
        result = result + "[%s]\n" % record.name
        for h in hosts:
            result = result + "%s\n" % h.name
        result = result + "\n"

        groupfiledir = os.path.join(dirname, "group_vars")
        if not os.path.exists(groupfiledir):
            print "* creating: %s" % groupfiledir
            os.makedirs(groupfiledir)
        groupfile = os.path.join(groupfiledir, group_name)
        print "* writing group variables for %s into %s" % (group_name, groupfile)
        groupfh = open(groupfile, 'w')
        groupfh.write(yaml.dump(record.get_variables()))
        groupfh.close()

    for (host_name, host_record) in yamlp._hosts.iteritems():
        hostfiledir = os.path.join(dirname, "host_vars")
        if not os.path.exists(hostfiledir):
            print "* creating: %s" % hostfiledir
            os.makedirs(hostfiledir)
        hostfile = os.path.join(hostfiledir, host_record.name)
        print "* writing host variables for %s into %s" % (host_record.name, hostfile)
        hostfh = open(hostfile, 'w')
        hostfh.write(yaml.dump(host_record.get_variables()))
        hostfh.close()
Michael DeHaan committed
189

190 191 192 193 194 195 196 197 198 199 200

    # also need to keep a hash of variables per each host
    # and variables per each group
    # and write those to disk

    newfilepath = os.path.join(dirname, "hosts.new")
    fdh = open(newfilepath, 'w')
    fdh.write(result)
    fdh.close()

    print "* COMPLETE: review your new inventory file and replace your original when ready"
Michael DeHaan committed
201
    print "*           new inventory file saved as %s" % newfilepath
202 203 204 205 206
    print "*           edit group specific variables in %s/group_vars/" % dirname
    print "*           edit host specific variables in %s/host_vars/" % dirname

    # now need to write this to disk as (oldname).new
    # and inform the user