Add filter for absolute path of file/dir
This adds a custom filter that can be used in playbooks to convert relative file/dir paths wrt home dir and tht_root to absolute paths. Also changes relevant playbooks to use it. Closes-Bug: #1895194 Change-Id: I260fa3ec2f85a095d1d3ae4c22e2488d6000257a
This commit is contained in:
parent
325d080583
commit
afe6f82185
|
@ -16,9 +16,12 @@
|
|||
|
||||
import ast
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import six
|
||||
|
||||
from ansible import errors
|
||||
|
||||
# cmp() doesn't exist on python3
|
||||
if six.PY3:
|
||||
def cmp(a, b):
|
||||
|
@ -48,7 +51,8 @@ class FilterModule(object):
|
|||
'get_filtered_service_chain': self.get_filtered_service_chain,
|
||||
'get_filtered_role_resources': self.get_filtered_role_resources,
|
||||
'get_node_capabilities': self.get_node_capabilities,
|
||||
'get_node_profile': self.get_node_profile
|
||||
'get_node_profile': self.get_node_profile,
|
||||
'tht_abspath': self.tht_abspath
|
||||
}
|
||||
|
||||
def subsort(self, dict_to_sort, attribute, null_value=0):
|
||||
|
@ -185,6 +189,37 @@ class FilterModule(object):
|
|||
|
||||
return to_delete
|
||||
|
||||
def tht_abspath(self, file_paths, ignore_error=False,
|
||||
root_dir='/usr/share/openstack-tripleo-heat-templates'):
|
||||
'''Find a file/dir absolute path or relative to the home/t-h-t dir'''
|
||||
|
||||
def get_path(file_path):
|
||||
path = os.path.abspath(file_path)
|
||||
if not os.path.exists(path):
|
||||
path = os.path.abspath(os.path.join(os.path.expanduser('~'),
|
||||
file_path))
|
||||
if not os.path.exists(path):
|
||||
path = os.path.abspath(os.path.join(root_dir, file_path))
|
||||
if not os.path.exists(path):
|
||||
if not ignore_error:
|
||||
raise errors.AnsibleFilterError(
|
||||
"Can't find path %s" % (file_path,))
|
||||
return file_path
|
||||
return path
|
||||
|
||||
if not file_paths:
|
||||
return None
|
||||
elif isinstance(file_paths, str):
|
||||
return get_path(file_paths)
|
||||
elif isinstance(file_paths, list):
|
||||
paths = []
|
||||
for f_path in file_paths:
|
||||
paths.append(get_path(f_path))
|
||||
return paths
|
||||
else:
|
||||
raise errors.AnsibleFilterError(
|
||||
"Either lists or string for paths only supported.")
|
||||
|
||||
def haskey(self, data, attribute, value=None, reverse=False, any=False,
|
||||
excluded_keys=[]):
|
||||
"""Return dict data with a specific key.
|
||||
|
|
|
@ -32,20 +32,21 @@
|
|||
tasks:
|
||||
- name: Fetch roles_data
|
||||
slurp:
|
||||
src: "{{ roles_file }}"
|
||||
src: "{{ roles_file | tht_abspath }}"
|
||||
register: roles_data
|
||||
- name: Set fact for log file
|
||||
set_fact:
|
||||
cip_log_file: "{{ lookup('env', 'HOME') ~ '/' ~ log_file }}"
|
||||
- name: Get all files in directories
|
||||
find:
|
||||
paths: "{{ environment_directories }}"
|
||||
paths: "{{ environment_directories | tht_abspath(ignore_error=true) }}"
|
||||
patterns: '*.yaml'
|
||||
when: environment_directories
|
||||
register: out_env_files
|
||||
- name: Build environment_files
|
||||
set_fact:
|
||||
env_files: "{{ out_env_files.files | map(attribute='path') | list + environment_files }}"
|
||||
env_files: "{{ out_env_files.files | map(
|
||||
attribute='path') | list + (environment_files | tht_abspath) }}"
|
||||
- name: Build heat stack environment
|
||||
tripleo_build_heat_environment:
|
||||
env_files: "{{ env_files }}"
|
||||
|
|
|
@ -89,18 +89,12 @@
|
|||
|
||||
when: use_default_templates|bool or source_url is not none
|
||||
|
||||
# If plan_environment is an absolute path, then
|
||||
# don't prepend the templates dir. Else prepend it.
|
||||
- name: Create plan environment if does not exist
|
||||
os_object:
|
||||
container: "{{ container }}"
|
||||
name: plan-environment.yaml
|
||||
state: present
|
||||
filename: >-
|
||||
{{ plan_environment.startswith('/')
|
||||
| ternary(plan_environment,
|
||||
default_templates_dir.rstrip('/')
|
||||
+ '/' + plan_environment) }}
|
||||
filename: "{{ plan_environment | tht_abspath }}"
|
||||
|
||||
- name: Generate passwords and update plan
|
||||
tripleo_passwords_rotate:
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from ansible import errors
|
||||
|
||||
from tripleo_ansible.ansible_plugins.filter import helpers
|
||||
from tripleo_ansible.tests import base as tests_base
|
||||
|
||||
|
@ -374,6 +376,24 @@ class TestHelperFilters(tests_base.TestCase):
|
|||
any=True)
|
||||
self.assertEqual(result, expected_list)
|
||||
|
||||
def test_abspath(self):
|
||||
file_path = '/etc/hosts'
|
||||
result = self.filters.tht_abspath(file_path)
|
||||
self.assertEqual(result, '/etc/hosts')
|
||||
|
||||
file_path = ['/etc', 'tmp']
|
||||
result = self.filters.tht_abspath(
|
||||
file_path, ignore_error=True)
|
||||
self.assertEqual(result, file_path)
|
||||
|
||||
def test_abspath_not_found(self):
|
||||
file_path = 'plan-environment.yaml'
|
||||
ex = self.assertRaises(
|
||||
errors.AnsibleFilterError,
|
||||
self.filters.tht_abspath, file_path)
|
||||
msg = ("Can't find path plan-environment.yaml")
|
||||
self.assertEqual(msg, str(ex))
|
||||
|
||||
def test_needs_delete(self):
|
||||
data = [
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue