Last active
November 1, 2017 11:01
-
-
Save longtian/20967825f28a7f7ca6045054a8121ca5 to your computer and use it in GitHub Desktop.
Reset virtualbox to snapshot using ansible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
VBOX_DEFAULT_ROLE: base | |
VBOX_GUEST_SSH_PORT: 22 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: "获取运行状态" | |
raw: "vboxmanage showvminfo {{ inventory_hostname }} --machinereadable | grep VMState=" | |
delegate_to: 127.0.0.1 | |
register: "vmstate" | |
- name: "关闭电源" | |
delegate_to: 127.0.0.1 | |
command: "vboxmanage controlvm {{ inventory_hostname }} poweroff" | |
when: "'runnin' in vmstate.stdout" | |
- name: "还原到 {{ VBOX_DEFAULT_ROLE }} 镜像" | |
delegate_to: 127.0.0.1 | |
command: "vboxmanage snapshot {{ inventory_hostname }} restore {{ VBOX_DEFAULT_ROLE }}" | |
- name: "启动电源" | |
delegate_to: 127.0.0.1 | |
command: "vboxmanage startvm {{ inventory_hostname }} --type=headless" | |
- name: "等待启动完成" | |
wait_for: | |
delay: 5 | |
host: "{{ inventory_hostname }}" | |
port: "{{ VBOX_GUEST_SSH_PORT }}" | |
delegate_to: 127.0.0.1 | |
- include: reset-to-snapshot.yml | |
when: "lookup('env','RESET_VMS') | bool" | |
- name: "显示消息" | |
delegate_to: 127.0.0.1 | |
run_once: yes | |
ignore_errors: yes | |
command: "notify-send -i dialog-warning 'Ansible' '重置完成,测试 {{ test_role }}'" | |
when: "test_role is defined" | |
changed_when: False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment