Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set up an Ansible laboratory

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to set up an Ansible laboratory, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Tools and softwar

This program uses the following tools and software:

Ansible is the automation tool of our choice because it is easy to use and flexible enough to meet the requirements of the laboratory.

Vagrant is easy to use and is used to build and maintain virtual machines.

VirtualBox is a managed hypervisor that can be used in Windows and Linux environments.

Fedora v30 + is the operating system on my local machine.

You must make the following settings to create an environment:

An Internet connection

Enable virtualization technical support in BIOS (the following is the process on my Lenovo laptop)

Vagrant v2.2.9

The latest version of Ansible

The latest version of VirtualBox

Fedora v30 + host operating system

What is the environment of this laboratory?

This project aims to deploy an Ansible host with an Ansible engine and multiple Linux nodes, as well as some preloaded and preconfigured applications (httpd and MySQL). It also enables Cockpit so that you can monitor the status of the virtual machine (VM) during testing. The reason for using pre-deployed applications is to improve efficiency (so you don't have to take the time to install these components). This allows you to focus on creating roles and scripts and testing against the environment in which the above tools are deployed.

We determined that the best solution for our use case is a multi-machine Vagrant environment. The Vagrant file creates three CentOS virtual machines to simulate two target hosts and an Ansible controller.

Host1: no graphical user interface (GUI), install httpd and MySQL

Host2: no GUI, httpd and MySQL installed

Ansible-host: no GUI, Ansible engine installed

Enable multiple hypervisors

If multiple hypervisors are used, some hypervisors may not allow you to pull up the virtual machine. To resolve this problem, follow these steps (Vagrant-based installation instructions).

First, find out the name of the hypervisor:

$lsmod | grep kvmkvm_intel 204800 6kvm 593920 1 kvm_intelirqbypass 16384 1 kvm

I am interested in kvm_intel, but you may need another one (such as kvm_amd).

Blacklist the hypervisor by running the following as root:

$echo 'blacklist kvm-intel' > > / etc/modprobe.d/blacklist.conf

Restart your machine and try to run Vagrant again.

Vagrant file cat Vagrantfile#-*-mode: ruby-*-# vi: set ft=ruby: Vagrant.configure ("2") do | config | # Define VMs with static private IP addresses, vcpu, memory and vagrant-box. Boxes = [{: name = > "web1.demo.com", ⇒ Host1 this is one of the target nodes: box = > "centos/8", ⇒ OS version: ram = > 1024, ⇒ Allocated memory: vcpu = > 1, ⇒ Allocated CPU: ip = > "192.168.29.2" ⇒ Allocated IP address of the node} {: name = > "web2.demo.com", ⇒ Host2 this is one of the target nodes: box = > "centos/8",: ram = > 1024,: vcpu = > 1,: ip = > "192.168.29.3"}, {: name = > "ansible-host", ⇒ Ansible Host with Ansible Engine: box = > "centos/8",: ram = > 8048,: vcpu = > 1 : ip = > "192.168.29.4"}] # Provision each of the VMs. Boxes.each do | opts | config.vm.define opts [: name] do | config | # Only Enable this if you are connecting to Proxy server# config.proxy.http = "http://usernam:password@x.y:80" ⇒ Needed if you have a proxy# config.proxy.https =" http://usernam:password@x.y:80"# config.proxy.no_proxy = "localhost,127.0.0.1" config.vm.synced_folder. "," / vagrant " Id: "vagrant-root", disabled: true config.ssh.insert_key = false config.vm.box = opts [: box] config.vm.hostname = opts [: name] config.vm.provider: virtualbox do | v | ⇒ Defines the vagrant provider v.memory = opts [: ram] v.cpus = opts [: vcpu] end config.vm.network: private_network Ip: opts [: ip] config.vm.provision: file do | file | file.source ='. / keys/vagrant' ⇒ vagrant keys to allow access to the nodes file.destination ='/ tmp/vagrant' ⇒ the location to copy the vagrant key end config.vm.provision: shell Path: "bootstrap-node.sh" ⇒ script that copy hosts entry config.vm.provision: ansible do | ansible | ⇒ declaration to run ansible playbook ansible.verbose = "v" ansible.playbook = "playbook.yml" ⇒ the playbook used to configure the hosts endend endend

These are important documents that you need to pay attention to.

Inventory-test.yaml: a manifest file connected to a node

Script file called by the playbook.yaml:Vagrant provider to configure the node

`Files used by Vagrantfile':Vagrant to deploy the environment

Vagrant key file: the Vagrant key that connects the nodes in the lab environment

You can adjust these documents according to your needs. The flexibility of Ansible gives you the ability to change your environment declaratively according to your needs.

Deploy your lab environment

First, clone the code in the GitHub repository:

$git clone https://github.com/mikecali/ansible-labs-101.gitCloning into 'ansible-labs-101'...remote: Enumerating objects: 15, done.remote: Counting objects: 100% (15x15), done.remote: Compressing objects: 100% (13x13), done.remote: Total 15 (delta 2), reused 10 (delta 0), pack-reused 0Unpacking objects: 100% (15gamma 15), 6.82 KiB | 634.00 KiB/s, done.

Next, change your directory to vagrant-session-2 and view its contents:

$lsBootstrap-node.sh inventory keys playbook.yml README.md Vagrantfile

Now you have all the artifacts and configuration files required for the lab environment. To deploy the environment, run:

$vagrant up

As long as you have a decent network connection, it only takes about 20 minutes to get an operating environment:

$vagrant upBringing machine 'web1.demo.com' up with' virtualbox' provider...Bringing machine 'web2.demo.com' up with' virtualbox' provider...Bringing machine 'ansible-host' up with' virtualbox' provider...== > web1.demo.com: Importing base box' centos/8'...== > web1.demo.com: Matching MAC address for NAT networking...== > web1.demo.com: Checking if box' centos/8' version '1905.1' is up to date...== > web1.demo .com: Setting the name of the VM: ansible-labs_web1democom_1606434176593_70913== > web1.demo.com: Clearing any previously set network interfaces...== > web1.demo.com: Preparing network interfaces based on configuration... Web1.demo.com: Adapter 1: nat web1.demo.com: Adapter 2: hostonly== > web1.demo.com: Forwarding ports... Web1.demo.com: 22 (guest) = > 2222 (host) (adapter 1) = > web1.demo.com: Running 'pre-boot' VM customizations...== > web1.demo.com: Booting VM...== > web1.demo.com: Waiting for machine to boot. This may take a few minutes... Web1.demo.com: SSH address: 127.0.0.1:2222 web1.demo.com: SSH username: vagrant web1.demo.com: SSH auth method: private key [...]

Once the script is executed, you will see the output like this:

PLAY RECAP * * Ansible-host: ok=20 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=3 Real 18m14.288sUser 2m26.978sSys 0m26.849s

Verify that all virtual machines are running:

Vagrant statusCurrent machine states: Web1.demo.com running (virtualbox) Web2.demo.com running (virtualbox) ansible-host running (virtualbox) [...]

You can investigate further by logging in to one of the virtual machines. Visit ansible-host:

> vagrant ssh ansible-hostActivate the web console with: systemctl enable-- now cockpit.socket Last login: Thu Nov 26 12:21:23 2020 from 10.0.2.2 [vagrant@ansible-host ~] uptime16:46:42 up 1:24, 1 user, load average: 0.00,0.01,0.04

Finally, you can use the Ansible module to ping the other nodes you create:

[vagrant@ansible-host] $ansible- I inventory-test.yaml\ webservers-m ping-u vagrant192.168.29.2 | SUCCESS = > {"Ansible-facts": {"Discovered_interpreter_python": "/ usr/libexec/platform-python"}, "Changed": false; "Ping": "pong"} [.] Clear

Run the following command to clean up the environment:

$vagrant destroy [vagrant machine name]

Your output will look like this:

The above is all the contents of the article "how to build an Ansible Lab". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report