ansible: Disable, wait, and enable root nodes during snapshot
authorKienan Stewart <kstewart@efficios.com>
Fri, 26 Jul 2024 20:51:57 +0000 (16:51 -0400)
committerKienan Stewart <kstewart@efficios.com>
Mon, 29 Jul 2024 13:12:11 +0000 (09:12 -0400)
This change decreases the number of manual steps required to perform
the updates to the rootnode snapshots.

Change-Id: I0ad25a4e282498ce5f146f75dcabaa84a6eb2400
Signed-off-by: Kienan Stewart <kstewart@efficios.com>
automation/ansible/README.md
automation/ansible/playbooks/snapshot-rootnode.yml

index fac4b7ceeb94a704d458159f77582eb2e4e11d81..be764c4643a61258d7893e5e51685b3f4faca9b3 100644 (file)
@@ -41,12 +41,14 @@ ansible-playbook -i hosts [-l SUBSET] site.yaml
 
 ## CI 'rootnode'
 
-1. Add an entry to the `vms` variable in the host vars for a libvirt host
+1. Add the new ansible node to the `node_standalone` group in the inventory
+2. Add an entry to the `vms` variable in the host vars for the libvirt host
   * See the defaults and details in `roles/libvirt/vars/main.yml` and `roles/libvirt/tasks/main.yml`
   * Make sure to set the `cdrom` key to the path of ISO for the installer
-2. Run the playbook, eg. `ansible-playbook -i hosts -l cloud07.internal.efficios.com site.yml`
+3. Run the playbook, eg. `ansible-playbook -i hosts -l cloud07.internal.efficios.com site.yml`
   * The VM should be created and started
-3. Once the VM is installed take a snapshot so that Jenkins may revert to the original state
+4. Once the VM is installed take a snapshot so that Jenkins may revert to the original state
+  * `ansible-playbook playbooks/snapshot-rootnode.yml -e '{"revert_before": false}' -l new-rootnode`
 
 ### Ubuntu auto-installer
 
index 9b17bc7f68b9b62ae1c96143a0468ac80143b29a..91809a710f0b348452f1724933a262dd91b46250 100644 (file)
@@ -1,22 +1,52 @@
 ---
-- hosts: localhost
+# To run this play: ansible-playbook playbooks/snapshot-rootnode.yml -l node_standalone
+- name: Initialize cross play variables
+  hosts: node_standalone
   vars:
-    # Useful for taking the first snapshot of a new rootnode
+    - job_wait_to_end: true
+    - job_abort: false
+    # Set to false when taking the first snapshot of a new rootnode
     - revert_before: true
-  vars_prompt:
-    - name: target_rootnode
-      private: false
-    - name: rootnode_host
-      private: false
-    - name: snapshot_name
-      default: 'base-configuration'
-      private: false
-  vars_files:
-    - ../vars/ci-instances.yml
+    - snapshot_name: 'base-configuration'
+    - wait_for_changes: true
   tasks:
-    # - Set a maintenance on the jenkins node
-    # - Wait until the task is done
-    # - Maybe interrupt the jenkins tasks
+    - ansible.builtin.set_fact:
+        job_wait_to_end: "{{job_wait_to_end}}"
+        job_abort: "{{job_abort}}"
+        revert_before: "{{revert_before}}"
+        snapshot_name: "{{snapshot_name}}"
+        target_rootnode: "{{inventory_hostname}}"
+        wait_for_changes: "{{wait_for_changes}}"
+
+- name: Shutdown and revert
+  hosts: node_standalone
+  tasks:
+    - name: "Find {{target_rootnode}}'s host"
+      ansible.builtin.command:
+        argv: "{{command_args|list}}"
+      register: get_hypervisor
+      vars:
+        command_args:
+          - '../scripts/manage_jenkins_node.py'
+          - 'get_hypervisor'
+          - "{{target_rootnode}}"
+      delegate_to: localhost
+    - ansible.builtin.set_fact:
+        rootnode_host: "{{get_hypervisor.stdout|trim}}"
+    - name: Disable {{target_rootnode}} in Jenkins
+      ansible.builtin.command:
+        argv: "{{command_args | reject('equalto', '') | list}}"
+      vars:
+        command_args:
+          - '../scripts/manage_jenkins_node.py'
+          - 'disable'
+          - '--wait'
+          - "{{ 0 if job_wait_to_end else -1}}"
+          - "{{target_rootnode}}"
+          - "{{'--force-abort' if job_abort else ''}}"
+          - '--reason'
+          - 'Ansible play: snapshot rootnode'
+      delegate_to: localhost
     - name: Revert target rootnode
       when: revert_before
       ansible.builtin.command:
                     delay=1
                     timeout=300
                     state=started
+
+# If the playbook isn't run with `-l node_standalone` or `-l ci-rootnode...`, then
+# this play errors out trying to find variables for the ci-hosts even if the condition
+# `inventory_hostname in groups.node_standalone` is false.
+- name: Run site.yml
+  import_playbook: '../site.yml'
+  when: inventory_hostname in groups.node_standalone
+
+- name: Cleanup and snapshot
+  hosts: node_standalone
+  tasks:
+    - ansible.builtin.debug:
+        msg: "Example playbook command: `ansible-playbook site.yml -l {{target_rootnode}}`"
+      when: wait_for_changes
+      delegate_to: localhost
     - ansible.builtin.pause:
         prompt: "Run any playbooks or make other changes against {{target_rootnode}} then hit enter when ready to continue (Ctrl-C to abort)"
+      when: wait_for_changes
+      delegate_to: localhost
     - name: Remove the jenkins workspace
       ansible.builtin.file:
         path: "{{item}}"
@@ -37,7 +84,6 @@
       with_items:
         - /root/workspace
         - /home/jenkins/workspace
-      delegate_to: "{{target_rootnode}}"
     - name: Stop the VM
       ansible.builtin.command:
         argv: ['virsh', 'destroy', "{{target_rootnode}}"]
       ansible.builtin.command:
         argv: ['virsh', 'start', "{{target_rootnode}}"]
       delegate_to: "{{rootnode_host}}"
-    # - Remove maintenance on the jenkins node
+    - name: Enable {{target_rootnode}} in Jenkins
+      ansible.builtin.command:
+        argv: "{{command_args | list}}"
+      vars:
+        command_args:
+          - '../scripts/manage_jenkins_node.py'
+          - 'enable'
+          - "{{target_rootnode}}"
+      delegate_to: localhost
This page took 0.025516 seconds and 4 git commands to generate.