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
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)

View file

@ -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

View file

@ -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