diff --git a/roles/update-json-file/README.rst b/roles/update-json-file/README.rst index 1f93f211a..be464c8c1 100644 --- a/roles/update-json-file/README.rst +++ b/roles/update-json-file/README.rst @@ -46,3 +46,7 @@ idempotent and will write the file each time. .. zuul:rolevar:: update_json_file_group The group for the combined file. + +.. zuul:rolevar:: update_json_dir_mode + + The mode for the directory if that does not already exists. diff --git a/roles/update-json-file/tasks/main.yaml b/roles/update-json-file/tasks/main.yaml index 7362f17af..fde66a505 100644 --- a/roles/update-json-file/tasks/main.yaml +++ b/roles/update-json-file/tasks/main.yaml @@ -28,6 +28,25 @@ var: _config when: update_json_file_debug +- name: Assure we have a target directory + when: update_json_file_name | dirname + block: + - name: Check if target directory exists + stat: + path: "{{ update_json_file_name | dirname }}" + register: _result + + - name: Create target directory + file: + path: "{{ update_json_file_name | dirname }}" + state: directory + mode: '{{ update_json_dir_mode | default(omit) }}' + owner: '{{ update_json_file_owner | default(omit) }}' + group: '{{ update_json_file_group | default(omit) }}' + recurse: yes + become: '{{ update_json_file_become }}' + when: not _result.stat.exists + - name: Save new file copy: content: "{{ _config | to_nice_json }}" diff --git a/test-playbooks/update-json-file.yaml b/test-playbooks/update-json-file.yaml index 1f5c4b371..40a464348 100644 --- a/test-playbooks/update-json-file.yaml +++ b/test-playbooks/update-json-file.yaml @@ -4,7 +4,7 @@ - include_role: name: update-json-file vars: - update_json_file_name: test.json + update_json_file_name: some/test.json update_json_file_default: foo: bar update_json_file_combine: @@ -14,7 +14,7 @@ - include_role: name: update-json-file vars: - update_json_file_name: test.json + update_json_file_name: some/test.json update_json_file_combine: new: content a: @@ -25,7 +25,7 @@ - name: Load resulting merged file slurp: - path: 'test.json' + path: 'some/test.json' register: _file - name: Parse merged file