Ansible で /etc/docker/daemon.json ファイルをいじったのでメモ。
slurp moduel を使って JSON ファイルを書き換えた。
まずは ansible をインストール (Ubuntu 22.04)
1 2 | pip install ansible-core ansible-galaxy collection install community.general |
つづいて playbook を作成。
update_daemon_json.yaml の名前で保存。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | --- - 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 |
そして実行
1 | ansible-playbook update_daemon_json.yml |