ansible: can delete old image before rebuild

This commit is contained in:
Emmanuel Garette 2022-10-17 18:51:04 +02:00
parent 34d277d80f
commit de48994d76
3 changed files with 30 additions and 3 deletions

View file

@ -99,6 +99,7 @@ class RisottoInventory(object):
continue continue
ret[server_name] = engine.rougail_variables_dict 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['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['configure_host'] = True
ret['only_machine'] = None ret['only_machine'] = None
return dumps(ret, cls=RougailEncoder) return dumps(ret, cls=RougailEncoder)

View file

@ -17,6 +17,7 @@
path: "installations{{ file.name }}" path: "installations{{ file.name }}"
checksum: sha256 checksum: sha256
get_checksum: yes get_checksum: yes
follow: true
loop: "{{ vars[item.name]['services'] | fileslist }}" loop: "{{ vars[item.name]['services'] | fileslist }}"
loop_control: loop_control:
loop_var: file loop_var: file
@ -28,6 +29,7 @@
path: "/var/lib/risotto/configurations/{{ item.name }}{{ file.name }}" path: "/var/lib/risotto/configurations/{{ item.name }}{{ file.name }}"
checksum: sha256 checksum: sha256
get_checksum: yes get_checksum: yes
follow: true
loop: "{{ vars[item.name]['services'] | fileslist }}" loop: "{{ vars[item.name]['services'] | fileslist }}"
loop_control: loop_control:
loop_var: file loop_var: file
@ -36,12 +38,12 @@
- name: "Configuration's file is up to date in {{ item.name }}" - name: "Configuration's file is up to date in {{ item.name }}"
debug: 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' }}" 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 file[0].stat.checksum != file[1].stat.checksum 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: "{{ local_configuration.results | zip(remote_configuration.results) | list }}"
loop_control: loop_control:
loop_var: file loop_var: file
label: "{{ file[0]['stat']['path'] }}" label: "{{ file[0]['file']['name'] }}"
ignore_errors: true ignore_errors: true
register: up_to_date_configuration register: up_to_date_configuration
@ -60,6 +62,24 @@
format: tar format: tar
when: up_to_date_configuration.changed 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 }}" - name: "Create system directory for {{ item.name }}"
file: file:
path: /var/lib/machines/{{ item.name }} path: /var/lib/machines/{{ item.name }}
@ -69,6 +89,7 @@
- name: "Check image for {{ item.name }}" - name: "Check image for {{ item.name }}"
stat: stat:
path: "/var/lib/risotto/images/{{ vars | modulename(item.name) }}.tar" path: "/var/lib/risotto/images/{{ vars | modulename(item.name) }}.tar"
follow: true
register: register_name register: register_name
when: system_directory_created.changed when: system_directory_created.changed

View file

@ -21,6 +21,11 @@
dest: /var/lib/risotto/configurations/{{ item | basename }}/ dest: /var/lib/risotto/configurations/{{ item | basename }}/
loop: "{{ lookup('fileglob', '/tmp/new_configurations/*', wantlist=True) }}" loop: "{{ lookup('fileglob', '/tmp/new_configurations/*', wantlist=True) }}"
- name: "Enable machines"
machinectl:
state: enabled
machines: "{{ vars | machineslist(only_name=True) }}"
- name: "Start machines" - name: "Start machines"
machinectl: machinectl:
state: started state: started