From 762ce6e96e2bc1db5c2348dd1112e88b93d78d12 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea <ssbarnea@redhat.com> Date: Fri, 12 Feb 2021 12:33:25 +0000 Subject: [PATCH] update-json-file: avoid failure when destination does not exists It is perfectly valid to want to add a value to a file that does not exist yet, even the path may be missing. This fixes last night regression when installing docker no longer creates the /etc/docker folder, causing our MTU update to fail. Change-Id: I0f037d1d6664de3c3b777aaf6da9cd7c3e8bb15f Reference: https://review.rdoproject.org/zuul/builds?job_name=tox-py36-ci-config&project=rdo-infra/ci-config --- roles/update-json-file/README.rst | 4 ++++ roles/update-json-file/tasks/main.yaml | 19 +++++++++++++++++++ test-playbooks/update-json-file.yaml | 6 +++--- 3 files changed, 26 insertions(+), 3 deletions(-) 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