From b636ab31124f8bafcdb1a4fcb34c21c8b117e537 Mon Sep 17 00:00:00 2001
From: Thomas Woerner <twoerner@redhat.com>
Date: Tue, 6 Aug 2019 09:17:59 +0200
Subject: [PATCH] utils/ansible-ipa-[server,replica,client]-install: New
 --playbook-dir option

If the --playbook-dir option is used, the inventory and playbook files will
be generated in this directory. The files will not be removed after the
playbook processing ended.

If the directory does not exist an error message will be printed and the
utility will not continue.
---
 utils/ansible-ipa-client-install  | 23 +++++++++++++++++++----
 utils/ansible-ipa-replica-install | 23 +++++++++++++++++++----
 utils/ansible-ipa-server-install  | 23 +++++++++++++++++++----
 3 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/utils/ansible-ipa-client-install b/utils/ansible-ipa-client-install
index 453b75f3..273f997f 100755
--- a/utils/ansible-ipa-client-install
+++ b/utils/ansible-ipa-client-install
@@ -201,9 +201,19 @@ def parse_options():
                         choices=("yes", "no"), default=None,
                         help="The bool value defines if the needed packages "
                         "are installed on the node. Default: yes")
+    # playbook
+    parser.add_argument("--playbook-dir",
+                        dest="playbook_dir",
+                        default=None,
+                        help="If defined will be used as to create inventory "
+                        "file and playbook in. The files will not be removed "
+                        "after the playbook processing ended.")
 
     options, args = parser.parse_known_args()
 
+    if options.playbook_dir and not os.path.isdir(options.playbook_dir):
+        parser.error("playbook dir does not exist")
+
     if options.password_prompt:
         parser.error("password prompt is not possible with ansible")
     if options.log_file:
@@ -240,10 +250,14 @@ def run_cmd(args):
 
 
 def main(options, args):
-    temp_dir = tempfile.mkdtemp(prefix='ansible-ipa')
+    if options.playbook_dir:
+        playbook_dir = options.playbook_dir
+    else:
+        temp_dir = tempfile.mkdtemp(prefix='ansible-ipa-client')
+        playbook_dir = temp_dir
 
-    inventory = os.path.join(temp_dir, "client-inventory")
-    playbook = os.path.join(temp_dir, "client-playbook.yml")
+    inventory = os.path.join(playbook_dir, "ipaclient-inventory")
+    playbook = os.path.join(playbook_dir, "ipaclient-playbook.yml")
 
     with open(inventory, 'w') as f:
         if options.servers:
@@ -356,7 +370,8 @@ def main(options, args):
         if returncode != 0:
             raise RuntimeError()
     finally:
-        shutil.rmtree(temp_dir, ignore_errors=True)
+        if not options.playbook_dir:
+            shutil.rmtree(temp_dir, ignore_errors=True)
 
 
 options, args = parse_options()
diff --git a/utils/ansible-ipa-replica-install b/utils/ansible-ipa-replica-install
index 54c85bb4..16e700f2 100755
--- a/utils/ansible-ipa-replica-install
+++ b/utils/ansible-ipa-replica-install
@@ -271,9 +271,19 @@ def parse_options():
                         help="The value defines if the needed services will "
                         "automatically be openen in the firewall managed by "
                         "firewalld. Default: yes")
+    # playbook
+    parser.add_argument("--playbook-dir",
+                        dest="playbook_dir",
+                        default=None,
+                        help="If defined will be used as to create inventory "
+                        "file and playbook in. The files will not be removed "
+                        "after the playbook processing ended.")
 
     options, args = parser.parse_known_args()
 
+    if options.playbook_dir and not os.path.isdir(options.playbook_dir):
+        parser.error("playbook dir does not exist")
+
     if options.log_file:
         parser.error("log_file is not supported")
 
@@ -308,10 +318,14 @@ def run_cmd(args):
 
 
 def main(options, args):
-    temp_dir = tempfile.mkdtemp(prefix='ansible-ipa')
+    if options.playbook_dir:
+        playbook_dir = options.playbook_dir
+    else:
+        temp_dir = tempfile.mkdtemp(prefix='ansible-ipa-replica')
+        playbook_dir = temp_dir
 
-    inventory = os.path.join(temp_dir, "replica-inventory")
-    playbook = os.path.join(temp_dir, "replica-playbook.yml")
+    inventory = os.path.join(playbook_dir, "ipareplica-inventory")
+    playbook = os.path.join(playbook_dir, "ipareplica-playbook.yml")
 
     with open(inventory, 'w') as f:
         if options.servers:
@@ -472,7 +486,8 @@ def main(options, args):
         if returncode != 0:
             raise RuntimeError()
     finally:
-        shutil.rmtree(temp_dir, ignore_errors=True)
+        if not options.playbook_dir:
+            shutil.rmtree(temp_dir, ignore_errors=True)
 
 
 options, args = parse_options()
diff --git a/utils/ansible-ipa-server-install b/utils/ansible-ipa-server-install
index 9e2ea486..906bca3c 100755
--- a/utils/ansible-ipa-server-install
+++ b/utils/ansible-ipa-server-install
@@ -307,9 +307,19 @@ def parse_options():
                         choices=("yes", "no"), default=None,
                         help="Copy the generated CSR from the ipaserver to "
                         "the controller as <hostname>-ipa.csr.")
+    # playbook
+    parser.add_argument("--playbook-dir",
+                        dest="playbook_dir",
+                        default=None,
+                        help="If defined will be used as to create inventory "
+                        "file and playbook in. The files will not be removed "
+                        "after the playbook processing ended.")
 
     options, args = parser.parse_known_args()
 
+    if options.playbook_dir and not os.path.isdir(options.playbook_dir):
+        parser.error("playbook dir does not exist")
+
     if options.log_file:
         parser.error("log_file is not supported")
 
@@ -344,10 +354,14 @@ def run_cmd(args):
 
 
 def main(options, args):
-    temp_dir = tempfile.mkdtemp(prefix='ansible-ipa')
+    if options.playbook_dir:
+        playbook_dir = options.playbook_dir
+    else:
+        temp_dir = tempfile.mkdtemp(prefix='ansible-ipa-server')
+        playbook_dir = temp_dir
 
-    inventory = os.path.join(temp_dir, "server-inventory")
-    playbook = os.path.join(temp_dir, "server-playbook.yml")
+    inventory = os.path.join(playbook_dir, "ipaserver-inventory")
+    playbook = os.path.join(playbook_dir, "ipaserver-playbook.yml")
 
     with open(inventory, 'w') as f:
         f.write("[ipaserver]\n")
@@ -529,7 +543,8 @@ def main(options, args):
         if returncode != 0:
             raise RuntimeError()
     finally:
-        shutil.rmtree(temp_dir, ignore_errors=True)
+        if not options.playbook_dir:
+            shutil.rmtree(temp_dir, ignore_errors=True)
 
 
 options, args = parse_options()
-- 
GitLab