it-artikel:linux:how-to-achieve-easy-automatic-deployment-of-centos7-as-virtualbox-vm-in-a-dynamic-dns-lan-environment-with-an-ansible-playbook
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | it-artikel:linux:how-to-achieve-easy-automatic-deployment-of-centos7-as-virtualbox-vm-in-a-dynamic-dns-lan-environment-with-an-ansible-playbook [2022-08-31 12:30] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== How to achieve easy automatic deployment of Centos7 as VirtualBox VM in a dynamic DNS LAN environment with an Ansible playbook ====== | ||
+ | |||
+ | <note important> | ||
+ | |||
+ | ===== Scenario / Environment: | ||
+ | |||
+ | * Have an internet connected LAN with a local DHCP and " | ||
+ | * DHCP Clients provide their FQDN to the DHCP Server and receive IPv4. | ||
+ | * DHCP Server updates local DNS Server (bind) so that the client FQDN becomes resolvable network wide. (Domainname is .lan ) | ||
+ | * My VirtualBox virtualisation host is my Ansible host. It itself is a DHCP Client. | ||
+ | * VirtualBox Server contains one predefined/ | ||
+ | * Guest OS CentOS7 | ||
+ | * Guest VM template name " | ||
+ | * Guest template FQDN " | ||
+ | * Guest Network: enabled, dhcp, bridged, sshd enabled | ||
+ | * Guest contains " | ||
+ | * Guests " | ||
+ | |||
+ | |||
+ | |||
+ | ===== Problem: ===== | ||
+ | |||
+ | To deploy another fresh Centos7 VM all i need to do is to " | ||
+ | |||
+ | Easy task, right ? But how to automate this with Ansible, so all i'll have to do is fire up just one command line and be flexible with the guests VM name and hostnames? | ||
+ | |||
+ | |||
+ | ===== My Solution: ===== | ||
+ | |||
+ | - Have Ansible installed on the VirtualBox Host (because, why not?) | ||
+ | - Have this Ansible Playbook prepared: <file yaml deploy-centos7-from-template-on-virtualbox.yml> | ||
+ | --- | ||
+ | - hosts: localhost | ||
+ | connection: local | ||
+ | gather_facts: | ||
+ | tasks: | ||
+ | - name: clone template vm | ||
+ | command: VBoxManage clonevm centos7-template --mode all --name {{ newVmName }} --register | ||
+ | - name: temp.start clone vm | ||
+ | command: VBoxManage startvm {{ newVmName }} --type headless | ||
+ | - name: add temp host to inventory (ram) | ||
+ | add_host: | ||
+ | hostname: "{{ newVmName }}.lan" | ||
+ | - name: flush local dns cache | ||
+ | command: systemd-resolve --flush-caches | ||
+ | - name: wait for new vm to register/ | ||
+ | shell: / | ||
+ | - name: wait for ssh@vm to be accessable | ||
+ | wait_for: | ||
+ | timeout: 90 | ||
+ | sleep: 2 | ||
+ | host: " | ||
+ | port: 22 | ||
+ | |||
+ | - hosts: centos7-template.lan | ||
+ | gather_facts: | ||
+ | remote_user: | ||
+ | tasks: | ||
+ | - name: check connection again | ||
+ | wait_for_connection: | ||
+ | timeout: 60 | ||
+ | - name: change vms hostname | ||
+ | command: hostnamectl set-hostname "{{ newVmName }}.lan" | ||
+ | - name: reboot vm | ||
+ | reboot: | ||
+ | |||
+ | - hosts: localhost | ||
+ | gather_facts: | ||
+ | connection: local | ||
+ | tasks: | ||
+ | - name: flush local dns cache | ||
+ | command: systemd-resolve --flush-caches | ||
+ | - name: wait for new vm to register/ | ||
+ | shell: / | ||
+ | - name: wait for ssh@vm to be accessable | ||
+ | wait_for: | ||
+ | timeout: 90 | ||
+ | sleep: 2 | ||
+ | host: "{{ newVmName }}.lan" | ||
+ | port: 22 | ||
+ | |||
+ | - hosts: "{{ newVmName }}.lan" | ||
+ | gather_facts: | ||
+ | remote_user: | ||
+ | tasks: | ||
+ | - name: show fqdn of new vm | ||
+ | command: hostname -f | ||
+ | |||
+ | </ | ||
+ | - Also have this little Linux shell script with it, so that Ansible has some way to check when a fqdn gets available/ | ||
+ | #!/bin/bash | ||
+ | |||
+ | targetHostname=" | ||
+ | timeOut=" | ||
+ | |||
+ | if test -z " | ||
+ | echo " | ||
+ | echo " | ||
+ | exit -1 | ||
+ | fi | ||
+ | |||
+ | if test -z " | ||
+ | timeOut=60 | ||
+ | fi | ||
+ | |||
+ | for seconds in $(seq $timeOut -1 1) ; do | ||
+ | systemd-resolve --flush-caches | ||
+ | if host " | ||
+ | if ping -c1 -w1 " | ||
+ | break | ||
+ | else | ||
+ | #echo " | ||
+ | continue | ||
+ | fi | ||
+ | else | ||
+ | #echo " | ||
+ | sleep 1 | ||
+ | fi | ||
+ | done | ||
+ | |||
+ | if test $seconds -gt 1 ; then | ||
+ | true | ||
+ | else | ||
+ | false | ||
+ | fi | ||
+ | |||
+ | </ | ||
+ | * **NOTE:** Ansible comes already with modules to check for hosts and connections (like **wait_for** and **wait_for_connection** ). However, i was not successfull with any of those, because in my special network environment hostnames are not always resolvable PLUS i had massive problems with " | ||
+ | - Have some Ansible " | ||
+ | [templatevms] | ||
+ | centos7-template.lan | ||
+ | |||
+ | #[admin] | ||
+ | # | ||
+ | |||
+ | #[cluster] | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | </ | ||
+ | - Run the Ansible playbook without SSH key checking to prevent answering security questions and provide the VMs new name on the command line: < | ||
+ | ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -v -i ansibleHosts -e newVmName=newVMsHostname deploy-centos7-from-template-on-virtualbox.yml | ||
+ | </ | ||
+ | - This will clone the template vm (resulting in new MAC and VBox media UUIDs), start the new VM, ssh into the new VM to change its hostname and reboots it. After new VM is back up it checks connection and hostname by a " | ||
+ | |||
+ | <note important> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | {{tag> |
it-artikel/linux/how-to-achieve-easy-automatic-deployment-of-centos7-as-virtualbox-vm-in-a-dynamic-dns-lan-environment-with-an-ansible-playbook.txt · Last modified: 2022-08-31 12:30 by 127.0.0.1