| 1 | --- |
| 2 | # To run this play: ansible-playbook playbooks/snapshot-rootnode.yml -l node_standalone |
| 3 | - name: Initialize cross play variables |
| 4 | hosts: node_standalone |
| 5 | vars: |
| 6 | - job_wait_to_end: true |
| 7 | - job_abort: false |
| 8 | # Set to false when taking the first snapshot of a new rootnode |
| 9 | - revert_before: true |
| 10 | - snapshot_name: 'base-configuration' |
| 11 | - wait_for_changes: true |
| 12 | tasks: |
| 13 | - ansible.builtin.set_fact: |
| 14 | job_wait_to_end: "{{job_wait_to_end}}" |
| 15 | job_abort: "{{job_abort}}" |
| 16 | revert_before: "{{revert_before}}" |
| 17 | snapshot_name: "{{snapshot_name}}" |
| 18 | target_rootnode: "{{inventory_hostname}}" |
| 19 | wait_for_changes: "{{wait_for_changes}}" |
| 20 | |
| 21 | - name: Shutdown and revert |
| 22 | hosts: node_standalone |
| 23 | tasks: |
| 24 | - name: "Find {{target_rootnode}}'s host" |
| 25 | ansible.builtin.command: |
| 26 | argv: "{{command_args|list}}" |
| 27 | register: get_hypervisor |
| 28 | vars: |
| 29 | command_args: |
| 30 | - '../scripts/manage_jenkins_node.py' |
| 31 | - 'get_hypervisor' |
| 32 | - "{{target_rootnode}}" |
| 33 | delegate_to: localhost |
| 34 | - ansible.builtin.set_fact: |
| 35 | rootnode_host: "{{get_hypervisor.stdout|trim}}" |
| 36 | - name: Disable {{target_rootnode}} in Jenkins |
| 37 | ansible.builtin.command: |
| 38 | argv: "{{command_args | reject('equalto', '') | list}}" |
| 39 | vars: |
| 40 | command_args: |
| 41 | - '../scripts/manage_jenkins_node.py' |
| 42 | - 'disable' |
| 43 | - '--wait' |
| 44 | - "{{ 0 if job_wait_to_end else -1}}" |
| 45 | - "{{target_rootnode}}" |
| 46 | - "{{'--force-abort' if job_abort else ''}}" |
| 47 | - '--reason' |
| 48 | - 'Ansible play: snapshot rootnode' |
| 49 | delegate_to: localhost |
| 50 | - name: Revert target rootnode |
| 51 | when: revert_before |
| 52 | ansible.builtin.command: |
| 53 | argv: ['virsh', 'snapshot-revert', "{{target_rootnode}}", '--snapshotname', "{{snapshot_name}}", '--running'] |
| 54 | delegate_to: "{{rootnode_host}}" |
| 55 | - name: Wait for target rootnode to come back online |
| 56 | local_action: wait_for host={{ target_rootnode }} |
| 57 | port=22 |
| 58 | delay=1 |
| 59 | timeout=300 |
| 60 | state=started |
| 61 | |
| 62 | # If the playbook isn't run with `-l node_standalone` or `-l ci-rootnode...`, then |
| 63 | # this play errors out trying to find variables for the ci-hosts even if the condition |
| 64 | # `inventory_hostname in groups.node_standalone` is false. |
| 65 | - name: Run site.yml |
| 66 | import_playbook: '../site.yml' |
| 67 | when: inventory_hostname in groups.node_standalone |
| 68 | |
| 69 | - name: Cleanup and snapshot |
| 70 | hosts: node_standalone |
| 71 | tasks: |
| 72 | - ansible.builtin.debug: |
| 73 | msg: "Example playbook command: `ansible-playbook site.yml -l {{target_rootnode}}`" |
| 74 | when: wait_for_changes |
| 75 | delegate_to: localhost |
| 76 | - ansible.builtin.pause: |
| 77 | prompt: "Run any playbooks or make other changes against {{target_rootnode}} then hit enter when ready to continue (Ctrl-C to abort)" |
| 78 | when: wait_for_changes |
| 79 | delegate_to: localhost |
| 80 | - name: Remove the jenkins workspace |
| 81 | ansible.builtin.file: |
| 82 | path: "{{item}}" |
| 83 | state: absent |
| 84 | with_items: |
| 85 | - /root/workspace |
| 86 | - /home/jenkins/workspace |
| 87 | - name: Stop the VM |
| 88 | ansible.builtin.command: |
| 89 | argv: ['virsh', 'destroy', "{{target_rootnode}}"] |
| 90 | delegate_to: "{{rootnode_host}}" |
| 91 | - name: Check for existing base snapshot |
| 92 | ansible.builtin.shell: |
| 93 | cmd: "virsh snapshot-list '{{target_rootnode}}' --name | grep -E '^{{snapshot_name}}$'" |
| 94 | ignore_errors: true |
| 95 | register: existing_snapshot |
| 96 | delegate_to: "{{rootnode_host}}" |
| 97 | - name: Remove existing base snapshot |
| 98 | when: existing_snapshot.rc == 0 |
| 99 | ansible.builtin.command: |
| 100 | argv: ['virsh', 'snapshot-delete', '--snapshotname', "{{snapshot_name}}", "{{target_rootnode}}"] |
| 101 | delegate_to: "{{rootnode_host}}" |
| 102 | - name: Snapshot |
| 103 | ansible.builtin.command: |
| 104 | argv: ['virsh', 'snapshot-create-as', '--name', "{{snapshot_name}}", "{{target_rootnode}}"] |
| 105 | delegate_to: "{{rootnode_host}}" |
| 106 | - name: Start the vm |
| 107 | ansible.builtin.command: |
| 108 | argv: ['virsh', 'start', "{{target_rootnode}}"] |
| 109 | delegate_to: "{{rootnode_host}}" |
| 110 | - name: Enable {{target_rootnode}} in Jenkins |
| 111 | ansible.builtin.command: |
| 112 | argv: "{{command_args | list}}" |
| 113 | vars: |
| 114 | command_args: |
| 115 | - '../scripts/manage_jenkins_node.py' |
| 116 | - 'enable' |
| 117 | - "{{target_rootnode}}" |
| 118 | delegate_to: localhost |