Ansible で /etc/docker/daemon.json ファイルをいじったのでメモ。
slurp moduel を使って JSON ファイルを書き換えた。
まずは ansible をインストール (Ubuntu 22.04)
pip install ansible-core ansible-galaxy collection install community.general
つづいて playbook を作成。
update_daemon_json.yaml の名前で保存。
---
- name: Update Docker daemon.json and restart Docker
hosts: localhost
become: true
vars:
log_config:
log-driver: json-file
log-opts:
max-size: "10m"
max-file: "3"
tasks:
- name: Read existing Docker daemon configuration
ansible.builtin.slurp:
src: /etc/docker/daemon.json
register: daemon_config
- name: Decode the Docker daemon configuration
set_fact:
current_config: "{{ daemon_config['content'] | b64decode | from_json }}"
- name: Update the Docker daemon configuration
set_fact:
new_config: "{{ current_config | combine(log_config, recursive=True) }}"
- name: Write the updated configuration back to daemon.json
copy:
dest: /etc/docker/daemon.json
content: "{{ new_config | to_nice_json }}"
notify: restart docker
handlers:
- name: restart docker
systemd:
name: docker
state: restarted
daemon_reload: yes
そして実行
ansible-playbook update_daemon_json.yml