Name
Last commit
Last update
..
README.md vm init

logo

Ansible을 Virtualization 구성

KVM 기반의 Hypervisor에서 Virtual Machine에 대한 생성을 자동화 한다.

수행 내역

  • 검증된 VM Image(.qcow2)를 KVM Hypervisor에 배포한다.
  • 변수된 지정된 IP주소로 각 VM에 네트워크 및 호스트네임을 세팅을 진행한다.
  • 각 VM에 root 패스워드를 세팅한다.
  • Playbook 수행완료 시 Slack으로 Push Alert을 보낸다.

Inventory 설정

# cat hosts
[ALL_HOSTS]
172.16.0.100 managed_ip=172.16.0.100 des="2020-12-03"

[ALL_HOSTS_OK]

대상 호스트는 172.16.0.100이며, 추가는 2020-12-03에 수행한다. 참고적으로 ALL_HOSTS_OK 호스트 그룹은 작업이 완료된 호스트의 history를 위한 그룹이다.

Host Variables 설정

# cat host_vars/172.16.0.100
ansible_ssh_host: 172.16.0.100
ansible_ssh_port: SSH_포트번호
ansible_ssh_user: 사용자 아이디
ansible_ssh_pass: "사용자 패스워드"
ansible_become: yes
ansible_become_method: su
ansible_become_user: root
ansible_become_pass: "!root_패스워드"
ansible_python_interpreter: python2.7

다음 Playbook은 KVM Hypervisor(172.16.0.100)에 총 3개의 VM을 생성한다.

Playbook 설정

---
- name: Virtualization Install (KVM)
  hosts: all
  become: true
  vars:
    USER_NAME: demouser #사용자 아이디를 입력
    SERVER1_IP: '172.16.0.200'
    SERVER2_IP: '172.16.0.201'
    SERVER3_IP: '172.16.0.202'
    GATEWAY_IP: '172.16.254.1'
    PASSWORD: "root 패스워드"
  tasks:
    - name: Install VM GuestTools
      yum: name={{ item }} update_cache=yes
      with_items:
        - guestfish
        - libguestfs-tools

    - name: VIRT Users Directory /root/VM-TEMP/CONFIGS
      file:
        path: "{{ item }}"
        state: directory
        owner: root
        group: root
        mode: 0755
      with_items:
        - /root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server1
        - /root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server2
        - /root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server3

    - name: VIRT Users Files Copy
      template:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        mode: 0644
        owner: root
        group: root
      with_items:
        - {src: 'templates/Server1-ifcfg-eth0.j2', dest: '/root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server1/ifcfg-eth0'}
        - {src: 'templates/Server1-ifcfg-eth1.j2', dest: '/root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server1/ifcfg-eth1'}
        - {src: 'templates/Server1-hostname.j2', dest: '/root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server1/hostname'}
        - {src: 'templates/Server2-ifcfg-eth0.j2', dest: '/root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server2/ifcfg-eth0'}
        - {src: 'templates/Server2-ifcfg-eth1.j2', dest: '/root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server2/ifcfg-eth1'}
        - {src: 'templates/Server2-hostname.j2', dest: '/root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server2/hostname'}
        - {src: 'templates/Server3-ifcfg-eth0.j2', dest: '/root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server3/ifcfg-eth0'}
        - {src: 'templates/Server3-ifcfg-eth1.j2', dest: '/root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server3/ifcfg-eth1'}
        - {src: 'templates/Server3-hostname.j2', dest: '/root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server3/hostname'}

    - name: Server/Desktop VM XML Files Copy
      template:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        mode: 0600
        owner: root
        group: root
      with_items:
        - {src: 'templates/server1.xml.j2', dest: '/etc/libvirt/qemu/{{ USER_NAME }}-server1.xml'}
        - {src: 'templates/server2.xml.j2', dest: '/etc/libvirt/qemu//{{ USER_NAME }}-server2.xml'}
        - {src: 'templates/server3.xml.j2', dest: '/etc/libvirt/qemu//{{ USER_NAME }}-server3.xml'}

    - name: VM Users Server Images Copy
      shell: |
       cp -rf /root/VM-TEMP/VMS/centos/7.8/centos78.qcow2 /var/lib/libvirt/images/{{ USER_NAME }}-server1.qcow2
       chmod 600 /var/lib/libvirt/images/{{ USER_NAME }}-server1.qcow2
       cp -rf /root/VM-TEMP/VMS/centos/7.8/centos78.qcow2 /var/lib/libvirt/images/{{ USER_NAME }}-server2.qcow2
       chmod 600 /var/lib/libvirt/images/{{ USER_NAME }}-server2.qcow2
       cp -rf /root/VM-TEMP/VMS/centos/7.8/centos78_20G.qcow2 /var/lib/libvirt/images/{{ USER_NAME }}-server3.qcow2
       chmod 600 /var/lib/libvirt/images/{{ USER_NAME }}-server3.qcow2

    - name: VM Users Server1 Configuration
      shell: |
       /usr/bin/virt-customize -a /var/lib/libvirt/images/{{ USER_NAME }}-server1.qcow2 \
        --root-password password:{{ PASSWORD }} \
        --copy-in /root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server1/ifcfg-eth0:/etc/sysconfig/network-scripts/ \
        --copy-in /root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server1/ifcfg-eth1:/etc/sysconfig/network-scripts/ \
        --copy-in /root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server1/hostname:/etc/

    - name: VM Users Server2 Configuration
      shell: |
       /usr/bin/virt-customize -a /var/lib/libvirt/images/{{ USER_NAME }}-server2.qcow2 \
        --root-password password:{{ PASSWORD }} \
        --copy-in /root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server2/ifcfg-eth0:/etc/sysconfig/network-scripts/ \
        --copy-in /root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server2/ifcfg-eth1:/etc/sysconfig/network-scripts/ \
        --copy-in /root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server2/hostname:/etc/

    - name: VM Users Server3 Configuration
      shell: |
       /usr/bin/virt-customize -a /var/lib/libvirt/images/{{ USER_NAME }}-server3.qcow2 \
        --root-password password:{{ PASSWORD }} \
        --copy-in /root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server3/ifcfg-eth0:/etc/sysconfig/network-scripts/ \
        --copy-in /root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server3/ifcfg-eth1:/etc/sysconfig/network-scripts/ \
        --copy-in /root/VM-TEMP/CONFIGS/{{ USER_NAME }}/Server3/hostname:/etc/

    - name: reloaded the libvirtd
      systemd:
        state: reloaded
        daemon_reload: yes
        name: libvirtd.service
        enabled: True

    - name: VM NIC Attaching
      shell: |
        virsh attach-interface --domain {{ USER_NAME }}-server1 --type bridge --source br17216 --model virtio --config
        sleep 2
        virsh attach-interface --domain {{ USER_NAME }}-server1 --type bridge --source br17216 --model virtio --config
        sleep 2
        virsh attach-interface --domain {{ USER_NAME }}-server2 --type bridge --source br17216 --model virtio --config
        sleep 2
        virsh attach-interface --domain {{ USER_NAME }}-server2 --type bridge --source br17216 --model virtio --config
        sleep 2
        virsh attach-interface --domain {{ USER_NAME }}-server3 --type bridge --source br17216 --model virtio --config
        sleep 2
        virsh attach-interface --domain {{ USER_NAME }}-server3 --type bridge --source br17216 --model virtio --config

    - name: reloaded the libvirtd
      systemd:
        state: reloaded
        daemon_reload: yes
        name: libvirtd.service
        enabled: True

    - name: Send notification message via Slack all options
      local_action:
        module: slack
        token: TH9557E80/XXXXX/XXXXXXX
        msg: "HOST: [ *{{ inventory_hostname }}* ],  TITLE: [ *VM Setting Ansible Playbook* ] \nUSER: [ *{{ USER_NAME }}* ],  VNC_PORT: [ *{{ VNC_PORT_CORE }}{{ VNC_PORT }}* ] *Completed!*"
        channel: "#ansible"
        username: "Ansible HOST on {{ inventory_hostname }}"
        color: good
        icon_url: "http://pds.hongsnet.net/images/ansible.png"
        link_names: 0

Virtualization 구성에 대한 Playbook 분석

  • 검증된 VM Image를 배포한다(copy, shell 모듈).
  • questfish 툴을 이용하여, 실제 VM에 네트워킹/호스트네임/패스워드를 설정한다(shell 모듈).
  • Jinja2 템플릿을 이용하여, Libvirtd의 XML을 설정한다.

playbook 실행

# ansible-playbook -i hosts vm_install.yml