@@ -31,58 +31,54 @@ from __future__ import print_function
 
 import sys
 import os
+import ConfigParser
 
 from debops import *
 from debops.cmds import *
 
 PATHSEP = ':'
 
+
+def write_config(filename, config):
+    cfgparser = ConfigParser.ConfigParser()
+    for section, pairs in config.items():
+        cfgparser.add_section(section)
+        for option, value in pairs.items():
+            cfgparser.set(section, option, value)
+
+    with open(filename, "w") as fh:
+        print("# Ansible configuration file generated by DebOps, "
+              "all changes will be lost", file=fh)
+        print(file=fh)
+        cfgparser.write(fh)
+
+
 def gen_ansible_cfg(filename, debops_root, playbooks_path, inventory_path):
     # Generate Ansible configuration file
-    def p(*args): print(*args, file=fh)
-    with open(filename, "w") as fh:
-        p("# Ansible configuration file generated by DebOps, all changes will be lost")
-        p()
-        p("[defaults]")
-        p("hostfile           = ", inventory_path)
-        p()
-        p("roles_path         = ", PATHSEP.join((
+
+    def plugin_paths(type):
+        yield os.path.join(debops_root, "ansible", plugin_type)
+        yield os.path.join(playbooks_path, plugin_type)
+        yield os.path.join("/usr/share/ansible_plugins", plugin_type)
+
+    cfg = {}
+    defaults = {
+        'hostfile': inventory_path,
+        'roles_path': PATHSEP.join((
             os.path.join(debops_root, "roles"),
             os.path.join(debops_root, "ansible", "roles"),
             os.path.join(playbooks_path, "..", "roles"),
             os.path.join(playbooks_path, "roles"),
-            "/etc/ansible/roles")
-        ))
-        p("action_plugins     =", PATHSEP.join((
-            os.path.join(debops_root, "ansible", "action_plugins"),
-            os.path.join(playbooks_path, "action_plugins"),
-            "/usr/share/ansible_plugins/action_plugins")
-        ))
-        p("callback_plugins   =", PATHSEP.join((
-            os.path.join(debops_root, "ansible", "callback_plugins"),
-            os.path.join(playbooks_path, "callback_plugins"),
-            "/usr/share/ansible_plugins/callback_plugins")
-        ))
-        p("connection_plugins =", PATHSEP.join((
-            os.path.join(debops_root, "ansible", "connection_plugins"),
-            os.path.join(playbooks_path, "connection_plugins"),
-            "/usr/share/ansible_plugins/connection_plugins")
-         ))
-        p("lookup_plugins     =", PATHSEP.join((
-            os.path.join(debops_root, "ansible", "lookup_plugins"),
-            os.path.join(playbooks_path, "lookup_plugins"),
-            "/usr/share/ansible_plugins/lookup_plugins")
-        ))
-        p("vars_plugins       =", PATHSEP.join((
-            os.path.join(debops_root, "ansible", "vars_plugins"),
-            os.path.join(playbooks_path, "vars_plugins"),
-            "/usr/share/ansible_plugins/vars_plugins")
-        ))
-        p("filter_plugins     =", PATHSEP.join((
-            os.path.join(debops_root, "ansible", "filter_plugins"),
-            os.path.join(playbooks_path, "filter_plugins"),
-            "/usr/share/ansible_plugins/filter_plugins")
-        ))
+            "/etc/ansible/roles")),
+        }
+    for plugin_type in ('action', 'callback', 'connection',
+                        'filter', 'lookup', 'vars'):
+        plugin_type = plugin_type+"_plugins"
+        defaults[plugin_type] = PATHSEP.join(plugin_paths(plugin_type))
+
+    cfg = {'defaults': defaults}
+    write_config(filename, cfg)
+
 
 # ---- DebOps environment setup ----
 