From 5287083116da2512b887529e67af4c4919e1e296 Mon Sep 17 00:00:00 2001
From: Mark Goddard <mark@stackhpc.com>
Date: Tue, 23 Aug 2022 16:46:29 +0100
Subject: [PATCH] Fix passwords.yml generation when parent directory doesn't
 exist

Fixes an issue where generation of passwords.yml for Kolla Ansible
could fail if the directory containing the file does not exist. This is
typical in a multiple environment setup, when creating a new
environment.

Story: 2010293
Task: 46275
Closes-Bug: #2015093
Change-Id: I9dce73a8a205c0c0ad02eba3a10e02b82f5b191d
---
 ansible/roles/kolla-ansible/library/kolla_passwords.py    | 3 +++
 .../notes/fix-kolla-passwords-f1b5d051c494b4d8.yaml       | 8 ++++++++
 2 files changed, 11 insertions(+)
 create mode 100644 releasenotes/notes/fix-kolla-passwords-f1b5d051c494b4d8.yaml

diff --git a/ansible/roles/kolla-ansible/library/kolla_passwords.py b/ansible/roles/kolla-ansible/library/kolla_passwords.py
index 4b3c0491d..351177635 100644
--- a/ansible/roles/kolla-ansible/library/kolla_passwords.py
+++ b/ansible/roles/kolla-ansible/library/kolla_passwords.py
@@ -22,6 +22,7 @@ short_description: Generates a kolla-ansible passwords file
 
 from ansible.module_utils.basic import *
 
+import os
 import os.path
 import shutil
 import tempfile
@@ -154,6 +155,8 @@ def kolla_passwords(module):
             checksum_temp_file = module.sha1(temp_file_path)
             changed = checksum_dest != checksum_temp_file
         else:
+            os.makedirs(os.path.dirname(module.params['dest']),
+                        exist_ok=True)
             changed = True
 
         # Encrypt the file.
diff --git a/releasenotes/notes/fix-kolla-passwords-f1b5d051c494b4d8.yaml b/releasenotes/notes/fix-kolla-passwords-f1b5d051c494b4d8.yaml
new file mode 100644
index 000000000..3a3c1490d
--- /dev/null
+++ b/releasenotes/notes/fix-kolla-passwords-f1b5d051c494b4d8.yaml
@@ -0,0 +1,8 @@
+---
+fixes:
+  - |
+    Fixes an issue where generation of ``passwords.yml`` for Kolla Ansible
+    could fail if the directory containing the file does not exist. This is
+    typical in a multiple environment setup, when creating a new environment.
+    See `story 2010293 <https://storyboard.openstack.org/#!/story/2010293>`_
+    for details.