We all appreciate the value of automation. Using Ansible playbooks to deploy virtual machines in the Red Hat Virtualization environment is efficient, consistent, and straightforward. It all starts with a good design and infrastructure, which I discussed in part one of this article series. In part two, I added the use of templates for automation. Here in part three, I provide two Ansible playbooks that you can use to deploy VMs in your own environment.

The first sample playbook is designed to deploy a single VM. The second playbook deploys a group of VMs.

Sample Ansible playbook

The first playbook contains two plays:

  • Create One VM - part of this play will be placed within a block snippet, to make to remove the authentication token later.
  • Post Configure One VM - this play performs post-installation tasks. In this example, the task installs the qemu-guest-agent-x86_64.rpm package.

The playbook is saved in a file called single-vm.yml.

Run the playbook

The single-vm.yml playbook is executed in two different ways depending on how you pass the ansible-vault password.

Use a password file:

$ ansible-playbook -e "vmchoice=vm01" --vault-password-file /path/to/my/vault-password-file single-vm.yml

Prompt for a password:

$ ansible-playbook -e "vmchoice=vm01" –ask-vault-pass single-vm.yml

Sample Ansible playbook to deploy many VMs

Iterating over the previous playbook can be done by using different techniques. For instance, a for loop is run in a simple bash shell. However, we need some logic to verify if the configuration files are available at this level, and it is simpler to copy the logic in the previous playbook. Also, the iteration assumes you used a password file for the ansible-vault. However, that can be changed if needed.

The sample code here will loop to deploy several VMs running the same application, or in the same data center. However, loops with more complicated conditions are achieved by modifying the code.

The playbook is saved in a file called many-vms.yml.

Note: Errors are ignored in this playbook. The playbook can continue if a non-existing site or system is passed to the playbook.

Run the playbook

This playbook is executed as follows:

$ ansible-playbook -e "system=webserver" many-vms.yml

or

$ ansible-playbook -e "site=first" many-vms.yml

Issues and debugging

While adapting this sample playbook, you may run into some issues. Some of the most common examples include syntax errors, module support, and logic problems.

Indentation - YAML syntax is very sensitive to proper spacing. Use an editor that can recognize the YAML format. The most common editor is vim, of course. Add the following line to the .vimrc file in the root of the user's home directory so vim can be more YAML-friendly.

autocmd FileType yaml setlocal ai ts=2 sw=2 et number

Module support - If Ansible community version is used, and if any of the modules are not working, check the GitHub site for the module. However, before using any module, confirm the module is still supported in the current Ansible version.

Logic - For issues related to the logic itself, connect to the #Ansible IRC channel on freenode. The community there is very helpful.

During the execution of a playbook, you may need more detailed output for debugging. Follow this link for different ways to debug the execution.

Wrap up

If you are looking to integrate Ansible automation with VM deployment in RHV, these two playbooks will get you started. Obviously, you can modify them to suit the needs of your organization.

Article one provides the goals and design concepts necessary to establish a solid automation environment. In article two, we built that environment and configured the essential components. Finally, here in article three, we created basic playbooks to manage our deployments. There are also tips and tricks throughout all three articles for design, support, and troubleshooting. There are also plenty of references included.

It's time to start your own automated VM deployment process.

Continue with this series

Automate VM deployment with Ansible: Design (Part 1)

Automate VM deployment with Ansible: Automation (Part 2)

References

Documentation for Red Hat Virtualization 4.3

Ansible Documentation

Sample playbooks

The code listings for the playbooks is too long to embed here. Please use the GitHub links below to view and copy the YAML files.

Playbook 1 - single-vm.yml

Playbook 2 - many-vms.yml

[ Wondering how else automation can help you? Get started with The Automated Enterprise, a free book from Red Hat. ]


저자 소개

I am Ashraf Hassan, originally from Egypt, but currently, I am living in the Netherlands
I started my career in 1998 in the telecom industry, specifically the value-added services.
Although my official studies were in the area of telecommunications, I was always attracted to system administration and scripting. I started to sharpen these skills in 2008, during my free time, I like to test new tips and tricks using my home lab.
Working as a senior designer enriched my skills further.
In 2016 I decided to start the journey to be an “RHCA” which I accomplished in 2019, but as IT is a fast-changing domain, I need to keep studying, testing and learning.
In 2019 I joined Red Hat Accelerators once they expanded the program to include Europe; being a member of highly skilled technical experts helped me further.
Please feel free to contact me (info@free-snippets.com) regarding my posts or questions that can pop up related to any of Red Hat Products.

UI_Icon-Red_Hat-Close-A-Black-RGB

채널별 검색

automation icon

오토메이션

기술, 팀, 인프라를 위한 IT 자동화 최신 동향

AI icon

인공지능

고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트

open hybrid cloud icon

오픈 하이브리드 클라우드

하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요

security icon

보안

환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보

edge icon

엣지 컴퓨팅

엣지에서의 운영을 단순화하는 플랫폼 업데이트

Infrastructure icon

인프라

세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보

application development icon

애플리케이션

복잡한 애플리케이션에 대한 솔루션 더 보기

Virtualization icon

가상화

온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래