From de48994d76a7f971a6a322e99945ded5812d5784 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Mon, 17 Oct 2022 18:51:04 +0200 Subject: [PATCH] ansible: can delete old image before rebuild --- ansible/inventory.py | 1 + ansible/machine.yml | 27 ++++++++++++++++++++++++--- ansible/machines.yml | 5 +++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/ansible/inventory.py b/ansible/inventory.py index 588a6c5..7d92b51 100755 --- a/ansible/inventory.py +++ b/ansible/inventory.py @@ -99,6 +99,7 @@ class RisottoInventory(object): continue ret[server_name] = engine.rougail_variables_dict ret['modules'] = {module_name: module_info['infos'].depends for module_name, module_info in module_infos.items() if module_name in modules} + ret['delete_old_image'] = False ret['configure_host'] = True ret['only_machine'] = None return dumps(ret, cls=RougailEncoder) diff --git a/ansible/machine.yml b/ansible/machine.yml index 1c00f83..133421f 100644 --- a/ansible/machine.yml +++ b/ansible/machine.yml @@ -17,6 +17,7 @@ path: "installations{{ file.name }}" checksum: sha256 get_checksum: yes + follow: true loop: "{{ vars[item.name]['services'] | fileslist }}" loop_control: loop_var: file @@ -28,6 +29,7 @@ path: "/var/lib/risotto/configurations/{{ item.name }}{{ file.name }}" checksum: sha256 get_checksum: yes + follow: true loop: "{{ vars[item.name]['services'] | fileslist }}" loop_control: loop_var: file @@ -36,12 +38,12 @@ - name: "Configuration's file is up to date in {{ item.name }}" debug: - msg: "file is {{ 'out of date' if not file[1].stat.exists or file[0].stat.checksum != file[1].stat.checksum else 'up to date' }}" - changed_when: not file[1].stat.exists or file[0].stat.checksum != file[1].stat.checksum + msg: "file is {{ 'out of date' if not file[1].stat.exists or (not 'checksum' in file[0].stat and 'checksum' in file[1].stat) or ('checksum' in file[0].stat and not 'checksum' in file[1].stat) or ('checksum' in file[0].stat and 'checksum' in file[1].stat and file[0].stat.checksum != file[1].stat.checksum) else 'up to date' }}" + changed_when: not file[1].stat.exists or (not 'checksum' in file[0].stat and 'checksum' in file[1].stat) or ('checksum' in file[0].stat and not 'checksum' in file[1].stat) or ('checksum' in file[0].stat and 'checksum' in file[1].stat and file[0].stat.checksum != file[1].stat.checksum) loop: "{{ local_configuration.results | zip(remote_configuration.results) | list }}" loop_control: loop_var: file - label: "{{ file[0]['stat']['path'] }}" + label: "{{ file[0]['file']['name'] }}" ignore_errors: true register: up_to_date_configuration @@ -60,6 +62,24 @@ format: tar when: up_to_date_configuration.changed +- name: "Remove old image {{ vars | modulename(item.name) }}" + file: + path: "/var/lib/risotto/images/{{ vars | modulename(item.name) }}.tar" + state: absent + when: delete_old_image == true + +- name: "Stop machine {{ item.name }}" + machinectl: + state: stopped + machines: "{{ item.name }}" + when: delete_old_image == true + +- name: "Remove old machine {{ item.name }}" + file: + path: /var/lib/machines/{{ item.name }} + state: absent + when: delete_old_image == true + - name: "Create system directory for {{ item.name }}" file: path: /var/lib/machines/{{ item.name }} @@ -69,6 +89,7 @@ - name: "Check image for {{ item.name }}" stat: path: "/var/lib/risotto/images/{{ vars | modulename(item.name) }}.tar" + follow: true register: register_name when: system_directory_created.changed diff --git a/ansible/machines.yml b/ansible/machines.yml index a190b08..131adaf 100644 --- a/ansible/machines.yml +++ b/ansible/machines.yml @@ -21,6 +21,11 @@ dest: /var/lib/risotto/configurations/{{ item | basename }}/ loop: "{{ lookup('fileglob', '/tmp/new_configurations/*', wantlist=True) }}" +- name: "Enable machines" + machinectl: + state: enabled + machines: "{{ vars | machineslist(only_name=True) }}" + - name: "Start machines" machinectl: state: started