zuul-jobs/roles/update-json-file/tasks/main.yaml
Sorin Sbarnea 762ce6e96e 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
2021-02-12 13:26:49 +00:00

58 lines
1.6 KiB
YAML

- name: Check if file exists
stat:
path: '{{ update_json_file_name }}'
register: _stat
- name: Load existing file
when: _stat.stat.exists
slurp:
path: '{{ update_json_file_name }}'
register: _file
- name: Parse exisiting file
when: _stat.stat.exists
set_fact:
_config: "{{ _file.content | b64decode | from_json }}"
- name: Set default for non existing file
when: not _stat.stat.exists
set_fact:
_config: '{{ update_json_file_default }}'
- name: Combine new configuration
set_fact:
_config: "{{ _config | combine(update_json_file_combine) }}"
- name: Debug _config variable
debug:
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 }}"
dest: '{{ update_json_file_name }}'
mode: '{{ update_json_file_mode | default(omit) }}'
owner: '{{ update_json_file_owner | default(omit) }}'
group: '{{ update_json_file_group | default(omit) }}'
become: '{{ update_json_file_become }}'