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
This commit is contained in:
Sorin Sbarnea 2021-02-12 12:33:25 +00:00
parent b1521dbdb5
commit 762ce6e96e
3 changed files with 26 additions and 3 deletions

View File

@ -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.

View File

@ -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 }}"

View File

@ -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